@iwascreatedtocreate/asciikit 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -5,38 +5,44 @@ import { gradients } from './color.js';
5
5
  import { ascii } from './compose.js';
6
6
  import { boxStyles, toAnsi } from './effects.js';
7
7
  import { toSVG } from './export.js';
8
+ import { critters, say } from './layout.js';
9
+ import { fancy, fancyStyles } from './text.js';
8
10
  import { loadFont } from './fonts/lazy.js';
9
11
  import { fontList } from './fonts/meta.js';
10
- const HELP = `asciikit — ASCII art in your terminal
11
-
12
- usage
13
- asciikit <text> [options]
14
- echo "text" | asciikit [options]
15
-
16
- options
17
- -f, --font <name> font id or name (default: standard) e.g. slant, "ANSI Shadow"
18
- -l, --list list all fonts (combine with --preview to render each one)
19
- --preview <text> sample text for --list
20
- -w, --width <n> wrap at n columns (default: terminal width)
21
- -a, --align <a> left | center | right
22
- --layout <l> default | full | fitted | smush | universal-smush
23
- --letter-spacing <n> extra columns between characters
24
- -b, --border [style] ${Object.keys(boxStyles).join(' | ')}
25
- --title <text> border title
26
- -s, --shadow drop shadow
27
- --fill <char> replace every visible character
28
- -g, --gradient <g> preset (${Object.keys(gradients).join(', ')}) or colors "#f00,#00f"
29
- -d, --direction <d> horizontal | vertical | diagonal | radial
30
- -c, --color <color> single text color
31
- --no-color plain text, no ANSI colors
32
- --svg <file> also write an SVG image
33
- -v, --version print version
34
- -h, --help show this help
35
-
36
- examples
37
- asciikit hello
38
- asciikit "deploy ok" -f slant -g sunset -b rounded
39
- asciikit --list --preview Hi
12
+ const HELP = `asciikit — ASCII art in your terminal
13
+
14
+ usage
15
+ asciikit <text> [options]
16
+ echo "text" | asciikit [options]
17
+
18
+ options
19
+ -f, --font <name> font id or name (default: standard) e.g. slant, "ANSI Shadow"
20
+ -l, --list list all fonts (combine with --preview to render each one)
21
+ --preview <text> sample text for --list
22
+ -w, --width <n> wrap at n columns (default: terminal width)
23
+ -a, --align <a> left | center | right
24
+ --layout <l> default | full | fitted | smush | universal-smush
25
+ --letter-spacing <n> extra columns between characters
26
+ -b, --border [style] ${Object.keys(boxStyles).join(' | ')}
27
+ --title <text> border title
28
+ -s, --shadow drop shadow
29
+ --fill <char> replace every visible character
30
+ -g, --gradient <g> preset (${Object.keys(gradients).join(', ')}) or colors "#f00,#00f"
31
+ -d, --direction <d> horizontal | vertical | diagonal | radial
32
+ -c, --color <color> single text color
33
+ --no-color plain text, no ANSI colors
34
+ --fancy <style> unicode text instead of a font: bold, script, fraktur, circled, smallCaps, upsideDown…
35
+ --say <critter> put the text in a speech bubble (${Object.keys(critters).join(' | ')})
36
+ --svg <file> also write an SVG image
37
+ -v, --version print version
38
+ -h, --help show this help
39
+
40
+ examples
41
+ asciikit hello
42
+ asciikit "deploy ok" -f slant -g sunset -b rounded
43
+ asciikit --list --preview Hi
44
+ asciikit "ship it" --say robot
45
+ asciikit hello --fancy fraktur
40
46
  `;
41
47
  function fail(message) {
42
48
  process.stderr.write(`asciikit: ${message}\n`);
@@ -71,6 +77,8 @@ async function main() {
71
77
  direction: { type: 'string', short: 'd' },
72
78
  color: { type: 'string', short: 'c' },
73
79
  'no-color': { type: 'boolean' },
80
+ fancy: { type: 'string' },
81
+ say: { type: 'string' },
74
82
  svg: { type: 'string' },
75
83
  version: { type: 'boolean', short: 'v' },
76
84
  help: { type: 'boolean', short: 'h' },
@@ -102,6 +110,16 @@ async function main() {
102
110
  const text = positionals.length ? positionals.join(' ') : await readStdin();
103
111
  if (!text)
104
112
  return void process.stdout.write(HELP);
113
+ // these two skip FIGlet fonts entirely
114
+ if (o.fancy !== undefined || o.say !== undefined) {
115
+ if (o.fancy !== undefined && !fancyStyles.includes(o.fancy))
116
+ fail(`unknown --fancy style "${o.fancy}". Try: ${fancyStyles.join(', ')}`);
117
+ if (o.say && !Object.prototype.hasOwnProperty.call(critters, o.say))
118
+ fail(`unknown critter "${o.say}". Try: ${Object.keys(critters).join(', ')}`);
119
+ const styled = o.fancy !== undefined ? fancy(text, o.fancy) : text;
120
+ const out = o.say !== undefined ? say(styled, (o.say || 'cat')) : styled;
121
+ return void process.stdout.write(`${out}\n`);
122
+ }
105
123
  let font;
106
124
  try {
107
125
  font = await loadFont(o.font ?? 'standard');
package/dist/index.d.ts CHANGED
@@ -10,3 +10,7 @@ export { animationFrame, continuousEffects, type AnimationEffect, type FrameOpti
10
10
  export { spinners, type Spinner, type SpinnerName } from './spinners.js';
11
11
  export { toSVG, toCanvas, toPNG, downloadFile, type ImageExportOptions } from './export.js';
12
12
  export { graphemes, charWidth, stringWidth, cells } from './width.js';
13
+ export { wrapText, truncate, fancy, fancyStyles, type FancyStyle, type WrapOptions } from './text.js';
14
+ export { table, tableStyles, tree, treeStyles, divider, speechBubble, say, critters, type TableChars, type TableStyle, type TableCell, type TableOptions, type TreeNode, type TreeInput, type TreeStyle, type DividerOptions, type BubbleOptions, type CritterName, } from './layout.js';
15
+ export { sparkline, barChart, lineChart, progressBar, progressStyles, type SparklineOptions, type BarChartOptions, type LineChartOptions, type ProgressOptions, type ProgressStyle, type ChartData, } from './charts.js';
16
+ export { AsciiCanvas, BrailleCanvas, type RectOptions } from './canvas.js';
package/dist/index.js CHANGED
@@ -9,3 +9,7 @@ export { animationFrame, continuousEffects } from './animate.js';
9
9
  export { spinners } from './spinners.js';
10
10
  export { toSVG, toCanvas, toPNG, downloadFile } from './export.js';
11
11
  export { graphemes, charWidth, stringWidth, cells } from './width.js';
12
+ export { wrapText, truncate, fancy, fancyStyles } from './text.js';
13
+ export { table, tableStyles, tree, treeStyles, divider, speechBubble, say, critters, } from './layout.js';
14
+ export { sparkline, barChart, lineChart, progressBar, progressStyles, } from './charts.js';
15
+ export { AsciiCanvas, BrailleCanvas } from './canvas.js';
@@ -0,0 +1,307 @@
1
+ import { type Art, type BoxChars, type BoxStyle } from './effects.js';
2
+ import type { Align } from './types.js';
3
+ export interface TableChars {
4
+ /** top: left corner, line, junction, right corner */
5
+ tl: string;
6
+ t: string;
7
+ tj: string;
8
+ tr: string;
9
+ /** middle separator: left tee, line, cross, right tee */
10
+ ml: string;
11
+ m: string;
12
+ mj: string;
13
+ mr: string;
14
+ /** bottom: left corner, line, junction, right corner */
15
+ bl: string;
16
+ b: string;
17
+ bj: string;
18
+ br: string;
19
+ /** vertical: left edge, between columns, right edge */
20
+ l: string;
21
+ c: string;
22
+ r: string;
23
+ }
24
+ export declare const tableStyles: {
25
+ single: {
26
+ tl: string;
27
+ t: string;
28
+ tj: string;
29
+ tr: string;
30
+ ml: string;
31
+ m: string;
32
+ mj: string;
33
+ mr: string;
34
+ bl: string;
35
+ b: string;
36
+ bj: string;
37
+ br: string;
38
+ l: string;
39
+ c: string;
40
+ r: string;
41
+ };
42
+ double: {
43
+ tl: string;
44
+ t: string;
45
+ tj: string;
46
+ tr: string;
47
+ ml: string;
48
+ m: string;
49
+ mj: string;
50
+ mr: string;
51
+ bl: string;
52
+ b: string;
53
+ bj: string;
54
+ br: string;
55
+ l: string;
56
+ c: string;
57
+ r: string;
58
+ };
59
+ rounded: {
60
+ tl: string;
61
+ t: string;
62
+ tj: string;
63
+ tr: string;
64
+ ml: string;
65
+ m: string;
66
+ mj: string;
67
+ mr: string;
68
+ bl: string;
69
+ b: string;
70
+ bj: string;
71
+ br: string;
72
+ l: string;
73
+ c: string;
74
+ r: string;
75
+ };
76
+ heavy: {
77
+ tl: string;
78
+ t: string;
79
+ tj: string;
80
+ tr: string;
81
+ ml: string;
82
+ m: string;
83
+ mj: string;
84
+ mr: string;
85
+ bl: string;
86
+ b: string;
87
+ bj: string;
88
+ br: string;
89
+ l: string;
90
+ c: string;
91
+ r: string;
92
+ };
93
+ dashed: {
94
+ tl: string;
95
+ t: string;
96
+ tj: string;
97
+ tr: string;
98
+ ml: string;
99
+ m: string;
100
+ mj: string;
101
+ mr: string;
102
+ bl: string;
103
+ b: string;
104
+ bj: string;
105
+ br: string;
106
+ l: string;
107
+ c: string;
108
+ r: string;
109
+ };
110
+ ascii: {
111
+ tl: string;
112
+ t: string;
113
+ tj: string;
114
+ tr: string;
115
+ ml: string;
116
+ m: string;
117
+ mj: string;
118
+ mr: string;
119
+ bl: string;
120
+ b: string;
121
+ bj: string;
122
+ br: string;
123
+ l: string;
124
+ c: string;
125
+ r: string;
126
+ };
127
+ minimal: {
128
+ tl: string;
129
+ t: string;
130
+ tj: string;
131
+ tr: string;
132
+ ml: string;
133
+ m: string;
134
+ mj: string;
135
+ mr: string;
136
+ bl: string;
137
+ b: string;
138
+ bj: string;
139
+ br: string;
140
+ l: string;
141
+ c: string;
142
+ r: string;
143
+ };
144
+ markdown: {
145
+ tl: string;
146
+ t: string;
147
+ tj: string;
148
+ tr: string;
149
+ ml: string;
150
+ m: string;
151
+ mj: string;
152
+ mr: string;
153
+ bl: string;
154
+ b: string;
155
+ bj: string;
156
+ br: string;
157
+ l: string;
158
+ c: string;
159
+ r: string;
160
+ };
161
+ none: {
162
+ tl: string;
163
+ t: string;
164
+ tj: string;
165
+ tr: string;
166
+ ml: string;
167
+ m: string;
168
+ mj: string;
169
+ mr: string;
170
+ bl: string;
171
+ b: string;
172
+ bj: string;
173
+ br: string;
174
+ l: string;
175
+ c: string;
176
+ r: string;
177
+ };
178
+ };
179
+ export type TableStyle = keyof typeof tableStyles;
180
+ export type TableCell = string | number | boolean | null | undefined;
181
+ export interface TableOptions {
182
+ /** Header row. */
183
+ head?: readonly TableCell[];
184
+ /** Border style (default `single`). */
185
+ style?: TableStyle | TableChars;
186
+ /** Column alignment, one value for all columns or one per column. */
187
+ align?: Align | readonly Align[];
188
+ /** Spaces on each side of a cell (default 1). */
189
+ padding?: number;
190
+ /** Max width per column (one value or per column). Longer cells wrap (or truncate). */
191
+ maxWidth?: number | readonly (number | undefined)[];
192
+ /** Truncate long cells with … instead of wrapping (default false). */
193
+ truncate?: boolean;
194
+ /** Draw a line between every row (default false). */
195
+ rowSeparators?: boolean;
196
+ /** Title shown centered above the table. */
197
+ title?: string;
198
+ }
199
+ /**
200
+ * Render rows as a text table. Cells may be multi-line (even ASCII art), wide characters
201
+ * line up, and long cells wrap inside `maxWidth`.
202
+ *
203
+ * ```ts
204
+ * table([['asciikit', '332 fonts'], ['figlet', 'classic']], { head: ['name', 'notes'], style: 'rounded' })
205
+ * ```
206
+ */
207
+ export declare function table(rows: readonly (readonly TableCell[])[], options?: TableOptions): string;
208
+ export interface TreeNode {
209
+ label: string;
210
+ children?: readonly TreeInput[];
211
+ }
212
+ /** A node, a plain label, or an object whose keys are labels (`null` = leaf). */
213
+ export type TreeInput = string | TreeNode | {
214
+ readonly [label: string]: TreeInput | readonly TreeInput[] | null;
215
+ };
216
+ export declare const treeStyles: {
217
+ readonly unicode: {
218
+ readonly branch: '├── ';
219
+ readonly last: '└── ';
220
+ readonly pipe: '│ ';
221
+ readonly space: ' ';
222
+ };
223
+ readonly rounded: {
224
+ readonly branch: '├── ';
225
+ readonly last: '╰── ';
226
+ readonly pipe: '│ ';
227
+ readonly space: ' ';
228
+ };
229
+ readonly heavy: {
230
+ readonly branch: '┣━━ ';
231
+ readonly last: '┗━━ ';
232
+ readonly pipe: '┃ ';
233
+ readonly space: ' ';
234
+ };
235
+ readonly double: {
236
+ readonly branch: '╠══ ';
237
+ readonly last: '╚══ ';
238
+ readonly pipe: '║ ';
239
+ readonly space: ' ';
240
+ };
241
+ readonly ascii: {
242
+ readonly branch: '|-- ';
243
+ readonly last: '`-- ';
244
+ readonly pipe: '| ';
245
+ readonly space: ' ';
246
+ };
247
+ readonly compact: {
248
+ readonly branch: '├ ';
249
+ readonly last: '└ ';
250
+ readonly pipe: '│ ';
251
+ readonly space: ' ';
252
+ };
253
+ };
254
+ export type TreeStyle = keyof typeof treeStyles;
255
+ /**
256
+ * Draw a tree, like the `tree` command.
257
+ *
258
+ * ```ts
259
+ * tree({ src: { 'index.ts': null, react: ['Ascii.tsx', 'hooks.ts'] }, 'package.json': null })
260
+ * ```
261
+ */
262
+ export declare function tree(input: TreeInput | readonly TreeInput[], options?: {
263
+ style?: TreeStyle;
264
+ }): string;
265
+ export interface DividerOptions {
266
+ /** Total width (default 40). */
267
+ width?: number;
268
+ /** Line character(s), repeated (default `─`). */
269
+ char?: string;
270
+ /** Label position (default center). */
271
+ align?: Align;
272
+ }
273
+ /** A horizontal rule, optionally with a label: `──── Section ────`. */
274
+ export declare function divider(label?: string, options?: DividerOptions): string;
275
+ export interface BubbleOptions {
276
+ /** Wrap the text at this many columns (default 40). */
277
+ width?: number;
278
+ /** `say` (pointed tail), `think` (o O bubbles), or any box style for the frame. */
279
+ kind?: 'say' | 'think';
280
+ /** Frame style (default `rounded`). */
281
+ style?: BoxStyle | BoxChars;
282
+ /** Where the tail goes (default `left`). */
283
+ tail?: 'left' | 'right' | 'center' | 'none';
284
+ /** Tail length in rows (default 2). */
285
+ tailLength?: number;
286
+ }
287
+ /** Wrap text in a speech (or thought) bubble with a tail pointing down. */
288
+ export declare function speechBubble(text: string, options?: BubbleOptions): string;
289
+ /** Small original characters to pair with {@link say}. */
290
+ export declare const critters: {
291
+ readonly cat: string;
292
+ readonly robot: string;
293
+ readonly ghost: string;
294
+ readonly blob: string;
295
+ readonly bird: string;
296
+ readonly computer: string;
297
+ };
298
+ export type CritterName = keyof typeof critters;
299
+ /**
300
+ * A character saying something: speech bubble on top, any art (or a built-in critter) below.
301
+ *
302
+ * ```ts
303
+ * say('ship it', 'robot')
304
+ * say('hello', myArt, { kind: 'think' })
305
+ * ```
306
+ */
307
+ export declare function say(text: string, character?: CritterName | Art, options?: BubbleOptions): string;
package/dist/layout.js ADDED
@@ -0,0 +1,194 @@
1
+ import { box, measure, normalize, toLines } from './effects.js';
2
+ import { wrapText, truncate } from './text.js';
3
+ import { own } from './util.js';
4
+ import { stringWidth } from './width.js';
5
+ const padTo = (s, width, align = 'left') => {
6
+ const free = Math.max(0, width - stringWidth(s));
7
+ const left = align === 'center' ? Math.floor(free / 2) : align === 'right' ? free : 0;
8
+ return ' '.repeat(left) + s + ' '.repeat(free - left);
9
+ };
10
+ export const tableStyles = {
11
+ single: { tl: '┌', t: '─', tj: '┬', tr: '┐', ml: '├', m: '─', mj: '┼', mr: '┤', bl: '└', b: '─', bj: '┴', br: '┘', l: '│', c: '│', r: '│' },
12
+ double: { tl: '╔', t: '═', tj: '╦', tr: '╗', ml: '╠', m: '═', mj: '╬', mr: '╣', bl: '╚', b: '═', bj: '╩', br: '╝', l: '║', c: '║', r: '║' },
13
+ rounded: { tl: '╭', t: '─', tj: '┬', tr: '╮', ml: '├', m: '─', mj: '┼', mr: '┤', bl: '╰', b: '─', bj: '┴', br: '╯', l: '│', c: '│', r: '│' },
14
+ heavy: { tl: '┏', t: '━', tj: '┳', tr: '┓', ml: '┣', m: '━', mj: '╋', mr: '┫', bl: '┗', b: '━', bj: '┻', br: '┛', l: '┃', c: '┃', r: '┃' },
15
+ dashed: { tl: '┌', t: '╌', tj: '┬', tr: '┐', ml: '├', m: '╌', mj: '┼', mr: '┤', bl: '└', b: '╌', bj: '┴', br: '┘', l: '╎', c: '╎', r: '╎' },
16
+ ascii: { tl: '+', t: '-', tj: '+', tr: '+', ml: '+', m: '-', mj: '+', mr: '+', bl: '+', b: '-', bj: '+', br: '+', l: '|', c: '|', r: '|' },
17
+ // no outer frame, just lines between columns and under the header
18
+ minimal: { tl: '', t: '', tj: '', tr: '', ml: '', m: '─', mj: '┼', mr: '', bl: '', b: '', bj: '', br: '', l: '', c: '│', r: '' },
19
+ // github flavored markdown
20
+ markdown: { tl: '', t: '', tj: '', tr: '', ml: '|', m: '-', mj: '|', mr: '|', bl: '', b: '', bj: '', br: '', l: '|', c: '|', r: '|' },
21
+ none: { tl: '', t: '', tj: '', tr: '', ml: '', m: '', mj: '', mr: '', bl: '', b: '', bj: '', br: '', l: '', c: ' ', r: '' },
22
+ };
23
+ const perCol = (v, i) => Array.isArray(v) ? v[i] : v;
24
+ /**
25
+ * Render rows as a text table. Cells may be multi-line (even ASCII art), wide characters
26
+ * line up, and long cells wrap inside `maxWidth`.
27
+ *
28
+ * ```ts
29
+ * table([['asciikit', '332 fonts'], ['figlet', 'classic']], { head: ['name', 'notes'], style: 'rounded' })
30
+ * ```
31
+ */
32
+ export function table(rows, options = {}) {
33
+ const s = typeof options.style === 'object' ? options.style : (own(tableStyles, options.style ?? 'single') ?? tableStyles.single);
34
+ const markdown = options.style === 'markdown';
35
+ const pad = Math.max(0, options.padding ?? 1);
36
+ const all = options.head ? [options.head, ...rows] : [...rows];
37
+ const cols = Math.max(0, ...all.map((r) => r.length));
38
+ // each cell becomes an array of lines
39
+ const grid = all.map((row) => Array.from({ length: cols }, (_, i) => {
40
+ const raw = row[i] == null || row[i] === false ? '' : String(row[i]);
41
+ const max = perCol(options.maxWidth, i);
42
+ if (!max)
43
+ return raw.split('\n');
44
+ return options.truncate ? raw.split('\n').map((l) => truncate(l, max)) : wrapText(raw, max).split('\n');
45
+ }));
46
+ const widths = Array.from({ length: cols }, (_, i) => Math.max(markdown ? 3 : 1, ...grid.map((row) => Math.max(0, ...row[i].map(stringWidth)))));
47
+ const sp = ' '.repeat(pad);
48
+ const rule = (l, fill, j, r) => {
49
+ if (!fill)
50
+ return null;
51
+ const parts = widths.map((w, i) => {
52
+ if (!markdown)
53
+ return fill.repeat(w + pad * 2);
54
+ const a = perCol(options.align, i) ?? 'left';
55
+ const inner = '-'.repeat(w + pad * 2 - 2);
56
+ return (a === 'center' || a === 'left' ? ':' : '-') + inner + (a === 'center' || a === 'right' ? ':' : '-');
57
+ });
58
+ return l + parts.join(j) + r;
59
+ };
60
+ const line = (cellsInRow) => {
61
+ const height = Math.max(1, ...cellsInRow.map((c) => c.length));
62
+ return Array.from({ length: height }, (_, k) => s.l + cellsInRow.map((c, i) => sp + padTo(c[k] ?? '', widths[i], perCol(options.align, i)) + sp).join(s.c) + s.r);
63
+ };
64
+ const out = [];
65
+ const total = widths.reduce((x, w) => x + w + pad * 2, 0) + stringWidth(s.c) * Math.max(0, cols - 1) + stringWidth(s.l) + stringWidth(s.r);
66
+ if (options.title)
67
+ out.push(padTo(truncate(options.title, total), total, 'center').trimEnd());
68
+ out.push(rule(s.tl, s.t, s.tj, s.tr));
69
+ grid.forEach((row, i) => {
70
+ if (i > 0 && (options.head ? i > 1 : true) && options.rowSeparators)
71
+ out.push(rule(s.ml, s.m, s.mj, s.mr));
72
+ out.push(...line(row));
73
+ if (i === 0 && options.head)
74
+ out.push(rule(s.ml, s.m || '─', s.mj || ' ', s.mr));
75
+ });
76
+ out.push(rule(s.bl, s.b, s.bj, s.br));
77
+ return out
78
+ .filter((l) => l !== null)
79
+ .map((l) => l.trimEnd())
80
+ .join('\n');
81
+ }
82
+ export const treeStyles = {
83
+ unicode: { branch: '├── ', last: '└── ', pipe: '│ ', space: ' ' },
84
+ rounded: { branch: '├── ', last: '╰── ', pipe: '│ ', space: ' ' },
85
+ heavy: { branch: '┣━━ ', last: '┗━━ ', pipe: '┃ ', space: ' ' },
86
+ double: { branch: '╠══ ', last: '╚══ ', pipe: '║ ', space: ' ' },
87
+ ascii: { branch: '|-- ', last: '`-- ', pipe: '| ', space: ' ' },
88
+ compact: { branch: '├ ', last: '└ ', pipe: '│ ', space: ' ' },
89
+ };
90
+ function toNodes(input) {
91
+ if (Array.isArray(input))
92
+ return input.flatMap((n) => toNodes(n));
93
+ if (typeof input === 'string')
94
+ return [{ label: input }];
95
+ if (input && typeof input === 'object' && 'label' in input && typeof input.label === 'string')
96
+ return [input];
97
+ return Object.entries(input).map(([label, v]) => ({
98
+ label,
99
+ children: v == null ? undefined : toNodes(v),
100
+ }));
101
+ }
102
+ /**
103
+ * Draw a tree, like the `tree` command.
104
+ *
105
+ * ```ts
106
+ * tree({ src: { 'index.ts': null, react: ['Ascii.tsx', 'hooks.ts'] }, 'package.json': null })
107
+ * ```
108
+ */
109
+ export function tree(input, options = {}) {
110
+ const st = own(treeStyles, options.style ?? 'unicode') ?? treeStyles.unicode;
111
+ const roots = toNodes(input);
112
+ const out = [];
113
+ const walk = (nodes, prefix, top) => {
114
+ nodes.forEach((n, i) => {
115
+ const last = i === nodes.length - 1;
116
+ const [first = '', ...rest] = String(n.label).split('\n');
117
+ // a single root is drawn bare, like `tree` does with the folder name
118
+ const bare = top && roots.length === 1;
119
+ const head = bare ? '' : prefix + (last ? st.last : st.branch);
120
+ const cont = bare ? '' : prefix + (last ? st.space : st.pipe);
121
+ out.push(head + first);
122
+ for (const r of rest)
123
+ out.push(cont + r);
124
+ if (n.children?.length)
125
+ walk(toNodes(n.children), cont, false);
126
+ });
127
+ };
128
+ walk(roots, '', true);
129
+ return out.map((l) => l.trimEnd()).join('\n');
130
+ }
131
+ /** A horizontal rule, optionally with a label: `──── Section ────`. */
132
+ export function divider(label, options = {}) {
133
+ const width = Math.max(1, Math.floor(options.width ?? 40));
134
+ const ch = options.char || '─';
135
+ const fill = (n) => {
136
+ if (n <= 0)
137
+ return '';
138
+ const unit = stringWidth(ch) || 1;
139
+ return ch.repeat(Math.ceil(n / unit)).slice(0, n);
140
+ };
141
+ if (!label)
142
+ return fill(width);
143
+ const text = ` ${truncate(label, Math.max(1, width - 2))} `;
144
+ const free = width - stringWidth(text);
145
+ const align = options.align ?? 'center';
146
+ const left = align === 'left' ? Math.min(2, free) : align === 'right' ? free - Math.min(2, free) : Math.floor(free / 2);
147
+ return fill(left) + text + fill(free - left);
148
+ }
149
+ /** Wrap text in a speech (or thought) bubble with a tail pointing down. */
150
+ export function speechBubble(text, options = {}) {
151
+ const width = Math.max(4, options.width ?? 40);
152
+ const body = box(wrapText(text, width), { style: options.style ?? 'rounded', padding: { x: 1, y: 0 } });
153
+ const tail = options.tail ?? 'left';
154
+ if (tail === 'none')
155
+ return body;
156
+ const w = measure(body).width;
157
+ const n = Math.max(1, options.tailLength ?? 2);
158
+ const think = options.kind === 'think';
159
+ const base = tail === 'left' ? 3 : tail === 'right' ? w - 4 : Math.floor(w / 2);
160
+ const rows = [];
161
+ for (let i = 0; i < n; i++) {
162
+ if (think)
163
+ rows.push(' '.repeat(Math.max(0, base + (tail === 'right' ? i : -i))) + (i === 0 ? 'o' : '°'));
164
+ else if (tail === 'right')
165
+ rows.push(' '.repeat(Math.max(0, base + i)) + '\\');
166
+ else if (tail === 'center')
167
+ rows.push(' '.repeat(base) + '|');
168
+ else
169
+ rows.push(' '.repeat(Math.max(0, base - i)) + '/');
170
+ }
171
+ return body + '\n' + rows.join('\n');
172
+ }
173
+ /** Small original characters to pair with {@link say}. */
174
+ export const critters = {
175
+ cat: [' /\\_/\\', '( ^.^ )', ' ( )~', ' " "'].join('\n'),
176
+ robot: [' [■_■]', ' /| |\\', ' |___|', ' / \\'].join('\n'),
177
+ ghost: [' .--.', ' ( °° )', ' | |', ' |/\\/\\|'].join('\n'),
178
+ blob: [' ____', ' / •• \\', '( ‿ )', ' \\____/'].join('\n'),
179
+ bird: [' __', ' (•>', ' //)', ' ^^'].join('\n'),
180
+ computer: [' ._____.', ' | >_ |', ' |_____|', ' /_____\\'].join('\n'),
181
+ };
182
+ /**
183
+ * A character saying something: speech bubble on top, any art (or a built-in critter) below.
184
+ *
185
+ * ```ts
186
+ * say('ship it', 'robot')
187
+ * say('hello', myArt, { kind: 'think' })
188
+ * ```
189
+ */
190
+ export function say(text, character = 'cat', options = {}) {
191
+ const art = typeof character === 'string' && own(critters, character) !== undefined ? critters[character] : character;
192
+ const bubble = speechBubble(text, { tail: 'left', ...options });
193
+ return normalize([...toLines(bubble), ...toLines(art)].join('\n')).replace(/[ ]+$/gm, '');
194
+ }
@@ -43,7 +43,7 @@ export declare function Ascii(props: AsciiProps): ReactElement;
43
43
  * ```
44
44
  */
45
45
  export declare function createAscii(text: string, defaults?: Omit<AsciiProps, 'text' | 'children'>): {
46
- (props: Omit<AsciiProps, "text" | "children"> & {
46
+ (props: Omit<AsciiProps, 'text' | 'children'> & {
47
47
  text?: string;
48
48
  }): import("react").JSX.Element;
49
49
  displayName: string;
@@ -1,5 +1,5 @@
1
1
  'use client';
2
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
3
  import { useEffect, useState } from 'react';
4
4
  import { imageToAscii } from '../image.js';
5
5
  import { asciiStyle } from './style.js';
@@ -5,3 +5,4 @@ export { AsciiSpinner, type AsciiSpinnerProps } from './AsciiSpinner.js';
5
5
  export { AsciiBox, type AsciiBoxProps } from './AsciiBox.js';
6
6
  export { useAscii, useFont, type UseFontResult } from './hooks.js';
7
7
  export { asciiStyle, DEFAULT_MONO, type AsciiStyleProps } from './style.js';
8
+ export { AsciiArt, AsciiTable, AsciiTree, AsciiChart, AsciiProgress, type AsciiArtProps, type AsciiTableProps, type AsciiTreeProps, type AsciiChartProps, type AsciiProgressProps, } from './widgets.js';
@@ -5,3 +5,4 @@ export { AsciiSpinner } from './AsciiSpinner.js';
5
5
  export { AsciiBox } from './AsciiBox.js';
6
6
  export { useAscii, useFont } from './hooks.js';
7
7
  export { asciiStyle, DEFAULT_MONO } from './style.js';
8
+ export { AsciiArt, AsciiTable, AsciiTree, AsciiChart, AsciiProgress, } from './widgets.js';