@iwascreatedtocreate/asciikit 0.1.2 → 0.3.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/LICENSE +24 -24
- package/README.md +219 -122
- package/dist/animate.d.ts +1 -1
- package/dist/animate.js +38 -1
- package/dist/canvas.d.ts +101 -0
- package/dist/canvas.js +321 -0
- package/dist/charts.d.ts +133 -0
- package/dist/charts.js +178 -0
- package/dist/cli.js +48 -30
- package/dist/element.d.ts +32 -0
- package/dist/element.js +226 -0
- package/dist/export.d.ts +4 -1
- package/dist/export.js +63 -24
- package/dist/glyphs.d.ts +12 -0
- package/dist/glyphs.js +96 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/layout.d.ts +307 -0
- package/dist/layout.js +194 -0
- package/dist/react/Ascii.d.ts +1 -1
- package/dist/react/AsciiImage.js +1 -1
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/react/widgets.d.ts +56 -0
- package/dist/react/widgets.js +52 -0
- package/dist/text.d.ts +26 -0
- package/dist/text.js +230 -0
- package/package.json +93 -87
package/dist/canvas.js
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { boxStyles, toLines } from './effects.js';
|
|
2
|
+
import { clamp, own } from './util.js';
|
|
3
|
+
import { MAX_COLUMNS, MAX_ROWS } from './image.js';
|
|
4
|
+
import { cells, charWidth, fixCells } from './width.js';
|
|
5
|
+
// light box-drawing chars as up/right/down/left bits, so crossing lines merge into ┼ ├ ┬…
|
|
6
|
+
const U = 1, R = 2, D = 4, L = 8;
|
|
7
|
+
const LIGHT = {
|
|
8
|
+
'─': R | L, '│': U | D, '┌': R | D, '┐': D | L, '└': U | R, '┘': U | L,
|
|
9
|
+
'├': U | D | R, '┤': U | D | L, '┬': R | D | L, '┴': U | R | L, '┼': U | R | D | L,
|
|
10
|
+
'╴': L, '╶': R, '╵': U, '╷': D,
|
|
11
|
+
};
|
|
12
|
+
const FROM_BITS = {};
|
|
13
|
+
for (const [ch, bits] of Object.entries(LIGHT))
|
|
14
|
+
FROM_BITS[bits] ?? (FROM_BITS[bits] = ch);
|
|
15
|
+
// rounded corners merge like their square twins
|
|
16
|
+
const ALIAS = { '╭': '┌', '╮': '┐', '╰': '└', '╯': '┘' };
|
|
17
|
+
/**
|
|
18
|
+
* A fixed-size character grid you can draw on: text, lines, boxes, circles and other art.
|
|
19
|
+
* Light box lines that touch merge into proper junctions (┼ ├ ┬ …).
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const c = new AsciiCanvas(30, 8);
|
|
23
|
+
* c.rect(0, 0, 14, 5, { title: 'api' }).rect(16, 0, 14, 5, { title: 'db' }).line(14, 2, 16, 2, '─');
|
|
24
|
+
* c.toString();
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export class AsciiCanvas {
|
|
28
|
+
constructor(width, height, background = ' ') {
|
|
29
|
+
this.width = Math.floor(clamp(width, 1, MAX_COLUMNS, 40));
|
|
30
|
+
this.height = Math.floor(clamp(height, 1, MAX_ROWS, 10));
|
|
31
|
+
const bg = cells(background)[0] && charWidth(background) === 1 ? background : ' ';
|
|
32
|
+
this.grid = Array.from({ length: this.height }, () => Array(this.width).fill(bg));
|
|
33
|
+
}
|
|
34
|
+
/** Read the character at a cell ('' for the right half of a wide char). */
|
|
35
|
+
get(x, y) {
|
|
36
|
+
return this.grid[Math.floor(y)]?.[Math.floor(x)];
|
|
37
|
+
}
|
|
38
|
+
/** Put one character. Light box chars merge with what's already there unless `merge` is false. */
|
|
39
|
+
set(x, y, ch, merge = true) {
|
|
40
|
+
const cx = Math.round(x);
|
|
41
|
+
const cy = Math.round(y);
|
|
42
|
+
const row = this.grid[cy];
|
|
43
|
+
if (!row || cx < 0 || cx >= this.width || !ch)
|
|
44
|
+
return this;
|
|
45
|
+
const c = cells(ch)[0] ?? ' ';
|
|
46
|
+
if (charWidth(c) === 2 && cx + 1 >= this.width)
|
|
47
|
+
return this; // no room for a wide char
|
|
48
|
+
const prev = ALIAS[row[cx]] ?? row[cx];
|
|
49
|
+
const next = ALIAS[c] ?? c;
|
|
50
|
+
if (merge && LIGHT[prev] !== undefined && LIGHT[next] !== undefined && prev !== next) {
|
|
51
|
+
row[cx] = FROM_BITS[LIGHT[prev] | LIGHT[next]] ?? c;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
row[cx] = c;
|
|
55
|
+
if (charWidth(c) === 2)
|
|
56
|
+
row[cx + 1] = '';
|
|
57
|
+
}
|
|
58
|
+
this.grid[cy] = fixCells(row);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
/** Write a string starting at (x, y). Newlines start a new row at the same x. */
|
|
62
|
+
text(x, y, str) {
|
|
63
|
+
toLines(String(str)).forEach((line, i) => {
|
|
64
|
+
let col = Math.round(x);
|
|
65
|
+
for (const c of cells(line)) {
|
|
66
|
+
if (c !== '')
|
|
67
|
+
this.set(col, y + i, c, false);
|
|
68
|
+
col++;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Draw a straight line (Bresenham). Without `ch` the character follows the slope:
|
|
75
|
+
* `─ │` for straight lines and `/ \` for diagonals.
|
|
76
|
+
*/
|
|
77
|
+
line(x0, y0, x1, y1, ch) {
|
|
78
|
+
let x = Math.round(x0), y = Math.round(y0);
|
|
79
|
+
const xe = Math.round(x1), ye = Math.round(y1);
|
|
80
|
+
const dx = Math.abs(xe - x), dy = -Math.abs(ye - y);
|
|
81
|
+
const sx = x < xe ? 1 : -1, sy = y < ye ? 1 : -1;
|
|
82
|
+
const auto = dy === 0 ? '─' : dx === 0 ? '│' : sx === sy ? '\\' : '/';
|
|
83
|
+
let err = dx + dy;
|
|
84
|
+
const sx0 = x, sy0 = y;
|
|
85
|
+
// a straight line that ends on another light line only joins it with its own half (├ not ┼)
|
|
86
|
+
const half = (px, py, start) => {
|
|
87
|
+
if (ch !== undefined || (auto !== '─' && auto !== '│'))
|
|
88
|
+
return undefined;
|
|
89
|
+
const under = this.get(px, py);
|
|
90
|
+
if (under === undefined || LIGHT[ALIAS[under] ?? under] === undefined)
|
|
91
|
+
return undefined;
|
|
92
|
+
const toward = auto === '─' ? ((sx > 0) === start ? R : L) : ((sy > 0) === start ? D : U);
|
|
93
|
+
return FROM_BITS[toward];
|
|
94
|
+
};
|
|
95
|
+
for (let i = 0; i <= MAX_COLUMNS + MAX_ROWS; i++) {
|
|
96
|
+
const end = x === xe && y === ye;
|
|
97
|
+
const begin = x === sx0 && y === sy0;
|
|
98
|
+
const piece = (begin && !end && half(x, y, true)) || (end && !begin && half(x, y, false)) || ch || auto;
|
|
99
|
+
this.set(x, y, piece);
|
|
100
|
+
if (end)
|
|
101
|
+
break;
|
|
102
|
+
const e2 = 2 * err;
|
|
103
|
+
if (e2 >= dy) {
|
|
104
|
+
err += dy;
|
|
105
|
+
x += sx;
|
|
106
|
+
}
|
|
107
|
+
if (e2 <= dx) {
|
|
108
|
+
err += dx;
|
|
109
|
+
y += sy;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/** Draw a box. Width and height include the border. */
|
|
115
|
+
rect(x, y, w, h, options = {}) {
|
|
116
|
+
const s = typeof options.style === 'object' ? options.style : (own(boxStyles, options.style ?? 'single') ?? boxStyles.single);
|
|
117
|
+
const x0 = Math.round(x), y0 = Math.round(y);
|
|
118
|
+
const x1 = x0 + Math.max(1, Math.round(w)) - 1, y1 = y0 + Math.max(1, Math.round(h)) - 1;
|
|
119
|
+
if (options.fill)
|
|
120
|
+
for (let yy = y0 + 1; yy < y1; yy++)
|
|
121
|
+
for (let xx = x0 + 1; xx < x1; xx++)
|
|
122
|
+
this.set(xx, yy, options.fill, false);
|
|
123
|
+
for (let xx = x0 + 1; xx < x1; xx++)
|
|
124
|
+
this.set(xx, y0, s.t).set(xx, y1, s.b);
|
|
125
|
+
for (let yy = y0 + 1; yy < y1; yy++)
|
|
126
|
+
this.set(x0, yy, s.l).set(x1, yy, s.r);
|
|
127
|
+
this.set(x0, y0, s.tl).set(x1, y0, s.tr).set(x0, y1, s.bl).set(x1, y1, s.br);
|
|
128
|
+
if (options.title && x1 - x0 > 3)
|
|
129
|
+
this.text(x0 + 2, y0, ` ${options.title} `.slice(0, x1 - x0 - 3));
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Draw an ellipse outline (or a filled one with `fill`). `ry` defaults to half of `rx`
|
|
134
|
+
* because character cells are about twice as tall as they are wide.
|
|
135
|
+
*/
|
|
136
|
+
circle(cx, cy, rx, options = {}) {
|
|
137
|
+
const ry = options.ry ?? rx / 2;
|
|
138
|
+
const ch = options.char ?? '*';
|
|
139
|
+
if (rx <= 0 || ry <= 0)
|
|
140
|
+
return this.set(cx, cy, ch);
|
|
141
|
+
for (let y = Math.floor(cy - ry); y <= Math.ceil(cy + ry); y++) {
|
|
142
|
+
for (let x = Math.floor(cx - rx); x <= Math.ceil(cx + rx); x++) {
|
|
143
|
+
const d = ((x - cx) / rx) ** 2 + ((y - cy) / ry) ** 2;
|
|
144
|
+
// thickness of the ring scales with how coarse the grid is
|
|
145
|
+
const edge = Math.abs(d - 1) <= Math.max(1 / rx, 1 / ry) * 1.2;
|
|
146
|
+
if (edge)
|
|
147
|
+
this.set(x, y, ch, false);
|
|
148
|
+
else if (options.fill && d < 1)
|
|
149
|
+
this.set(x, y, options.fill, false);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
/** Paste art at (x, y). Spaces are transparent unless `opaque` is set. */
|
|
155
|
+
draw(art, x, y, options = {}) {
|
|
156
|
+
toLines(art).forEach((line, i) => {
|
|
157
|
+
let col = Math.round(x);
|
|
158
|
+
for (const c of cells(line)) {
|
|
159
|
+
if (c !== '' && (options.opaque || c !== ' '))
|
|
160
|
+
this.set(col, Math.round(y) + i, c, false);
|
|
161
|
+
col++;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
/** Fill every cell with `ch`. */
|
|
167
|
+
clear(ch = ' ') {
|
|
168
|
+
for (const row of this.grid)
|
|
169
|
+
row.fill(ch);
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
/** The drawing as a string. Trailing spaces are removed unless `keepSpaces` is set. */
|
|
173
|
+
toString(options = {}) {
|
|
174
|
+
const rows = this.grid.map((r) => r.join(''));
|
|
175
|
+
return (options.keepSpaces ? rows : rows.map((r) => r.replace(/\s+$/, ''))).join('\n');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// braille canvas: each character is a 2×4 grid of dots, so you get 8× the resolution
|
|
180
|
+
const DOT = [
|
|
181
|
+
[0x01, 0x08],
|
|
182
|
+
[0x02, 0x10],
|
|
183
|
+
[0x04, 0x20],
|
|
184
|
+
[0x40, 0x80],
|
|
185
|
+
];
|
|
186
|
+
/**
|
|
187
|
+
* A pixel canvas drawn with braille characters (2×4 dots per character). Great for
|
|
188
|
+
* plots, curves and small drawings.
|
|
189
|
+
*
|
|
190
|
+
* ```ts
|
|
191
|
+
* const b = new BrailleCanvas(40, 10); // 80×40 dots
|
|
192
|
+
* b.circle(40, 20, 18).line(0, 39, 79, 0);
|
|
193
|
+
* b.toString();
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export class BrailleCanvas {
|
|
197
|
+
constructor(columns, rows) {
|
|
198
|
+
this.columns = Math.floor(clamp(columns, 1, MAX_COLUMNS, 40));
|
|
199
|
+
this.rows = Math.floor(clamp(rows, 1, MAX_ROWS, 10));
|
|
200
|
+
this.width = this.columns * 2;
|
|
201
|
+
this.height = this.rows * 4;
|
|
202
|
+
this.bits = new Uint8Array(this.columns * this.rows);
|
|
203
|
+
}
|
|
204
|
+
at(x, y) {
|
|
205
|
+
const px = Math.round(x), py = Math.round(y);
|
|
206
|
+
if (px < 0 || py < 0 || px >= this.width || py >= this.height)
|
|
207
|
+
return null;
|
|
208
|
+
return [(py >> 2) * this.columns + (px >> 1), DOT[py & 3][px & 1]];
|
|
209
|
+
}
|
|
210
|
+
set(x, y) {
|
|
211
|
+
const p = this.at(x, y);
|
|
212
|
+
if (p)
|
|
213
|
+
this.bits[p[0]] |= p[1];
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
unset(x, y) {
|
|
217
|
+
const p = this.at(x, y);
|
|
218
|
+
if (p)
|
|
219
|
+
this.bits[p[0]] &= ~p[1];
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
toggle(x, y) {
|
|
223
|
+
const p = this.at(x, y);
|
|
224
|
+
if (p)
|
|
225
|
+
this.bits[p[0]] ^= p[1];
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
get(x, y) {
|
|
229
|
+
const p = this.at(x, y);
|
|
230
|
+
return !!p && (this.bits[p[0]] & p[1]) !== 0;
|
|
231
|
+
}
|
|
232
|
+
line(x0, y0, x1, y1) {
|
|
233
|
+
let x = Math.round(x0), y = Math.round(y0);
|
|
234
|
+
const xe = Math.round(x1), ye = Math.round(y1);
|
|
235
|
+
const dx = Math.abs(xe - x), dy = -Math.abs(ye - y);
|
|
236
|
+
const sx = x < xe ? 1 : -1, sy = y < ye ? 1 : -1;
|
|
237
|
+
let err = dx + dy;
|
|
238
|
+
for (let i = 0; i <= (MAX_COLUMNS + MAX_ROWS) * 4; i++) {
|
|
239
|
+
this.set(x, y);
|
|
240
|
+
if (x === xe && y === ye)
|
|
241
|
+
break;
|
|
242
|
+
const e2 = 2 * err;
|
|
243
|
+
if (e2 >= dy) {
|
|
244
|
+
err += dy;
|
|
245
|
+
x += sx;
|
|
246
|
+
}
|
|
247
|
+
if (e2 <= dx) {
|
|
248
|
+
err += dx;
|
|
249
|
+
y += sy;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
rect(x, y, w, h, fill = false) {
|
|
255
|
+
const x1 = x + w - 1, y1 = y + h - 1;
|
|
256
|
+
if (fill) {
|
|
257
|
+
for (let yy = y; yy <= y1; yy++)
|
|
258
|
+
for (let xx = x; xx <= x1; xx++)
|
|
259
|
+
this.set(xx, yy);
|
|
260
|
+
return this;
|
|
261
|
+
}
|
|
262
|
+
return this.line(x, y, x1, y).line(x1, y, x1, y1).line(x1, y1, x, y1).line(x, y1, x, y);
|
|
263
|
+
}
|
|
264
|
+
/** Circle (dots are square-ish, so no aspect correction needed). `ry` makes an ellipse. */
|
|
265
|
+
circle(cx, cy, r, options = {}) {
|
|
266
|
+
const ry = options.ry ?? r;
|
|
267
|
+
const steps = Math.max(16, Math.ceil(Math.PI * 2 * Math.max(r, ry)));
|
|
268
|
+
if (options.fill) {
|
|
269
|
+
for (let y = -ry; y <= ry; y++) {
|
|
270
|
+
const span = r * Math.sqrt(Math.max(0, 1 - (y / ry) ** 2));
|
|
271
|
+
this.line(cx - span, cy + y, cx + span, cy + y);
|
|
272
|
+
}
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
let px = cx + r, py = cy;
|
|
276
|
+
for (let i = 1; i <= steps; i++) {
|
|
277
|
+
const t = (i / steps) * Math.PI * 2;
|
|
278
|
+
const nx = cx + r * Math.cos(t), ny = cy + ry * Math.sin(t);
|
|
279
|
+
this.line(px, py, nx, ny);
|
|
280
|
+
px = nx;
|
|
281
|
+
py = ny;
|
|
282
|
+
}
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
/** Plot y = fn(x) across the canvas. `fn` gets x in [xMin, xMax] and returns y in [yMin, yMax]. */
|
|
286
|
+
plot(fn, range = {}) {
|
|
287
|
+
const { xMin = 0, xMax = 1, yMin = 0, yMax = 1 } = range;
|
|
288
|
+
let prev = null;
|
|
289
|
+
for (let px = 0; px < this.width; px++) {
|
|
290
|
+
const y = fn(xMin + ((xMax - xMin) * px) / Math.max(1, this.width - 1));
|
|
291
|
+
if (!Number.isFinite(y)) {
|
|
292
|
+
prev = null;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const py = (this.height - 1) * (1 - (y - yMin) / (yMax - yMin || 1));
|
|
296
|
+
if (prev)
|
|
297
|
+
this.line(prev[0], prev[1], px, py);
|
|
298
|
+
else
|
|
299
|
+
this.set(px, py);
|
|
300
|
+
prev = [px, py];
|
|
301
|
+
}
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
clear() {
|
|
305
|
+
this.bits.fill(0);
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
/** Empty cells become spaces unless `blank` is set (U+2800 keeps width in some fonts). */
|
|
309
|
+
toString(options = {}) {
|
|
310
|
+
const out = [];
|
|
311
|
+
for (let r = 0; r < this.rows; r++) {
|
|
312
|
+
let line = '';
|
|
313
|
+
for (let c = 0; c < this.columns; c++) {
|
|
314
|
+
const b = this.bits[r * this.columns + c];
|
|
315
|
+
line += b || options.blank ? String.fromCharCode(0x2800 + b) : ' ';
|
|
316
|
+
}
|
|
317
|
+
out.push(options.blank ? line : line.replace(/\s+$/, ''));
|
|
318
|
+
}
|
|
319
|
+
return out.join('\n');
|
|
320
|
+
}
|
|
321
|
+
}
|
package/dist/charts.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
export interface SparklineOptions {
|
|
2
|
+
min?: number;
|
|
3
|
+
max?: number;
|
|
4
|
+
/** Ramp from low to high (default `▁▂▃▄▅▆▇█`). */
|
|
5
|
+
chars?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Tiny inline chart: `sparkline([1, 5, 2, 8])` → `▁▅▂█`. Missing values become spaces. */
|
|
8
|
+
export declare function sparkline(values: readonly (number | null | undefined)[], options?: SparklineOptions): string;
|
|
9
|
+
export type ChartData = Readonly<Record<string, number>> | readonly {
|
|
10
|
+
label: string;
|
|
11
|
+
value: number;
|
|
12
|
+
}[];
|
|
13
|
+
export interface BarChartOptions {
|
|
14
|
+
/** Bar length in columns for horizontal charts (default 40). */
|
|
15
|
+
width?: number;
|
|
16
|
+
/** Chart height in rows for vertical charts (default 10). */
|
|
17
|
+
height?: number;
|
|
18
|
+
/** `horizontal` (default) or `vertical`. */
|
|
19
|
+
direction?: 'horizontal' | 'vertical';
|
|
20
|
+
/** Bar character (default `█`). Smooth eighth-blocks are used only with `█`. */
|
|
21
|
+
char?: string;
|
|
22
|
+
/** Character for the empty part of the track (default none). */
|
|
23
|
+
track?: string;
|
|
24
|
+
/** Show the value next to each bar (default true). */
|
|
25
|
+
showValues?: boolean;
|
|
26
|
+
/** Scale maximum (default: largest value). */
|
|
27
|
+
max?: number;
|
|
28
|
+
/** Sort bars (default: keep order). */
|
|
29
|
+
sort?: 'asc' | 'desc';
|
|
30
|
+
/** Value formatter. */
|
|
31
|
+
format?: (value: number) => string;
|
|
32
|
+
/** Columns per bar in vertical charts (default 2). */
|
|
33
|
+
barWidth?: number;
|
|
34
|
+
/** Space between vertical bars (default 1). */
|
|
35
|
+
gap?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Bar chart from `{ label: value }` or `[{ label, value }]`.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* barChart({ react: 92, vue: 61, svelte: 40 }, { width: 30 })
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function barChart(data: ChartData, options?: BarChartOptions): string;
|
|
45
|
+
export declare const progressStyles: {
|
|
46
|
+
readonly smooth: {
|
|
47
|
+
readonly full: '█';
|
|
48
|
+
readonly empty: ' ';
|
|
49
|
+
readonly open: '│';
|
|
50
|
+
readonly close: '│';
|
|
51
|
+
};
|
|
52
|
+
readonly blocks: {
|
|
53
|
+
readonly full: '█';
|
|
54
|
+
readonly empty: '░';
|
|
55
|
+
readonly open: '';
|
|
56
|
+
readonly close: '';
|
|
57
|
+
};
|
|
58
|
+
readonly shade: {
|
|
59
|
+
readonly full: '▓';
|
|
60
|
+
readonly empty: '░';
|
|
61
|
+
readonly open: '';
|
|
62
|
+
readonly close: '';
|
|
63
|
+
};
|
|
64
|
+
readonly ascii: {
|
|
65
|
+
readonly full: '=';
|
|
66
|
+
readonly empty: ' ';
|
|
67
|
+
readonly open: '[';
|
|
68
|
+
readonly close: ']';
|
|
69
|
+
readonly head: '>';
|
|
70
|
+
};
|
|
71
|
+
readonly hash: {
|
|
72
|
+
readonly full: '#';
|
|
73
|
+
readonly empty: '.';
|
|
74
|
+
readonly open: '[';
|
|
75
|
+
readonly close: ']';
|
|
76
|
+
};
|
|
77
|
+
readonly dots: {
|
|
78
|
+
readonly full: '●';
|
|
79
|
+
readonly empty: '○';
|
|
80
|
+
readonly open: '';
|
|
81
|
+
readonly close: '';
|
|
82
|
+
};
|
|
83
|
+
readonly line: {
|
|
84
|
+
readonly full: '━';
|
|
85
|
+
readonly empty: '─';
|
|
86
|
+
readonly open: '';
|
|
87
|
+
readonly close: '';
|
|
88
|
+
readonly head: '╸';
|
|
89
|
+
};
|
|
90
|
+
readonly squares: {
|
|
91
|
+
readonly full: '■';
|
|
92
|
+
readonly empty: '□';
|
|
93
|
+
readonly open: '';
|
|
94
|
+
readonly close: '';
|
|
95
|
+
};
|
|
96
|
+
readonly arrows: {
|
|
97
|
+
readonly full: '>';
|
|
98
|
+
readonly empty: '-';
|
|
99
|
+
readonly open: '';
|
|
100
|
+
readonly close: '';
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
export type ProgressStyle = keyof typeof progressStyles;
|
|
104
|
+
export interface ProgressOptions {
|
|
105
|
+
/** Bar width in columns, without brackets and label (default 30). */
|
|
106
|
+
width?: number;
|
|
107
|
+
style?: ProgressStyle;
|
|
108
|
+
/** Show the percentage after the bar (default true). */
|
|
109
|
+
showPercent?: boolean;
|
|
110
|
+
/** Text before the bar. */
|
|
111
|
+
label?: string;
|
|
112
|
+
}
|
|
113
|
+
/** A progress bar for a value between 0 and 1: `███████▌ 45%`. */
|
|
114
|
+
export declare function progressBar(value: number, options?: ProgressOptions): string;
|
|
115
|
+
export interface LineChartOptions {
|
|
116
|
+
/** Plot width in characters, without the axis (default 60). */
|
|
117
|
+
width?: number;
|
|
118
|
+
/** Plot height in rows (default 10). */
|
|
119
|
+
height?: number;
|
|
120
|
+
min?: number;
|
|
121
|
+
max?: number;
|
|
122
|
+
/** Draw the y axis with labels (default true). */
|
|
123
|
+
axis?: boolean;
|
|
124
|
+
format?: (value: number) => string;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Line chart drawn with braille dots (2×4 per character). Pass one series or several.
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
* lineChart([3, 7, 4, 9, 12, 8, 15], { height: 6 })
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export declare function lineChart(series: readonly number[] | readonly (readonly number[])[], options?: LineChartOptions): string;
|
package/dist/charts.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { BrailleCanvas } from './canvas.js';
|
|
2
|
+
import { clamp, own } from './util.js';
|
|
3
|
+
import { stringWidth } from './width.js';
|
|
4
|
+
import { truncate } from './text.js';
|
|
5
|
+
const SPARK = '▁▂▃▄▅▆▇█';
|
|
6
|
+
// left-aligned eighth blocks, index = eighths filled
|
|
7
|
+
const EIGHTHS = ['', '▏', '▎', '▍', '▌', '▋', '▊', '▉'];
|
|
8
|
+
// bottom-aligned eighth blocks for vertical bars
|
|
9
|
+
const RISE = ['', '▁', '▂', '▃', '▄', '▅', '▆', '▇'];
|
|
10
|
+
const finite = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
11
|
+
// 7.5 not 7.50, 1234.6 not 1234.56
|
|
12
|
+
const defaultFormat = (v) => (Number.isInteger(v) ? String(v) : String(+v.toFixed(Math.abs(v) < 10 ? 2 : 1)));
|
|
13
|
+
/** Tiny inline chart: `sparkline([1, 5, 2, 8])` → `▁▅▂█`. Missing values become spaces. */
|
|
14
|
+
export function sparkline(values, options = {}) {
|
|
15
|
+
const ramp = [...(options.chars || SPARK)];
|
|
16
|
+
const nums = values.filter(finite);
|
|
17
|
+
if (!nums.length)
|
|
18
|
+
return ' '.repeat(values.length);
|
|
19
|
+
const min = options.min ?? Math.min(...nums);
|
|
20
|
+
const max = options.max ?? Math.max(...nums);
|
|
21
|
+
const span = max - min;
|
|
22
|
+
return values
|
|
23
|
+
.map((v) => {
|
|
24
|
+
if (!finite(v))
|
|
25
|
+
return ' ';
|
|
26
|
+
const t = span ? clamp((v - min) / span, 0, 1, 0) : 0.5;
|
|
27
|
+
return ramp[Math.round(t * (ramp.length - 1))];
|
|
28
|
+
})
|
|
29
|
+
.join('');
|
|
30
|
+
}
|
|
31
|
+
const entries = (data) => (Array.isArray(data) ? data.map((d) => [d.label, d.value]) : Object.entries(data))
|
|
32
|
+
.map(([l, v]) => [String(l), finite(v) ? v : 0]);
|
|
33
|
+
/**
|
|
34
|
+
* Bar chart from `{ label: value }` or `[{ label, value }]`.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* barChart({ react: 92, vue: 61, svelte: 40 }, { width: 30 })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function barChart(data, options = {}) {
|
|
41
|
+
let rows = entries(data);
|
|
42
|
+
if (options.sort)
|
|
43
|
+
rows = [...rows].sort((a, b) => (options.sort === 'asc' ? a[1] - b[1] : b[1] - a[1]));
|
|
44
|
+
if (!rows.length)
|
|
45
|
+
return '';
|
|
46
|
+
const fmt = options.format ?? defaultFormat;
|
|
47
|
+
const max = options.max ?? Math.max(0, ...rows.map((r) => r[1]));
|
|
48
|
+
const ch = options.char || '█';
|
|
49
|
+
const smooth = ch === '█';
|
|
50
|
+
const show = options.showValues ?? true;
|
|
51
|
+
if (options.direction === 'vertical') {
|
|
52
|
+
const height = Math.floor(clamp(options.height, 1, 200, 10));
|
|
53
|
+
const bw = Math.floor(clamp(options.barWidth, 1, 20, 2));
|
|
54
|
+
const gap = Math.floor(clamp(options.gap, 0, 20, 1));
|
|
55
|
+
const slot = Math.max(bw, ...(show ? rows.map((r) => stringWidth(fmt(r[1]))) : [0]));
|
|
56
|
+
const cols = rows.map(([, v]) => Math.max(0, max ? (v / max) * height : 0));
|
|
57
|
+
const lines = [];
|
|
58
|
+
if (show)
|
|
59
|
+
lines.push(rows.map(([, v], i) => center(fmt(v), slot) + (i < rows.length - 1 ? ' '.repeat(gap) : '')).join(''));
|
|
60
|
+
for (let r = height - 1; r >= 0; r--) {
|
|
61
|
+
lines.push(cols
|
|
62
|
+
.map((h, i) => {
|
|
63
|
+
const fill = h - r;
|
|
64
|
+
const cell = fill >= 1 ? ch : fill > 0 && smooth ? RISE[Math.round(fill * 8)] || ' ' : options.track ?? ' ';
|
|
65
|
+
return center(cell.repeat(bw), slot) + (i < cols.length - 1 ? ' '.repeat(gap) : '');
|
|
66
|
+
})
|
|
67
|
+
.join(''));
|
|
68
|
+
}
|
|
69
|
+
lines.push(rows.map(([l], i) => center(truncate(l, slot), slot) + (i < rows.length - 1 ? ' '.repeat(gap) : '')).join(''));
|
|
70
|
+
return lines.map((l) => l.trimEnd()).join('\n');
|
|
71
|
+
}
|
|
72
|
+
const width = Math.floor(clamp(options.width, 1, 1000, 40));
|
|
73
|
+
const labelW = Math.max(...rows.map((r) => stringWidth(r[0])));
|
|
74
|
+
return rows
|
|
75
|
+
.map(([label, v]) => {
|
|
76
|
+
const len = max ? clamp((v / max) * width, 0, width, 0) : 0;
|
|
77
|
+
const full = Math.floor(len);
|
|
78
|
+
const part = smooth ? EIGHTHS[Math.round((len - full) * 8)] ?? '' : '';
|
|
79
|
+
let bar = ch.repeat(full) + part;
|
|
80
|
+
const used = full + (part ? 1 : 0);
|
|
81
|
+
if (options.track)
|
|
82
|
+
bar += options.track.repeat(Math.max(0, width - used));
|
|
83
|
+
const value = show ? ` ${fmt(v)}` : '';
|
|
84
|
+
return (label + ' '.repeat(labelW - stringWidth(label)) + ' │' + bar + value).trimEnd();
|
|
85
|
+
})
|
|
86
|
+
.join('\n');
|
|
87
|
+
}
|
|
88
|
+
function center(s, width) {
|
|
89
|
+
const free = Math.max(0, width - stringWidth(s));
|
|
90
|
+
return ' '.repeat(Math.floor(free / 2)) + s + ' '.repeat(free - Math.floor(free / 2));
|
|
91
|
+
}
|
|
92
|
+
export const progressStyles = {
|
|
93
|
+
smooth: { full: '█', empty: ' ', open: '│', close: '│' },
|
|
94
|
+
blocks: { full: '█', empty: '░', open: '', close: '' },
|
|
95
|
+
shade: { full: '▓', empty: '░', open: '', close: '' },
|
|
96
|
+
ascii: { full: '=', empty: ' ', open: '[', close: ']', head: '>' },
|
|
97
|
+
hash: { full: '#', empty: '.', open: '[', close: ']' },
|
|
98
|
+
dots: { full: '●', empty: '○', open: '', close: '' },
|
|
99
|
+
line: { full: '━', empty: '─', open: '', close: '', head: '╸' },
|
|
100
|
+
squares: { full: '■', empty: '□', open: '', close: '' },
|
|
101
|
+
arrows: { full: '>', empty: '-', open: '', close: '' },
|
|
102
|
+
};
|
|
103
|
+
/** A progress bar for a value between 0 and 1: `███████▌ 45%`. */
|
|
104
|
+
export function progressBar(value, options = {}) {
|
|
105
|
+
const st = own(progressStyles, options.style ?? 'smooth') ?? progressStyles.smooth;
|
|
106
|
+
const width = Math.floor(clamp(options.width, 1, 1000, 30));
|
|
107
|
+
const v = clamp(value, 0, 1, 0);
|
|
108
|
+
const len = v * width;
|
|
109
|
+
let full = Math.floor(len);
|
|
110
|
+
let bar = st.full.repeat(full);
|
|
111
|
+
if (options.style === undefined || options.style === 'smooth') {
|
|
112
|
+
const part = EIGHTHS[Math.round((len - full) * 8)] ?? '';
|
|
113
|
+
if (part && full < width) {
|
|
114
|
+
bar += part;
|
|
115
|
+
full++;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if ('head' in st && full < width && v > 0) {
|
|
119
|
+
bar += st.head;
|
|
120
|
+
full++;
|
|
121
|
+
}
|
|
122
|
+
bar += st.empty.repeat(Math.max(0, width - full));
|
|
123
|
+
const pct = options.showPercent ?? true ? ` ${String(Math.round(v * 100)).padStart(3)}%` : '';
|
|
124
|
+
return `${options.label ? options.label + ' ' : ''}${st.open}${bar}${st.close}${pct}`;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Line chart drawn with braille dots (2×4 per character). Pass one series or several.
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
* lineChart([3, 7, 4, 9, 12, 8, 15], { height: 6 })
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export function lineChart(series, options = {}) {
|
|
134
|
+
const list = (Array.isArray(series[0]) ? series : [series]);
|
|
135
|
+
const all = list.flat().filter(finite);
|
|
136
|
+
if (!all.length)
|
|
137
|
+
return '';
|
|
138
|
+
const width = Math.floor(clamp(options.width, 2, 500, 60));
|
|
139
|
+
const height = Math.floor(clamp(options.height, 1, 200, 10));
|
|
140
|
+
let min = options.min ?? Math.min(...all);
|
|
141
|
+
let max = options.max ?? Math.max(...all);
|
|
142
|
+
if (min === max) {
|
|
143
|
+
min -= 1;
|
|
144
|
+
max += 1;
|
|
145
|
+
}
|
|
146
|
+
const canvas = new BrailleCanvas(width, height);
|
|
147
|
+
const py = (v) => (canvas.height - 1) * (1 - (v - min) / (max - min));
|
|
148
|
+
for (const s of list) {
|
|
149
|
+
const n = s.length;
|
|
150
|
+
let prev = null;
|
|
151
|
+
s.forEach((v, i) => {
|
|
152
|
+
if (!finite(v)) {
|
|
153
|
+
prev = null;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const x = n > 1 ? (i / (n - 1)) * (canvas.width - 1) : 0;
|
|
157
|
+
const y = py(clamp(v, min, max, min));
|
|
158
|
+
if (prev)
|
|
159
|
+
canvas.line(prev[0], prev[1], x, y);
|
|
160
|
+
else
|
|
161
|
+
canvas.set(x, y);
|
|
162
|
+
prev = [x, y];
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const plot = canvas.toString({ blank: false }).split('\n');
|
|
166
|
+
if (options.axis === false)
|
|
167
|
+
return plot.map((l) => l.trimEnd()).join('\n');
|
|
168
|
+
const fmt = options.format ?? defaultFormat;
|
|
169
|
+
const top = fmt(max), mid = fmt((max + min) / 2), bottom = fmt(min);
|
|
170
|
+
const lw = Math.max(stringWidth(top), stringWidth(mid), stringWidth(bottom));
|
|
171
|
+
const midRow = Math.floor((height - 1) / 2);
|
|
172
|
+
const rows = plot.map((line, r) => {
|
|
173
|
+
const label = r === 0 ? top : r === height - 1 ? bottom : r === midRow && height > 2 ? mid : '';
|
|
174
|
+
return (label.padStart(lw) + (label ? ' ┤' : ' │') + line).trimEnd();
|
|
175
|
+
});
|
|
176
|
+
rows.push(' '.repeat(lw) + ' └' + '─'.repeat(width));
|
|
177
|
+
return rows.join('\n');
|
|
178
|
+
}
|