@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.
- package/LICENSE +24 -24
- package/README.md +106 -2
- package/dist/animate.d.ts +1 -1
- package/dist/animate.js +44 -6
- 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.d.ts +2 -0
- package/dist/cli.js +159 -0
- package/dist/color.js +4 -2
- package/dist/effects.js +30 -14
- package/dist/export.d.ts +37 -0
- package/dist/export.js +112 -0
- package/dist/fonts/lazy.js +1 -1
- package/dist/image.d.ts +3 -0
- package/dist/image.js +20 -7
- package/dist/index.d.ts +7 -1
- package/dist/index.js +7 -1
- package/dist/layout.d.ts +307 -0
- package/dist/layout.js +194 -0
- package/dist/react/Ascii.d.ts +1 -1
- package/dist/react/AsciiAnimator.js +25 -0
- 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/style.js +2 -1
- package/dist/react/widgets.d.ts +56 -0
- package/dist/react/widgets.js +52 -0
- package/dist/render.js +20 -6
- package/dist/text.d.ts +26 -0
- package/dist/text.js +230 -0
- package/dist/util.d.ts +14 -0
- package/dist/util.js +32 -0
- package/dist/width.d.ts +22 -0
- package/dist/width.js +111 -0
- package/package.json +87 -80
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { barChart, lineChart, progressBar, sparkline } from '../charts.js';
|
|
3
|
+
import { table, tree } from '../layout.js';
|
|
4
|
+
import { asciiStyle } from './style.js';
|
|
5
|
+
const STYLE_KEYS = ['color', 'gradient', 'gradientDirection', 'size', 'lineHeight', 'glow', 'fontFamily'];
|
|
6
|
+
// pulls the styling props out so the rest can go straight to the DOM
|
|
7
|
+
function Pre({ art, props, label, lineHeight = 1.2 }) {
|
|
8
|
+
const { as: Tag = 'pre', style, ...rest } = props;
|
|
9
|
+
const styleProps = {};
|
|
10
|
+
const dom = {};
|
|
11
|
+
for (const [k, v] of Object.entries(rest)) {
|
|
12
|
+
if (STYLE_KEYS.includes(k))
|
|
13
|
+
styleProps[k] = v;
|
|
14
|
+
else
|
|
15
|
+
dom[k] = v;
|
|
16
|
+
}
|
|
17
|
+
return (_jsx(Tag, { role: label ? 'img' : undefined, "aria-label": label, style: asciiStyle({ lineHeight, ...styleProps }, style), ...dom, children: art }));
|
|
18
|
+
}
|
|
19
|
+
/** Show any art string with asciikit styling (gradient, glow, …). Server-component safe. */
|
|
20
|
+
export function AsciiArt({ art, label, ...props }) {
|
|
21
|
+
return _jsx(Pre, { art: art, props: props, label: label, lineHeight: typeof props.lineHeight === 'number' ? props.lineHeight : 1 });
|
|
22
|
+
}
|
|
23
|
+
/** Text-mode table. Server-component safe. */
|
|
24
|
+
export function AsciiTable({ rows, head, variant, align, padding, maxWidth, truncate, rowSeparators, title, ...props }) {
|
|
25
|
+
const art = table(rows, { head, style: variant, align, padding, maxWidth, truncate, rowSeparators, title });
|
|
26
|
+
return _jsx(Pre, { art: art, props: props });
|
|
27
|
+
}
|
|
28
|
+
/** Tree diagram like the `tree` command. Server-component safe. */
|
|
29
|
+
export function AsciiTree({ data, variant, ...props }) {
|
|
30
|
+
return _jsx(Pre, { art: tree(data, { style: variant }), props: props });
|
|
31
|
+
}
|
|
32
|
+
/** Bar, line (braille) or sparkline chart. Server-component safe. */
|
|
33
|
+
export function AsciiChart(props) {
|
|
34
|
+
const { type, data, label, ...rest } = props;
|
|
35
|
+
const opts = {};
|
|
36
|
+
const dom = {};
|
|
37
|
+
const CHART_KEYS = ['width', 'height', 'direction', 'char', 'track', 'showValues', 'max', 'min', 'sort', 'format', 'barWidth', 'gap', 'axis'];
|
|
38
|
+
for (const [k, v] of Object.entries(rest))
|
|
39
|
+
(CHART_KEYS.includes(k) ? opts : dom)[k] = v;
|
|
40
|
+
const art = type === 'bar'
|
|
41
|
+
? barChart(data, opts)
|
|
42
|
+
: type === 'line'
|
|
43
|
+
? lineChart(data, opts)
|
|
44
|
+
: sparkline(data, opts);
|
|
45
|
+
return _jsx(Pre, { art: art, props: dom, label: label, lineHeight: type === 'line' ? 1 : 1.2 });
|
|
46
|
+
}
|
|
47
|
+
/** Text progress bar with progressbar semantics. Server-component safe. */
|
|
48
|
+
export function AsciiProgress({ value, width, variant, text, showPercent, ...props }) {
|
|
49
|
+
const v = Math.max(0, Math.min(1, Number.isFinite(value) ? value : 0));
|
|
50
|
+
const aria = { role: 'progressbar', 'aria-valuemin': 0, 'aria-valuemax': 100, 'aria-valuenow': Math.round(v * 100) };
|
|
51
|
+
return _jsx(Pre, { art: progressBar(v, { width, style: variant, showPercent, label: text }), props: { as: 'span', ...aria, ...props } });
|
|
52
|
+
}
|
package/dist/render.js
CHANGED
|
@@ -110,16 +110,19 @@ function smushChars(l, r, c) {
|
|
|
110
110
|
}
|
|
111
111
|
return null;
|
|
112
112
|
}
|
|
113
|
-
|
|
113
|
+
/**
|
|
114
|
+
* How far the next glyph can slide left. `lastNonSpace[row]` is maintained by the caller,
|
|
115
|
+
* so this is O(glyph width) instead of rescanning the whole output row (which made long
|
|
116
|
+
* lines quadratic).
|
|
117
|
+
*/
|
|
118
|
+
function smushAmount(out, glyph, c, lastNonSpace) {
|
|
114
119
|
if ((c.mode & (H_SMUSH | H_KERN)) === 0)
|
|
115
120
|
return 0;
|
|
116
121
|
let max = Math.min(c.curWidth, out[0].length);
|
|
117
122
|
for (let row = 0; row < glyph.length; row++) {
|
|
118
123
|
const line = out[row];
|
|
119
124
|
const g = glyph[row];
|
|
120
|
-
|
|
121
|
-
while (lb > 0 && (lb >= line.length || line[lb] === ' '))
|
|
122
|
-
lb--;
|
|
125
|
+
const lb = Math.max(0, lastNonSpace[row] ?? -1);
|
|
123
126
|
let cb = 0;
|
|
124
127
|
while (cb < g.length && g[cb] === ' ')
|
|
125
128
|
cb++;
|
|
@@ -164,6 +167,8 @@ function renderSingleLine(text, ctx) {
|
|
|
164
167
|
const out = Array.from({ length: height }, () => []);
|
|
165
168
|
const spacing = Math.max(0, Math.floor(opts.letterSpacing ?? 0));
|
|
166
169
|
const c = { mode: ctx.mode, hardblank: font.hardblank, rtl: ctx.rtl, prevWidth: 0, curWidth: 0 };
|
|
170
|
+
// Index of the last non-space character in each output row (-1 = none yet).
|
|
171
|
+
const lastNonSpace = Array.from({ length: height }, () => -1);
|
|
167
172
|
for (const cp of cps) {
|
|
168
173
|
let glyph;
|
|
169
174
|
if (cp === 32 && opts.wordSpacing !== undefined) {
|
|
@@ -179,12 +184,14 @@ function renderSingleLine(text, ctx) {
|
|
|
179
184
|
let amt = 0;
|
|
180
185
|
if (out[0].length > 0) {
|
|
181
186
|
if (spacing > 0) {
|
|
182
|
-
|
|
187
|
+
out.forEach((row, r) => {
|
|
183
188
|
for (let i = 0; i < spacing; i++)
|
|
184
189
|
row.push(font.hardblank);
|
|
190
|
+
lastNonSpace[r] = row.length - 1;
|
|
191
|
+
});
|
|
185
192
|
}
|
|
186
193
|
else {
|
|
187
|
-
amt = smushAmount(out, glyph, c);
|
|
194
|
+
amt = smushAmount(out, glyph, c, lastNonSpace);
|
|
188
195
|
}
|
|
189
196
|
}
|
|
190
197
|
for (let r = 0; r < height; r++) {
|
|
@@ -198,6 +205,13 @@ function renderSingleLine(text, ctx) {
|
|
|
198
205
|
}
|
|
199
206
|
for (let k = amt; k < g.length; k++)
|
|
200
207
|
row.push(g[k]);
|
|
208
|
+
// Smushing never turns a visible char into a space, so only the touched tail can move the marker.
|
|
209
|
+
for (let k = row.length - 1; k >= Math.max(0, len - amt) && k > lastNonSpace[r]; k--) {
|
|
210
|
+
if (row[k] !== ' ') {
|
|
211
|
+
lastNonSpace[r] = k;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
201
215
|
}
|
|
202
216
|
c.prevWidth = width;
|
|
203
217
|
}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface WrapOptions {
|
|
2
|
+
/** Break words longer than the width (default true). Otherwise they overflow. */
|
|
3
|
+
hard?: boolean;
|
|
4
|
+
/** Keep leading/trailing spaces of each line (default false). */
|
|
5
|
+
keepSpaces?: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** Word-wrap plain text to `width` columns. Emoji and CJK count as two columns. */
|
|
8
|
+
export declare function wrapText(text: string, width: number, options?: WrapOptions): string;
|
|
9
|
+
/** Cut text to at most `width` columns, ending with `ellipsis` when it was cut. */
|
|
10
|
+
export declare function truncate(text: string, width: number, ellipsis?: string): string;
|
|
11
|
+
declare const COMBINING: {
|
|
12
|
+
readonly strike: '̶';
|
|
13
|
+
readonly underline: '̲';
|
|
14
|
+
readonly doubleUnderline: '̳';
|
|
15
|
+
readonly overline: '̅';
|
|
16
|
+
readonly slash: '̸';
|
|
17
|
+
};
|
|
18
|
+
export declare const fancyStyles: readonly FancyStyle[];
|
|
19
|
+
export type FancyStyle = 'bold' | 'italic' | 'boldItalic' | 'script' | 'boldScript' | 'fraktur' | 'boldFraktur' | 'doubleStruck' | 'sans' | 'sansBold' | 'sansItalic' | 'sansBoldItalic' | 'monospace' | 'fullwidth' | 'circled' | 'circledNegative' | 'squared' | 'squaredNegative' | 'parenthesized' | 'smallCaps' | 'upsideDown' | keyof typeof COMBINING;
|
|
20
|
+
/**
|
|
21
|
+
* Restyle text with Unicode look-alike characters (𝐛𝐨𝐥𝐝, 𝔣𝔯𝔞𝔨𝔱𝔲𝔯, ⓒⓘⓡⓒⓛⓔⓓ, ꜱᴍᴀʟʟ ᴄᴀᴘꜱ, uʍop ǝpısdn…).
|
|
22
|
+
* Works anywhere plain text does: bios, titles, terminals. Pass several styles to combine
|
|
23
|
+
* a letter style with marks, e.g. `fancy('hi', ['bold', 'underline'])`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function fancy(text: string, style: FancyStyle | readonly FancyStyle[]): string;
|
|
26
|
+
export {};
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { own } from './util.js';
|
|
2
|
+
import { cells, charWidth, graphemes, stringWidth } from './width.js';
|
|
3
|
+
/** Word-wrap plain text to `width` columns. Emoji and CJK count as two columns. */
|
|
4
|
+
export function wrapText(text, width, options = {}) {
|
|
5
|
+
const max = Math.max(1, Math.floor(width) || 1);
|
|
6
|
+
const hard = options.hard ?? true;
|
|
7
|
+
const out = [];
|
|
8
|
+
for (const para of String(text).split('\n')) {
|
|
9
|
+
const words = para.split(/(\s+)/).filter((w) => w !== '');
|
|
10
|
+
let line = '';
|
|
11
|
+
let lineW = 0;
|
|
12
|
+
const flush = () => {
|
|
13
|
+
out.push(options.keepSpaces ? line : line.trim());
|
|
14
|
+
line = '';
|
|
15
|
+
lineW = 0;
|
|
16
|
+
};
|
|
17
|
+
for (const word of words) {
|
|
18
|
+
const w = stringWidth(word);
|
|
19
|
+
if (/^\s+$/.test(word)) {
|
|
20
|
+
if (lineW && lineW + w <= max) {
|
|
21
|
+
line += word;
|
|
22
|
+
lineW += w;
|
|
23
|
+
}
|
|
24
|
+
else if (lineW)
|
|
25
|
+
flush();
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (lineW + w <= max) {
|
|
29
|
+
line += word;
|
|
30
|
+
lineW += w;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (lineW && line.trim())
|
|
34
|
+
flush();
|
|
35
|
+
else {
|
|
36
|
+
line = '';
|
|
37
|
+
lineW = 0;
|
|
38
|
+
}
|
|
39
|
+
if (w <= max || !hard) {
|
|
40
|
+
line = word;
|
|
41
|
+
lineW = w;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// word is longer than a whole line: chop it into column-sized pieces
|
|
45
|
+
let piece = '';
|
|
46
|
+
let pieceW = 0;
|
|
47
|
+
for (const g of graphemes(word)) {
|
|
48
|
+
const gw = charWidth(g);
|
|
49
|
+
if (pieceW + gw > max && piece) {
|
|
50
|
+
out.push(piece);
|
|
51
|
+
piece = '';
|
|
52
|
+
pieceW = 0;
|
|
53
|
+
}
|
|
54
|
+
piece += g;
|
|
55
|
+
pieceW += gw;
|
|
56
|
+
}
|
|
57
|
+
line = piece;
|
|
58
|
+
lineW = pieceW;
|
|
59
|
+
}
|
|
60
|
+
flush();
|
|
61
|
+
}
|
|
62
|
+
return out.join('\n');
|
|
63
|
+
}
|
|
64
|
+
/** Cut text to at most `width` columns, ending with `ellipsis` when it was cut. */
|
|
65
|
+
export function truncate(text, width, ellipsis = '…') {
|
|
66
|
+
const max = Math.max(0, Math.floor(width) || 0);
|
|
67
|
+
if (stringWidth(text) <= max)
|
|
68
|
+
return text;
|
|
69
|
+
const room = max - stringWidth(ellipsis);
|
|
70
|
+
if (room <= 0)
|
|
71
|
+
return cells(ellipsis).slice(0, max).join('');
|
|
72
|
+
let out = '';
|
|
73
|
+
let w = 0;
|
|
74
|
+
for (const g of graphemes(text)) {
|
|
75
|
+
const gw = charWidth(g);
|
|
76
|
+
if (w + gw > room)
|
|
77
|
+
break;
|
|
78
|
+
out += g;
|
|
79
|
+
w += gw;
|
|
80
|
+
}
|
|
81
|
+
return out + ellipsis;
|
|
82
|
+
}
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
// fancy unicode text styles
|
|
85
|
+
const A = 65;
|
|
86
|
+
const a = 97;
|
|
87
|
+
const ZERO = 48;
|
|
88
|
+
const from = (cp) => String.fromCodePoint(cp);
|
|
89
|
+
// note: the math alphanumeric block has holes where letters already existed (ℎ, ℬ, ℭ…)
|
|
90
|
+
const MATH = {
|
|
91
|
+
bold: { upper: 0x1d400, lower: 0x1d41a, digits: 0x1d7ce },
|
|
92
|
+
italic: { upper: 0x1d434, lower: 0x1d44e, holes: { h: 0x210e } },
|
|
93
|
+
boldItalic: { upper: 0x1d468, lower: 0x1d482 },
|
|
94
|
+
script: {
|
|
95
|
+
upper: 0x1d49c,
|
|
96
|
+
lower: 0x1d4b6,
|
|
97
|
+
holes: { B: 0x212c, E: 0x2130, F: 0x2131, H: 0x210b, I: 0x2110, L: 0x2112, M: 0x2133, R: 0x211b, e: 0x212f, g: 0x210a, o: 0x2134 },
|
|
98
|
+
},
|
|
99
|
+
boldScript: { upper: 0x1d4d0, lower: 0x1d4ea },
|
|
100
|
+
fraktur: { upper: 0x1d504, lower: 0x1d51e, holes: { C: 0x212d, H: 0x210c, I: 0x2111, R: 0x211c, Z: 0x2128 } },
|
|
101
|
+
boldFraktur: { upper: 0x1d56c, lower: 0x1d586 },
|
|
102
|
+
doubleStruck: {
|
|
103
|
+
upper: 0x1d538,
|
|
104
|
+
lower: 0x1d552,
|
|
105
|
+
digits: 0x1d7d8,
|
|
106
|
+
holes: { C: 0x2102, H: 0x210d, N: 0x2115, P: 0x2119, Q: 0x211a, R: 0x211d, Z: 0x2124 },
|
|
107
|
+
},
|
|
108
|
+
sans: { upper: 0x1d5a0, lower: 0x1d5ba, digits: 0x1d7e2 },
|
|
109
|
+
sansBold: { upper: 0x1d5d4, lower: 0x1d5ee, digits: 0x1d7ec },
|
|
110
|
+
sansItalic: { upper: 0x1d608, lower: 0x1d622 },
|
|
111
|
+
sansBoldItalic: { upper: 0x1d63c, lower: 0x1d656 },
|
|
112
|
+
monospace: { upper: 0x1d670, lower: 0x1d68a, digits: 0x1d7f6 },
|
|
113
|
+
};
|
|
114
|
+
const SMALL_CAPS = 'ᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘǫʀꜱᴛᴜᴠᴡxʏᴢ';
|
|
115
|
+
const UPSIDE_LO = 'ɐqɔpǝɟƃɥᴉɾʞlɯuodbɹsʇnʌʍxʎz';
|
|
116
|
+
const UPSIDE_UP = '∀ᗺƆᗡƎℲ⅁HIſꞰ˥WNOԀῸᴚS⊥∩ΛMX⅄Z';
|
|
117
|
+
const UPSIDE_DIGITS = '0ƖᘔƐᔭϛ9L86';
|
|
118
|
+
const UPSIDE_PUNCT = {
|
|
119
|
+
'.': '˙', ',': "'", "'": ',', '"': '„', '?': '¿', '!': '¡', '(': ')', ')': '(', '[': ']', ']': '[',
|
|
120
|
+
'{': '}', '}': '{', '<': '>', '>': '<', _: '‾', '&': '⅋', ';': '؛',
|
|
121
|
+
};
|
|
122
|
+
const COMBINING = {
|
|
123
|
+
strike: '̶',
|
|
124
|
+
underline: '̲',
|
|
125
|
+
doubleUnderline: '̳',
|
|
126
|
+
overline: '̅',
|
|
127
|
+
slash: '̸',
|
|
128
|
+
};
|
|
129
|
+
export const fancyStyles = [
|
|
130
|
+
...Object.keys(MATH),
|
|
131
|
+
'fullwidth',
|
|
132
|
+
'circled',
|
|
133
|
+
'circledNegative',
|
|
134
|
+
'squared',
|
|
135
|
+
'squaredNegative',
|
|
136
|
+
'parenthesized',
|
|
137
|
+
'smallCaps',
|
|
138
|
+
'upsideDown',
|
|
139
|
+
...Object.keys(COMBINING),
|
|
140
|
+
];
|
|
141
|
+
function mapChar(ch, style) {
|
|
142
|
+
const cp = ch.codePointAt(0);
|
|
143
|
+
const up = ch >= 'A' && ch <= 'Z';
|
|
144
|
+
const lo = ch >= 'a' && ch <= 'z';
|
|
145
|
+
const dig = ch >= '0' && ch <= '9';
|
|
146
|
+
const math = own(MATH, style);
|
|
147
|
+
if (math) {
|
|
148
|
+
const hole = math.holes && own(math.holes, ch);
|
|
149
|
+
if (hole)
|
|
150
|
+
return from(hole);
|
|
151
|
+
if (up)
|
|
152
|
+
return from(math.upper + cp - A);
|
|
153
|
+
if (lo && math.lower)
|
|
154
|
+
return from(math.lower + cp - a);
|
|
155
|
+
if (dig && math.digits)
|
|
156
|
+
return from(math.digits + cp - ZERO);
|
|
157
|
+
return ch;
|
|
158
|
+
}
|
|
159
|
+
switch (style) {
|
|
160
|
+
case 'fullwidth':
|
|
161
|
+
if (ch === ' ')
|
|
162
|
+
return ' ';
|
|
163
|
+
return cp > 0x20 && cp < 0x7f ? from(cp + 0xfee0) : ch;
|
|
164
|
+
case 'circled':
|
|
165
|
+
if (up)
|
|
166
|
+
return from(0x24b6 + cp - A);
|
|
167
|
+
if (lo)
|
|
168
|
+
return from(0x24d0 + cp - a);
|
|
169
|
+
if (ch === '0')
|
|
170
|
+
return '⓪';
|
|
171
|
+
if (dig)
|
|
172
|
+
return from(0x2460 + cp - ZERO - 1);
|
|
173
|
+
return ch;
|
|
174
|
+
case 'circledNegative':
|
|
175
|
+
if (up || lo)
|
|
176
|
+
return from(0x1f150 + ch.toUpperCase().charCodeAt(0) - A);
|
|
177
|
+
if (ch === '0')
|
|
178
|
+
return '⓿';
|
|
179
|
+
if (dig)
|
|
180
|
+
return from(0x2776 + cp - ZERO - 1);
|
|
181
|
+
return ch;
|
|
182
|
+
case 'squared':
|
|
183
|
+
return up || lo ? from(0x1f130 + ch.toUpperCase().charCodeAt(0) - A) : ch;
|
|
184
|
+
case 'squaredNegative':
|
|
185
|
+
return up || lo ? from(0x1f170 + ch.toUpperCase().charCodeAt(0) - A) : ch;
|
|
186
|
+
case 'parenthesized':
|
|
187
|
+
if (up || lo)
|
|
188
|
+
return from(0x249c + ch.toLowerCase().charCodeAt(0) - a);
|
|
189
|
+
if (dig && ch !== '0')
|
|
190
|
+
return from(0x2474 + cp - ZERO - 1);
|
|
191
|
+
return ch;
|
|
192
|
+
case 'smallCaps':
|
|
193
|
+
return up || lo ? SMALL_CAPS[ch.toLowerCase().charCodeAt(0) - a] : ch;
|
|
194
|
+
case 'upsideDown':
|
|
195
|
+
if (lo)
|
|
196
|
+
return [...UPSIDE_LO][cp - a];
|
|
197
|
+
if (up)
|
|
198
|
+
return [...UPSIDE_UP][cp - A];
|
|
199
|
+
if (dig)
|
|
200
|
+
return [...UPSIDE_DIGITS][cp - ZERO];
|
|
201
|
+
return own(UPSIDE_PUNCT, ch) ?? ch;
|
|
202
|
+
default: {
|
|
203
|
+
const mark = own(COMBINING, style);
|
|
204
|
+
return mark && ch !== ' ' && ch !== '\n' ? ch + mark : ch;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Restyle text with Unicode look-alike characters (𝐛𝐨𝐥𝐝, 𝔣𝔯𝔞𝔨𝔱𝔲𝔯, ⓒⓘⓡⓒⓛⓔⓓ, ꜱᴍᴀʟʟ ᴄᴀᴘꜱ, uʍop ǝpısdn…).
|
|
210
|
+
* Works anywhere plain text does: bios, titles, terminals. Pass several styles to combine
|
|
211
|
+
* a letter style with marks, e.g. `fancy('hi', ['bold', 'underline'])`.
|
|
212
|
+
*/
|
|
213
|
+
export function fancy(text, style) {
|
|
214
|
+
const styles = typeof style === 'string' ? [style] : style;
|
|
215
|
+
let out = String(text);
|
|
216
|
+
for (const s of styles) {
|
|
217
|
+
out = out
|
|
218
|
+
.split('\n')
|
|
219
|
+
.map((line) => {
|
|
220
|
+
// map the base char only, so accents and other marks stay attached
|
|
221
|
+
const mapped = graphemes(line).map((g) => {
|
|
222
|
+
const base = String.fromCodePoint(g.codePointAt(0));
|
|
223
|
+
return mapChar(base, s) + g.slice(base.length);
|
|
224
|
+
});
|
|
225
|
+
return (s === 'upsideDown' ? mapped.reverse() : mapped).join('');
|
|
226
|
+
})
|
|
227
|
+
.join('\n');
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe lookup in a plain record: only the object's own keys count, so inputs
|
|
3
|
+
* like "constructor" or "__proto__" never resolve to Object.prototype members.
|
|
4
|
+
*/
|
|
5
|
+
export declare function own<T>(record: Readonly<Record<string, T>>, key: string): T | undefined;
|
|
6
|
+
/** Clamp a number into [min, max]; NaN/undefined fall back to `fallback`. */
|
|
7
|
+
export declare function clamp(value: number | undefined, min: number, max: number, fallback: number): number;
|
|
8
|
+
/** Escape text for HTML element content and double-quoted attributes. */
|
|
9
|
+
export declare function escapeHTML(s: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Accepts only values that are safe to embed as a CSS color: hex, rgb()/rgba()/hsl()/hsla()
|
|
12
|
+
* with numeric arguments, or a bare color keyword. Anything else returns `undefined`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function safeCssColor(value: string | undefined): string | undefined;
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe lookup in a plain record: only the object's own keys count, so inputs
|
|
3
|
+
* like "constructor" or "__proto__" never resolve to Object.prototype members.
|
|
4
|
+
*/
|
|
5
|
+
export function own(record, key) {
|
|
6
|
+
return Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined;
|
|
7
|
+
}
|
|
8
|
+
/** Clamp a number into [min, max]; NaN/undefined fall back to `fallback`. */
|
|
9
|
+
export function clamp(value, min, max, fallback) {
|
|
10
|
+
const v = typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
11
|
+
return Math.min(max, Math.max(min, v));
|
|
12
|
+
}
|
|
13
|
+
/** Escape text for HTML element content and double-quoted attributes. */
|
|
14
|
+
export function escapeHTML(s) {
|
|
15
|
+
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Accepts only values that are safe to embed as a CSS color: hex, rgb()/rgba()/hsl()/hsla()
|
|
19
|
+
* with numeric arguments, or a bare color keyword. Anything else returns `undefined`.
|
|
20
|
+
*/
|
|
21
|
+
export function safeCssColor(value) {
|
|
22
|
+
if (!value)
|
|
23
|
+
return undefined;
|
|
24
|
+
const v = value.trim();
|
|
25
|
+
if (/^#[0-9a-f]{3,8}$/i.test(v))
|
|
26
|
+
return v;
|
|
27
|
+
if (/^(rgb|rgba|hsl|hsla)\(\s*[-\d.%\s,/deg]+\)$/i.test(v))
|
|
28
|
+
return v;
|
|
29
|
+
if (/^[a-z]{3,30}$/i.test(v))
|
|
30
|
+
return v;
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
package/dist/width.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Display-width helpers. Terminals and monospace fonts draw CJK characters and
|
|
3
|
+
* emoji two columns wide, and combining marks zero wide. These helpers let every
|
|
4
|
+
* effect (box, pad, align, flip, …) line things up correctly.
|
|
5
|
+
*/
|
|
6
|
+
/** Split text into user-perceived characters (grapheme clusters). */
|
|
7
|
+
export declare function graphemes(text: string): string[];
|
|
8
|
+
/** Columns a single grapheme occupies: 0, 1 or 2. */
|
|
9
|
+
export declare function charWidth(grapheme: string): 0 | 1 | 2;
|
|
10
|
+
/** Display width of a string in columns. */
|
|
11
|
+
export declare function stringWidth(text: string): number;
|
|
12
|
+
/**
|
|
13
|
+
* Split a string into one entry per terminal column. A wide character is followed
|
|
14
|
+
* by an empty-string placeholder, so `cells(s).join('') === s` and `cells(s).length`
|
|
15
|
+
* is the display width. Zero-width graphemes are dropped from the grid.
|
|
16
|
+
*/
|
|
17
|
+
export declare function cells(text: string): string[];
|
|
18
|
+
/**
|
|
19
|
+
* Repair a cell row after cells were moved or replaced: a placeholder must follow a
|
|
20
|
+
* wide char, and a wide char must be followed by its placeholder.
|
|
21
|
+
*/
|
|
22
|
+
export declare function fixCells(row: string[]): string[];
|
package/dist/width.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Display-width helpers. Terminals and monospace fonts draw CJK characters and
|
|
3
|
+
* emoji two columns wide, and combining marks zero wide. These helpers let every
|
|
4
|
+
* effect (box, pad, align, flip, …) line things up correctly.
|
|
5
|
+
*/
|
|
6
|
+
let segmenter;
|
|
7
|
+
function getSegmenter() {
|
|
8
|
+
if (segmenter === undefined) {
|
|
9
|
+
const Seg = Intl.Segmenter;
|
|
10
|
+
segmenter = Seg ? new Seg(undefined, { granularity: 'grapheme' }) : null;
|
|
11
|
+
}
|
|
12
|
+
return segmenter;
|
|
13
|
+
}
|
|
14
|
+
/** Split text into user-perceived characters (grapheme clusters). */
|
|
15
|
+
export function graphemes(text) {
|
|
16
|
+
if (!/[^\x20-\x7e]/.test(text))
|
|
17
|
+
return text.split(''); // ASCII fast path
|
|
18
|
+
const seg = getSegmenter();
|
|
19
|
+
if (!seg)
|
|
20
|
+
return Array.from(text);
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const s of seg.segment(text))
|
|
23
|
+
out.push(s.segment);
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function isWideCodePoint(cp) {
|
|
27
|
+
return ((cp >= 0x1100 && cp <= 0x115f) || // Hangul Jamo
|
|
28
|
+
(cp >= 0x2e80 && cp <= 0x303e) || // CJK radicals, punctuation
|
|
29
|
+
(cp >= 0x3041 && cp <= 0x33ff) || // Kana, CJK compat
|
|
30
|
+
(cp >= 0x3400 && cp <= 0x4dbf) || // CJK ext A
|
|
31
|
+
(cp >= 0x4e00 && cp <= 0x9fff) || // CJK unified
|
|
32
|
+
(cp >= 0xa000 && cp <= 0xa4cf) || // Yi
|
|
33
|
+
(cp >= 0xac00 && cp <= 0xd7a3) || // Hangul syllables
|
|
34
|
+
(cp >= 0xf900 && cp <= 0xfaff) || // CJK compat ideographs
|
|
35
|
+
(cp >= 0xfe30 && cp <= 0xfe4f) || // CJK compat forms
|
|
36
|
+
(cp >= 0xff00 && cp <= 0xff60) || // Fullwidth forms
|
|
37
|
+
(cp >= 0xffe0 && cp <= 0xffe6) ||
|
|
38
|
+
(cp >= 0x1f300 && cp <= 0x1f64f) || // Misc symbols & pictographs, emoticons
|
|
39
|
+
(cp >= 0x1f680 && cp <= 0x1f6ff) || // Transport & map
|
|
40
|
+
(cp >= 0x1f900 && cp <= 0x1f9ff) || // Supplemental symbols & pictographs
|
|
41
|
+
(cp >= 0x1fa70 && cp <= 0x1faff) ||
|
|
42
|
+
(cp >= 0x20000 && cp <= 0x3fffd) // CJK ext B+
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
const ZERO_WIDTH = /^[\p{Mn}\p{Me}\p{Cf}]+$/u;
|
|
46
|
+
const EMOJI_PRESENTATION = /\p{Emoji_Presentation}|️/u;
|
|
47
|
+
/** Columns a single grapheme occupies: 0, 1 or 2. */
|
|
48
|
+
export function charWidth(grapheme) {
|
|
49
|
+
if (grapheme.length === 1) {
|
|
50
|
+
const c = grapheme.charCodeAt(0);
|
|
51
|
+
if (c >= 0x20 && c < 0x7f)
|
|
52
|
+
return 1;
|
|
53
|
+
if (c < 0x20 || (c >= 0x7f && c < 0xa0))
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
if (!grapheme || ZERO_WIDTH.test(grapheme))
|
|
57
|
+
return 0;
|
|
58
|
+
if (EMOJI_PRESENTATION.test(grapheme))
|
|
59
|
+
return 2;
|
|
60
|
+
return isWideCodePoint(grapheme.codePointAt(0)) ? 2 : 1;
|
|
61
|
+
}
|
|
62
|
+
/** Display width of a string in columns. */
|
|
63
|
+
export function stringWidth(text) {
|
|
64
|
+
if (!/[^\x20-\x7e]/.test(text))
|
|
65
|
+
return text.length;
|
|
66
|
+
let w = 0;
|
|
67
|
+
for (const g of graphemes(text))
|
|
68
|
+
w += charWidth(g);
|
|
69
|
+
return w;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Split a string into one entry per terminal column. A wide character is followed
|
|
73
|
+
* by an empty-string placeholder, so `cells(s).join('') === s` and `cells(s).length`
|
|
74
|
+
* is the display width. Zero-width graphemes are dropped from the grid.
|
|
75
|
+
*/
|
|
76
|
+
export function cells(text) {
|
|
77
|
+
if (!/[^\x20-\x7e]/.test(text))
|
|
78
|
+
return text.split('');
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const g of graphemes(text)) {
|
|
81
|
+
const w = charWidth(g);
|
|
82
|
+
if (w === 0) {
|
|
83
|
+
if (out.length)
|
|
84
|
+
out[out.length - 1] += g; // keep marks attached
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
out.push(g);
|
|
88
|
+
if (w === 2)
|
|
89
|
+
out.push('');
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Repair a cell row after cells were moved or replaced: a placeholder must follow a
|
|
95
|
+
* wide char, and a wide char must be followed by its placeholder.
|
|
96
|
+
*/
|
|
97
|
+
export function fixCells(row) {
|
|
98
|
+
const out = row.slice();
|
|
99
|
+
for (let i = 0; i < out.length; i++) {
|
|
100
|
+
const c = out[i];
|
|
101
|
+
if (c === '') {
|
|
102
|
+
const prev = out[i - 1];
|
|
103
|
+
if (prev === undefined || prev === '' || charWidth(prev) !== 2)
|
|
104
|
+
out[i] = ' ';
|
|
105
|
+
}
|
|
106
|
+
else if (charWidth(c) === 2 && out[i + 1] !== '') {
|
|
107
|
+
out[i] = ' ';
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|