@iwascreatedtocreate/asciikit 0.1.1 → 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.
@@ -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;
@@ -51,6 +51,14 @@ export function AsciiAnimator({ art, animate, as: Tag = 'pre', label, color, gra
51
51
  return () => io.disconnect();
52
52
  }
53
53
  }, [trigger, reduced, finalArt]);
54
+ // Geometry check (not IntersectionObserver, whose first report can be wrong in background tabs).
55
+ const isOnScreen = () => {
56
+ const el = ref.current;
57
+ if (!el || typeof window === 'undefined')
58
+ return true;
59
+ const r = el.getBoundingClientRect();
60
+ return r.bottom > 0 && r.right > 0 && r.top < window.innerHeight && r.left < window.innerWidth;
61
+ };
54
62
  // Animation loop
55
63
  useEffect(() => {
56
64
  if (run === 0 || reduced)
@@ -62,7 +70,24 @@ export function AsciiAnimator({ art, animate, as: Tag = 'pre', label, color, gra
62
70
  let iteration = 0;
63
71
  let start = 0;
64
72
  const maxLoops = loop === true ? Infinity : typeof loop === 'number' ? loop : 1;
73
+ let frames = 0;
65
74
  const step = (now) => {
75
+ // Every 15 frames, pause while off screen: no work and no re-renders until it scrolls back in.
76
+ if (frames++ % 15 === 0 && !isOnScreen()) {
77
+ const pausedAt = now;
78
+ const poll = () => {
79
+ if (isOnScreen()) {
80
+ if (start)
81
+ start += performance.now() - pausedAt;
82
+ raf = requestAnimationFrame(step);
83
+ }
84
+ else {
85
+ timer = setTimeout(poll, 250);
86
+ }
87
+ };
88
+ timer = setTimeout(poll, 250);
89
+ return;
90
+ }
66
91
  if (!start)
67
92
  start = now;
68
93
  const elapsed = now - start;
@@ -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';
@@ -1,4 +1,5 @@
1
1
  import { gradients } from '../color.js';
2
+ import { own } from '../util.js';
2
3
  export const DEFAULT_MONO = 'ui-monospace, "SFMono-Regular", "JetBrains Mono", "Cascadia Mono", Menlo, Consolas, "Liberation Mono", monospace';
3
4
  export function asciiStyle(p, extra) {
4
5
  const style = {
@@ -11,7 +12,7 @@ export function asciiStyle(p, extra) {
11
12
  color: p.color,
12
13
  };
13
14
  if (p.gradient) {
14
- const stops = (typeof p.gradient === 'string' ? gradients[p.gradient] : p.gradient).join(', ');
15
+ const stops = (typeof p.gradient === 'string' ? (own(gradients, p.gradient) ?? [p.gradient, p.gradient]) : p.gradient).join(', ');
15
16
  const d = p.gradientDirection ?? 'horizontal';
16
17
  const image = d === 'radial'
17
18
  ? `radial-gradient(circle, ${stops})`
@@ -0,0 +1,56 @@
1
+ import type { ElementType, HTMLAttributes } from 'react';
2
+ import { type BarChartOptions, type ChartData, type LineChartOptions, type ProgressStyle } from '../charts.js';
3
+ import { type TableCell, type TableOptions, type TreeInput, type TreeStyle } from '../layout.js';
4
+ import { type AsciiStyleProps } from './style.js';
5
+ type Base = AsciiStyleProps & Omit<HTMLAttributes<HTMLElement>, 'children' | 'color' | 'title'> & {
6
+ as?: ElementType;
7
+ };
8
+ export interface AsciiArtProps extends Base {
9
+ /** Any pre-made art string: from `ascii()`, `table()`, an `AsciiCanvas`, a file… */
10
+ art: string;
11
+ label?: string;
12
+ }
13
+ /** Show any art string with asciikit styling (gradient, glow, …). Server-component safe. */
14
+ export declare function AsciiArt({ art, label, ...props }: AsciiArtProps): import("react").JSX.Element;
15
+ export interface AsciiTableProps extends Base, Omit<TableOptions, 'style'> {
16
+ rows: readonly (readonly TableCell[])[];
17
+ variant?: TableOptions['style'];
18
+ }
19
+ /** Text-mode table. Server-component safe. */
20
+ export declare function AsciiTable({ rows, head, variant, align, padding, maxWidth, truncate, rowSeparators, title, ...props }: AsciiTableProps): import("react").JSX.Element;
21
+ export interface AsciiTreeProps extends Base {
22
+ data: TreeInput | readonly TreeInput[];
23
+ variant?: TreeStyle;
24
+ }
25
+ /** Tree diagram like the `tree` command. Server-component safe. */
26
+ export declare function AsciiTree({ data, variant, ...props }: AsciiTreeProps): import("react").JSX.Element;
27
+ export type AsciiChartProps = Base & {
28
+ label?: string;
29
+ } & (({
30
+ type: 'bar';
31
+ data: ChartData;
32
+ } & BarChartOptions) | ({
33
+ type: 'line';
34
+ data: readonly number[] | readonly (readonly number[])[];
35
+ } & LineChartOptions) | {
36
+ type: 'sparkline';
37
+ data: readonly (number | null | undefined)[];
38
+ min?: number;
39
+ max?: number;
40
+ });
41
+ /** Bar, line (braille) or sparkline chart. Server-component safe. */
42
+ export declare function AsciiChart(props: AsciiChartProps): import("react").JSX.Element;
43
+ export interface AsciiProgressProps extends Base {
44
+ /** 0…1 */
45
+ value: number;
46
+ /** Text before the bar. */
47
+ text?: string;
48
+ /** Bar width in columns (default 30). */
49
+ width?: number;
50
+ variant?: ProgressStyle;
51
+ /** Show the percentage (default true). */
52
+ showPercent?: boolean;
53
+ }
54
+ /** Text progress bar with progressbar semantics. Server-component safe. */
55
+ export declare function AsciiProgress({ value, width, variant, text, showPercent, ...props }: AsciiProgressProps): import("react").JSX.Element;
56
+ export {};