@userspace-dev/ansi 0.0.0-stage → 0.1.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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/charset.d.ts +2 -0
- package/dist/charset.js +6 -0
- package/dist/html.d.ts +18 -0
- package/dist/html.js +96 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +7 -0
- package/dist/link.d.ts +8 -0
- package/dist/link.js +22 -0
- package/dist/palette.d.ts +12 -0
- package/dist/palette.js +39 -0
- package/dist/parser.d.ts +24 -0
- package/dist/parser.js +81 -0
- package/dist/row.d.ts +32 -0
- package/dist/row.js +246 -0
- package/dist/screen.d.ts +27 -0
- package/dist/screen.js +508 -0
- package/dist/stream.d.ts +10 -0
- package/dist/stream.js +24 -0
- package/dist/strip.d.ts +6 -0
- package/dist/strip.js +33 -0
- package/dist/style.d.ts +29 -0
- package/dist/style.js +216 -0
- package/dist/text.d.ts +17 -0
- package/dist/text.js +34 -0
- package/dist/tokenizer.d.ts +32 -0
- package/dist/tokenizer.js +259 -0
- package/dist/width.d.ts +16 -0
- package/dist/width.js +111 -0
- package/package.json +44 -4
package/dist/style.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
export const PLAIN = Object.freeze({});
|
|
2
|
+
const UNDERLINES = {
|
|
3
|
+
1: "single",
|
|
4
|
+
2: "double",
|
|
5
|
+
3: "curly",
|
|
6
|
+
4: "dotted",
|
|
7
|
+
5: "dashed",
|
|
8
|
+
};
|
|
9
|
+
/** Enough for any real output; hostile output must not grow it forever. */
|
|
10
|
+
const MAX_CACHED = 1024;
|
|
11
|
+
/**
|
|
12
|
+
* Applies SGR parameters, remembering the results. Output repeats a handful of
|
|
13
|
+
* styles, and the same style is then the same object.
|
|
14
|
+
*/
|
|
15
|
+
export function createSgr() {
|
|
16
|
+
let cache = new Map();
|
|
17
|
+
let size = 0;
|
|
18
|
+
return (style, params) => {
|
|
19
|
+
let known = cache.get(style);
|
|
20
|
+
const hit = known?.get(params);
|
|
21
|
+
if (hit) {
|
|
22
|
+
return hit;
|
|
23
|
+
}
|
|
24
|
+
if (size >= MAX_CACHED) {
|
|
25
|
+
cache = new Map();
|
|
26
|
+
known = undefined;
|
|
27
|
+
size = 0;
|
|
28
|
+
}
|
|
29
|
+
if (!known) {
|
|
30
|
+
known = new Map();
|
|
31
|
+
cache.set(style, known);
|
|
32
|
+
}
|
|
33
|
+
const next = applySgr(style, params);
|
|
34
|
+
known.set(params, next);
|
|
35
|
+
size++;
|
|
36
|
+
return next;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Applies the parameters of an SGR sequence (`ESC [ … m`) to a style. */
|
|
40
|
+
export function applySgr(style, params) {
|
|
41
|
+
const next = { ...style };
|
|
42
|
+
const groups = params.split(";");
|
|
43
|
+
for (let i = 0; i < groups.length; i++) {
|
|
44
|
+
const sub = (groups[i] ?? "").split(":");
|
|
45
|
+
const code = toNumber(sub[0]);
|
|
46
|
+
switch (code) {
|
|
47
|
+
case 0:
|
|
48
|
+
for (const key of Object.keys(next)) {
|
|
49
|
+
delete next[key];
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case 1:
|
|
53
|
+
next.bold = true;
|
|
54
|
+
break;
|
|
55
|
+
case 2:
|
|
56
|
+
next.dim = true;
|
|
57
|
+
break;
|
|
58
|
+
case 3:
|
|
59
|
+
next.italic = true;
|
|
60
|
+
break;
|
|
61
|
+
case 4: {
|
|
62
|
+
// `4:0` turns the underline off, `4:3` makes it curly.
|
|
63
|
+
const kind = sub.length > 1 ? toNumber(sub[1]) : 1;
|
|
64
|
+
const underline = UNDERLINES[kind];
|
|
65
|
+
if (underline) {
|
|
66
|
+
next.underline = underline;
|
|
67
|
+
}
|
|
68
|
+
else if (kind === 0) {
|
|
69
|
+
delete next.underline;
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case 5:
|
|
74
|
+
case 6:
|
|
75
|
+
next.blink = true;
|
|
76
|
+
break;
|
|
77
|
+
case 7:
|
|
78
|
+
next.inverse = true;
|
|
79
|
+
break;
|
|
80
|
+
case 8:
|
|
81
|
+
next.hidden = true;
|
|
82
|
+
break;
|
|
83
|
+
case 9:
|
|
84
|
+
next.strike = true;
|
|
85
|
+
break;
|
|
86
|
+
case 21:
|
|
87
|
+
next.underline = "double";
|
|
88
|
+
break;
|
|
89
|
+
case 22:
|
|
90
|
+
delete next.bold;
|
|
91
|
+
delete next.dim;
|
|
92
|
+
break;
|
|
93
|
+
case 23:
|
|
94
|
+
delete next.italic;
|
|
95
|
+
break;
|
|
96
|
+
case 24:
|
|
97
|
+
delete next.underline;
|
|
98
|
+
break;
|
|
99
|
+
case 25:
|
|
100
|
+
delete next.blink;
|
|
101
|
+
break;
|
|
102
|
+
case 27:
|
|
103
|
+
delete next.inverse;
|
|
104
|
+
break;
|
|
105
|
+
case 28:
|
|
106
|
+
delete next.hidden;
|
|
107
|
+
break;
|
|
108
|
+
case 29:
|
|
109
|
+
delete next.strike;
|
|
110
|
+
break;
|
|
111
|
+
case 39:
|
|
112
|
+
delete next.fg;
|
|
113
|
+
break;
|
|
114
|
+
case 49:
|
|
115
|
+
delete next.bg;
|
|
116
|
+
break;
|
|
117
|
+
case 53:
|
|
118
|
+
next.overline = true;
|
|
119
|
+
break;
|
|
120
|
+
case 55:
|
|
121
|
+
delete next.overline;
|
|
122
|
+
break;
|
|
123
|
+
case 59:
|
|
124
|
+
delete next.underlineColor;
|
|
125
|
+
break;
|
|
126
|
+
case 38:
|
|
127
|
+
case 48:
|
|
128
|
+
case 58: {
|
|
129
|
+
const { color, used } = sub.length > 1
|
|
130
|
+
? { color: colonColor(sub), used: 0 }
|
|
131
|
+
: semicolonColor(groups, i);
|
|
132
|
+
i += used;
|
|
133
|
+
if (color !== undefined) {
|
|
134
|
+
if (code === 38)
|
|
135
|
+
next.fg = color;
|
|
136
|
+
else if (code === 48)
|
|
137
|
+
next.bg = color;
|
|
138
|
+
else
|
|
139
|
+
next.underlineColor = color;
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
default:
|
|
144
|
+
if (code >= 30 && code <= 37)
|
|
145
|
+
next.fg = code - 30;
|
|
146
|
+
else if (code >= 40 && code <= 47)
|
|
147
|
+
next.bg = code - 40;
|
|
148
|
+
else if (code >= 90 && code <= 97)
|
|
149
|
+
next.fg = code - 90 + 8;
|
|
150
|
+
else if (code >= 100 && code <= 107)
|
|
151
|
+
next.bg = code - 100 + 8;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return isPlain(next) ? PLAIN : next;
|
|
155
|
+
}
|
|
156
|
+
function isPlain(style) {
|
|
157
|
+
for (const _key in style) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
export function isSameStyle(a, b) {
|
|
163
|
+
return (a === b ||
|
|
164
|
+
(a.fg === b.fg &&
|
|
165
|
+
a.bg === b.bg &&
|
|
166
|
+
a.underlineColor === b.underlineColor &&
|
|
167
|
+
a.underline === b.underline &&
|
|
168
|
+
a.bold === b.bold &&
|
|
169
|
+
a.dim === b.dim &&
|
|
170
|
+
a.italic === b.italic &&
|
|
171
|
+
a.blink === b.blink &&
|
|
172
|
+
a.inverse === b.inverse &&
|
|
173
|
+
a.hidden === b.hidden &&
|
|
174
|
+
a.strike === b.strike &&
|
|
175
|
+
a.overline === b.overline));
|
|
176
|
+
}
|
|
177
|
+
/** A missing parameter counts as 0. */
|
|
178
|
+
function toNumber(value) {
|
|
179
|
+
return value ? Number.parseInt(value, 10) : 0;
|
|
180
|
+
}
|
|
181
|
+
/** `38:5:n`, `38:2:r:g:b` and `38:2:colorspace:r:g:b` */
|
|
182
|
+
function colonColor(sub) {
|
|
183
|
+
const mode = toNumber(sub[1]);
|
|
184
|
+
if (mode === 5) {
|
|
185
|
+
return indexed(toNumber(sub[2]));
|
|
186
|
+
}
|
|
187
|
+
if (mode === 2) {
|
|
188
|
+
const [r, g, b] = sub.slice(sub.length > 5 ? 3 : 2);
|
|
189
|
+
return rgb(toNumber(r), toNumber(g), toNumber(b));
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
/** `38;5;n` and `38;2;r;g;b`, which take up the parameters after them. */
|
|
194
|
+
function semicolonColor(groups, index) {
|
|
195
|
+
const mode = toNumber(groups[index + 1]);
|
|
196
|
+
if (mode === 5) {
|
|
197
|
+
return { color: indexed(toNumber(groups[index + 2])), used: 2 };
|
|
198
|
+
}
|
|
199
|
+
if (mode === 2) {
|
|
200
|
+
return {
|
|
201
|
+
color: rgb(toNumber(groups[index + 2]), toNumber(groups[index + 3]), toNumber(groups[index + 4])),
|
|
202
|
+
used: 4,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return { color: undefined, used: 1 };
|
|
206
|
+
}
|
|
207
|
+
const isByte = (value) => value >= 0 && value <= 255;
|
|
208
|
+
function indexed(value) {
|
|
209
|
+
return isByte(value) ? value : undefined;
|
|
210
|
+
}
|
|
211
|
+
function rgb(r, g, b) {
|
|
212
|
+
return isByte(r) && isByte(g) && isByte(b) ? hex(r, g, b) : undefined;
|
|
213
|
+
}
|
|
214
|
+
export function hex(r, g, b) {
|
|
215
|
+
return `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, "0")}`;
|
|
216
|
+
}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ScreenOptions } from "./screen.ts";
|
|
2
|
+
export type TextOptions = Omit<ScreenOptions, "maxLines"> & {
|
|
3
|
+
/** Keeps the start and the end of longer output, and notes what was cut. */
|
|
4
|
+
maxLines?: number;
|
|
5
|
+
/**
|
|
6
|
+
* What to do with concealed text, which a person cannot see. It is dropped by
|
|
7
|
+
* default, since it can carry instructions meant only for a model.
|
|
8
|
+
*/
|
|
9
|
+
hidden?: "drop" | "keep";
|
|
10
|
+
/** The line that stands for the ones left out. */
|
|
11
|
+
omission?: (count: number) => string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The output as a person would have read it: without codes, with spinners and
|
|
15
|
+
* progress bars reduced to their last state. Meant for models and for logs.
|
|
16
|
+
*/
|
|
17
|
+
export declare function toText(output: string, { maxLines, hidden, omission, ...options }?: TextOptions): string;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createScreen } from "./screen.js";
|
|
2
|
+
/** How much of the line budget goes to the start; errors tend to be at the end. */
|
|
3
|
+
const HEAD_SHARE = 1 / 3;
|
|
4
|
+
/**
|
|
5
|
+
* The output as a person would have read it: without codes, with spinners and
|
|
6
|
+
* progress bars reduced to their last state. Meant for models and for logs.
|
|
7
|
+
*/
|
|
8
|
+
export function toText(output, { maxLines = Infinity, hidden = "drop", omission = (count) => `… ${count} lines omitted …`, ...options } = {}) {
|
|
9
|
+
const screen = createScreen(options);
|
|
10
|
+
screen.write(output);
|
|
11
|
+
screen.end();
|
|
12
|
+
const lines = screen.lines().map((line) => lineText(line, hidden).trimEnd());
|
|
13
|
+
while (lines.length > 0 && lines.at(-1) === "") {
|
|
14
|
+
lines.pop();
|
|
15
|
+
}
|
|
16
|
+
if (lines.length <= maxLines) {
|
|
17
|
+
return lines.join("\n");
|
|
18
|
+
}
|
|
19
|
+
const budget = Math.max(0, Math.floor(maxLines));
|
|
20
|
+
const head = Math.floor(budget * HEAD_SHARE);
|
|
21
|
+
const tail = budget - head;
|
|
22
|
+
const omitted = lines.length - head - tail;
|
|
23
|
+
return [
|
|
24
|
+
...lines.slice(0, head),
|
|
25
|
+
omission(omitted),
|
|
26
|
+
...(tail > 0 ? lines.slice(-tail) : []),
|
|
27
|
+
].join("\n");
|
|
28
|
+
}
|
|
29
|
+
function lineText({ segments }, hidden) {
|
|
30
|
+
return segments
|
|
31
|
+
.filter(({ style }) => hidden === "keep" || !style.hidden)
|
|
32
|
+
.map(({ text }) => text)
|
|
33
|
+
.join("");
|
|
34
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** A control sequence: `ESC [ prefix params intermediates final`. */
|
|
2
|
+
export type Csi = {
|
|
3
|
+
prefix: string;
|
|
4
|
+
params: string;
|
|
5
|
+
intermediates: string;
|
|
6
|
+
final: string;
|
|
7
|
+
};
|
|
8
|
+
export type Handler = {
|
|
9
|
+
/** Printable text without any control characters. */
|
|
10
|
+
text(text: string): void;
|
|
11
|
+
/** A single control character, by code. */
|
|
12
|
+
control(code: number): void;
|
|
13
|
+
csi(sequence: Csi): void;
|
|
14
|
+
/** A short escape sequence, such as `ESC c`. */
|
|
15
|
+
esc(intermediates: string, final: string): void;
|
|
16
|
+
/** The body of an operating system command, such as a hyperlink. */
|
|
17
|
+
osc(data: string): void;
|
|
18
|
+
};
|
|
19
|
+
export type Tokenizer = {
|
|
20
|
+
write(chunk: string): void;
|
|
21
|
+
/** Flushes text held back at a chunk boundary and forgets partial sequences. */
|
|
22
|
+
end(): void;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Splits terminal output into text and sequences. It keeps its state between
|
|
26
|
+
* writes, so a sequence may be cut anywhere, and it never throws: whatever is
|
|
27
|
+
* malformed is dropped.
|
|
28
|
+
*
|
|
29
|
+
* Follows the usual VT state machine, with one deviation that suits logs: a
|
|
30
|
+
* line feed ends an unterminated OSC, which limits the damage to one line.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createTokenizer(handler: Handler): Tokenizer;
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { toGraphics } from "./charset.js";
|
|
2
|
+
const GROUND = 0;
|
|
3
|
+
const ESCAPE = 1;
|
|
4
|
+
const ESCAPE_INTERMEDIATE = 2;
|
|
5
|
+
const CSI = 3;
|
|
6
|
+
const CSI_IGNORE = 4;
|
|
7
|
+
const OSC = 5;
|
|
8
|
+
const STRING = 6;
|
|
9
|
+
const BEL = 0x07;
|
|
10
|
+
const SO = 0x0e;
|
|
11
|
+
const SI = 0x0f;
|
|
12
|
+
const LF = 0x0a;
|
|
13
|
+
const CAN = 0x18;
|
|
14
|
+
const SUB = 0x1a;
|
|
15
|
+
const ESC = 0x1b;
|
|
16
|
+
const DEL = 0x7f;
|
|
17
|
+
/** Longer control sequences are noise, and are skipped instead of collected. */
|
|
18
|
+
const MAX_CSI = 256;
|
|
19
|
+
/** Room for any hyperlink; longer commands (such as clipboard data) are skipped. */
|
|
20
|
+
const MAX_OSC = 8192;
|
|
21
|
+
/** An unterminated image must not swallow the rest of the output. */
|
|
22
|
+
const MAX_STRING = 1 << 20;
|
|
23
|
+
const isControl = (code) => code < 0x20 || (code >= DEL && code <= 0x9f);
|
|
24
|
+
const isHighSurrogate = (code) => code >= 0xd800 && code <= 0xdbff;
|
|
25
|
+
/**
|
|
26
|
+
* Splits terminal output into text and sequences. It keeps its state between
|
|
27
|
+
* writes, so a sequence may be cut anywhere, and it never throws: whatever is
|
|
28
|
+
* malformed is dropped.
|
|
29
|
+
*
|
|
30
|
+
* Follows the usual VT state machine, with one deviation that suits logs: a
|
|
31
|
+
* line feed ends an unterminated OSC, which limits the damage to one line.
|
|
32
|
+
*/
|
|
33
|
+
export function createTokenizer(handler) {
|
|
34
|
+
let state = GROUND;
|
|
35
|
+
let prefix = "";
|
|
36
|
+
let params = "";
|
|
37
|
+
let intermediates = "";
|
|
38
|
+
let data = "";
|
|
39
|
+
let length = 0;
|
|
40
|
+
let carry = "";
|
|
41
|
+
/** Whether G0 and G1 draw lines, and which of them is in use. */
|
|
42
|
+
let graphics = [false, false];
|
|
43
|
+
let active = 0;
|
|
44
|
+
let saved = { graphics, active };
|
|
45
|
+
const text = (value) => {
|
|
46
|
+
handler.text(graphics[active] ? toGraphics(value) : value);
|
|
47
|
+
};
|
|
48
|
+
const saveCharset = () => {
|
|
49
|
+
saved = { graphics: [...graphics], active };
|
|
50
|
+
};
|
|
51
|
+
const restoreCharset = () => {
|
|
52
|
+
graphics = [...saved.graphics];
|
|
53
|
+
active = saved.active;
|
|
54
|
+
};
|
|
55
|
+
const resetCharset = () => {
|
|
56
|
+
graphics = [false, false];
|
|
57
|
+
active = 0;
|
|
58
|
+
saved = { graphics, active };
|
|
59
|
+
};
|
|
60
|
+
const enter = (next) => {
|
|
61
|
+
state = next;
|
|
62
|
+
prefix = params = intermediates = data = "";
|
|
63
|
+
length = 0;
|
|
64
|
+
};
|
|
65
|
+
/** Handles a control character outside of any sequence. */
|
|
66
|
+
const ground = (code) => {
|
|
67
|
+
switch (code) {
|
|
68
|
+
case ESC:
|
|
69
|
+
enter(ESCAPE);
|
|
70
|
+
break;
|
|
71
|
+
case 0x9b:
|
|
72
|
+
enter(CSI);
|
|
73
|
+
break;
|
|
74
|
+
case 0x9d:
|
|
75
|
+
enter(OSC);
|
|
76
|
+
break;
|
|
77
|
+
case 0x90:
|
|
78
|
+
case 0x98:
|
|
79
|
+
case 0x9e:
|
|
80
|
+
case 0x9f:
|
|
81
|
+
enter(STRING);
|
|
82
|
+
break;
|
|
83
|
+
case DEL:
|
|
84
|
+
break;
|
|
85
|
+
case SO:
|
|
86
|
+
active = 1;
|
|
87
|
+
break;
|
|
88
|
+
case SI:
|
|
89
|
+
active = 0;
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
handler.control(code);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const escape = (code) => {
|
|
96
|
+
switch (code) {
|
|
97
|
+
case 0x5b: // [
|
|
98
|
+
enter(CSI);
|
|
99
|
+
break;
|
|
100
|
+
case 0x5d: // ]
|
|
101
|
+
enter(OSC);
|
|
102
|
+
break;
|
|
103
|
+
case 0x50: // P
|
|
104
|
+
case 0x58: // X
|
|
105
|
+
case 0x5e: // ^
|
|
106
|
+
case 0x5f: // _
|
|
107
|
+
enter(STRING);
|
|
108
|
+
break;
|
|
109
|
+
default:
|
|
110
|
+
if (code <= 0x2f) {
|
|
111
|
+
intermediates += String.fromCharCode(code);
|
|
112
|
+
state = ESCAPE_INTERMEDIATE;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const found = intermediates;
|
|
116
|
+
const final = String.fromCharCode(code);
|
|
117
|
+
enter(GROUND);
|
|
118
|
+
if (found === "(" || found === ")") {
|
|
119
|
+
graphics[found === "(" ? 0 : 1] = final === "0";
|
|
120
|
+
}
|
|
121
|
+
else if (!found && final === "7")
|
|
122
|
+
saveCharset();
|
|
123
|
+
else if (!found && final === "8")
|
|
124
|
+
restoreCharset();
|
|
125
|
+
else if (!found && final === "c")
|
|
126
|
+
resetCharset();
|
|
127
|
+
handler.esc(found, final);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const csi = (code) => {
|
|
132
|
+
const char = String.fromCharCode(code);
|
|
133
|
+
if (code >= 0x40) {
|
|
134
|
+
const sequence = { prefix, params, intermediates, final: char };
|
|
135
|
+
enter(GROUND);
|
|
136
|
+
if (!sequence.prefix && !sequence.intermediates) {
|
|
137
|
+
if (char === "s")
|
|
138
|
+
saveCharset();
|
|
139
|
+
else if (char === "u")
|
|
140
|
+
restoreCharset();
|
|
141
|
+
}
|
|
142
|
+
handler.csi(sequence);
|
|
143
|
+
}
|
|
144
|
+
else if (++length > MAX_CSI) {
|
|
145
|
+
enter(CSI_IGNORE);
|
|
146
|
+
}
|
|
147
|
+
else if (code >= 0x3c) {
|
|
148
|
+
if (params || prefix || intermediates)
|
|
149
|
+
enter(CSI_IGNORE);
|
|
150
|
+
else
|
|
151
|
+
prefix = char;
|
|
152
|
+
}
|
|
153
|
+
else if (code >= 0x30) {
|
|
154
|
+
if (intermediates)
|
|
155
|
+
enter(CSI_IGNORE);
|
|
156
|
+
else
|
|
157
|
+
params += char;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
intermediates += char;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
/** Returns whether the character was used up. */
|
|
164
|
+
const step = (code) => {
|
|
165
|
+
if (state === GROUND) {
|
|
166
|
+
ground(code);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
if (code === ESC) {
|
|
170
|
+
// Also ends an OSC; the backslash of the terminator is then a no-op.
|
|
171
|
+
if (state === OSC)
|
|
172
|
+
finishOsc();
|
|
173
|
+
enter(ESCAPE);
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
if (code === CAN || code === SUB) {
|
|
177
|
+
enter(GROUND);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
if (state === OSC) {
|
|
181
|
+
if (code === BEL || code === 0x9c) {
|
|
182
|
+
finishOsc();
|
|
183
|
+
}
|
|
184
|
+
else if (code === LF) {
|
|
185
|
+
enter(GROUND);
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
else if (code >= 0x20 && code !== DEL) {
|
|
189
|
+
if (++length <= MAX_OSC)
|
|
190
|
+
data += String.fromCharCode(code);
|
|
191
|
+
}
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
if (state === STRING) {
|
|
195
|
+
if (code === 0x9c || ++length > MAX_STRING)
|
|
196
|
+
enter(GROUND);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
// Inside a sequence, other control characters take effect immediately.
|
|
200
|
+
if (code < 0x20) {
|
|
201
|
+
ground(code);
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (code === DEL) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
// Not part of any sequence: give up on it and keep the text.
|
|
208
|
+
if (code > DEL) {
|
|
209
|
+
enter(GROUND);
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
if (state === CSI)
|
|
213
|
+
csi(code);
|
|
214
|
+
else if (state !== CSI_IGNORE)
|
|
215
|
+
escape(code);
|
|
216
|
+
else if (code >= 0x40)
|
|
217
|
+
enter(GROUND);
|
|
218
|
+
return true;
|
|
219
|
+
};
|
|
220
|
+
const finishOsc = () => {
|
|
221
|
+
const body = data;
|
|
222
|
+
const complete = length <= MAX_OSC;
|
|
223
|
+
enter(GROUND);
|
|
224
|
+
if (complete)
|
|
225
|
+
handler.osc(body);
|
|
226
|
+
};
|
|
227
|
+
return {
|
|
228
|
+
write(chunk) {
|
|
229
|
+
let input = carry + chunk;
|
|
230
|
+
carry = "";
|
|
231
|
+
// Half of a surrogate pair would turn into a broken character.
|
|
232
|
+
if (isHighSurrogate(input.charCodeAt(input.length - 1))) {
|
|
233
|
+
carry = input.slice(-1);
|
|
234
|
+
input = input.slice(0, -1);
|
|
235
|
+
}
|
|
236
|
+
let index = 0;
|
|
237
|
+
while (index < input.length) {
|
|
238
|
+
const code = input.charCodeAt(index);
|
|
239
|
+
if (state === GROUND && !isControl(code)) {
|
|
240
|
+
let end = index + 1;
|
|
241
|
+
while (end < input.length && !isControl(input.charCodeAt(end)))
|
|
242
|
+
end++;
|
|
243
|
+
text(input.slice(index, end));
|
|
244
|
+
index = end;
|
|
245
|
+
}
|
|
246
|
+
else if (step(code)) {
|
|
247
|
+
index++;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
end() {
|
|
252
|
+
const rest = carry;
|
|
253
|
+
carry = "";
|
|
254
|
+
if (rest && state === GROUND)
|
|
255
|
+
text(rest);
|
|
256
|
+
enter(GROUND);
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
package/dist/width.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Whether every code unit is one character that takes one column. */
|
|
2
|
+
export declare const isNarrow: (text: string) => boolean;
|
|
3
|
+
/** Whether a character may combine with what comes before or after it. */
|
|
4
|
+
export declare const mayJoin: (text: string) => boolean;
|
|
5
|
+
/**
|
|
6
|
+
* How many columns one character, as a person sees it, takes. Usually 0, 1 or
|
|
7
|
+
* 2; some scripts write several consonants as one.
|
|
8
|
+
*/
|
|
9
|
+
export declare function clusterWidth(cluster: string): number;
|
|
10
|
+
/** Splits text into characters as a person sees them. */
|
|
11
|
+
export declare function clusters(text: string): Iterable<string>;
|
|
12
|
+
/**
|
|
13
|
+
* What two pieces of text become as one character, or null if they are two.
|
|
14
|
+
* The result is `a` itself where the character is already as long as it gets.
|
|
15
|
+
*/
|
|
16
|
+
export declare function join(a: string, b: string): string | null;
|
package/dist/width.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wide and fullwidth characters from the Unicode East Asian Width data, above
|
|
3
|
+
* U+0300. Pairs in base 36: the gap since the last range, and its length.
|
|
4
|
+
*/
|
|
5
|
+
const WIDE = "3cw 2n 3i3 1 e 1 5b 3 4 0 3 0 ei 1 m 1 r 7 h b 18 0 b 5 4 0 e 0 9 1 i 1 6 1 9 0 6 0 m 0 8 1 2 0 5 0 3 0 8 0 5 1 t 0 10 0 2 0 5 2 2 0 1q 2 p 0 f 0 nw 1 1g 0 5 0 mj p 2 2g d 5x r 26 3 2d 3 2u 6 16 2 2l 2 2d a 1b 2 13 9 mkc 4 1i wq s hw 8mb 6l9 e7 lt 9 n y 2 i 2 3 46 2n 3k 6 m4a 4 c 6 a 5p6 11 x 2o 36 e pd f 1e 5y6 3 2 6 2 1 2 88 a 0 u 2 3 0 f 4 8 az 6bp 2e a m 5n2 0 5n 0 5b 0 3 9 k 0 2a 2 e 17 5 8 8 1 f 5 4b w d 8 2 1x 2 l d 16 5 4 d g 4 0 4 1y 2 0 2 56 3 1q e 3 2 n j 0 r 1 e 0 2f 2c 1d 1x 7 0 4 2 3 4 3 3 c 1 8 8 66 0 6 b 5 0 7w 1a 2 9 2 54 35 c 4 1y 2 0 4 h 2 c 4 b zq 1ekd 3 1ekd";
|
|
6
|
+
let wide = null;
|
|
7
|
+
/** Range starts and ends, alternating and sorted. */
|
|
8
|
+
function wideRanges() {
|
|
9
|
+
if (!wide) {
|
|
10
|
+
let last = 0;
|
|
11
|
+
wide = WIDE.split(" ").map((value) => {
|
|
12
|
+
last += Number.parseInt(value, 36);
|
|
13
|
+
return last;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return wide;
|
|
17
|
+
}
|
|
18
|
+
function isWide(code) {
|
|
19
|
+
const ranges = wideRanges();
|
|
20
|
+
let low = 0;
|
|
21
|
+
let high = ranges.length / 2 - 1;
|
|
22
|
+
while (low <= high) {
|
|
23
|
+
const middle = (low + high) >> 1;
|
|
24
|
+
if (code < ranges[middle * 2])
|
|
25
|
+
high = middle - 1;
|
|
26
|
+
else if (code > ranges[middle * 2 + 1])
|
|
27
|
+
low = middle + 1;
|
|
28
|
+
else
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
/** Format characters that are drawn, such as the Arabic number sign. */
|
|
34
|
+
const PREPENDED = "\\u0600-\\u0605\\u06dd\\u0890\\u0891\\u08e2\\u{110bd}\\u{110cd}";
|
|
35
|
+
const VISIBLE = new RegExp(`[^\\p{Default_Ignorable_Code_Point}\\p{Control}\\p{Format}\\p{Nonspacing_Mark}\\p{Enclosing_Mark}]|[${PREPENDED}]`, "u");
|
|
36
|
+
const EMOJI = /^(?:\p{Emoji_Presentation}|\p{Emoji}\ufe0f|\p{Emoji_Modifier_Base}\p{Emoji_Modifier})/u;
|
|
37
|
+
/** A wide emoji that asks for its narrow text style. */
|
|
38
|
+
const TEXT_STYLE = /^(?![\u{10000}-\u{10ffff}])\p{Emoji_Presentation}\ufe0e/u;
|
|
39
|
+
const ZWJ = "\u200d";
|
|
40
|
+
const JOINED_EMOJI = /\u200d\p{Extended_Pictographic}/u;
|
|
41
|
+
const HANGUL = /^[\u1100-\u11ff\ua960-\ua97c\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb]/;
|
|
42
|
+
/** Characters that may join with their neighbours into one. */
|
|
43
|
+
const JOINING = new RegExp(`[\\p{Mark}\\p{Format}\\p{Emoji_Modifier}\\p{RI}\\p{Default_Ignorable_Code_Point}${PREPENDED}\\u0e33\\u0eb3\\u1100-\\u11ff\\ua960-\\ua97f\\ud7b0-\\ud7ff\\uff9e\\uff9f]`, "u");
|
|
44
|
+
// Latin, Greek, Cyrillic and other scripts, arrows, math and box drawing. No
|
|
45
|
+
// character in here is wide, and each one is a single code unit.
|
|
46
|
+
const NOT_NARROW = /[^\x20-\x7e\xa0-\u10ff\u2010-\u2319\u2400-\u25fc]/;
|
|
47
|
+
/** Whether every code unit is one character that takes one column. */
|
|
48
|
+
export const isNarrow = (text) => !NOT_NARROW.test(text) && !mayJoin(text);
|
|
49
|
+
/** Whether a character may combine with what comes before or after it. */
|
|
50
|
+
export const mayJoin = (text) => JOINING.test(text);
|
|
51
|
+
/**
|
|
52
|
+
* Longer characters are cut. Real ones are far shorter, and endless marks on a
|
|
53
|
+
* single letter would otherwise take endless work.
|
|
54
|
+
*/
|
|
55
|
+
const MAX_CLUSTER = 64;
|
|
56
|
+
/**
|
|
57
|
+
* How many columns one character, as a person sees it, takes. Usually 0, 1 or
|
|
58
|
+
* 2; some scripts write several consonants as one.
|
|
59
|
+
*/
|
|
60
|
+
export function clusterWidth(cluster) {
|
|
61
|
+
const code = cluster.codePointAt(0) ?? 0;
|
|
62
|
+
if (code < 0x300 && cluster.length === 1)
|
|
63
|
+
return 1;
|
|
64
|
+
if (TEXT_STYLE.test(cluster))
|
|
65
|
+
return 1;
|
|
66
|
+
if (EMOJI.test(cluster) || JOINED_EMOJI.test(cluster))
|
|
67
|
+
return 2;
|
|
68
|
+
let width = 0;
|
|
69
|
+
for (const char of cluster) {
|
|
70
|
+
// Whatever is joined on is drawn inside of what came before.
|
|
71
|
+
if (char === ZWJ && width > 0)
|
|
72
|
+
break;
|
|
73
|
+
if (!VISIBLE.test(char))
|
|
74
|
+
continue;
|
|
75
|
+
width += isWide(char.codePointAt(0)) ? 2 : 1;
|
|
76
|
+
// One syllable, however many letters it was typed with.
|
|
77
|
+
if (HANGUL.test(cluster))
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
return width;
|
|
81
|
+
}
|
|
82
|
+
const segmenter = typeof Intl !== "undefined" && "Segmenter" in Intl
|
|
83
|
+
? new Intl.Segmenter(undefined, { granularity: "grapheme" })
|
|
84
|
+
: null;
|
|
85
|
+
/** Splits text into characters as a person sees them. */
|
|
86
|
+
export function clusters(text) {
|
|
87
|
+
return segmenter && mayJoin(text) ? segments(text, segmenter) : text;
|
|
88
|
+
}
|
|
89
|
+
function* segments(text, by) {
|
|
90
|
+
for (const { segment } of by.segment(text)) {
|
|
91
|
+
yield segment.length > MAX_CLUSTER ? cut(segment) : segment;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Cuts without breaking a surrogate pair. */
|
|
95
|
+
function cut(cluster) {
|
|
96
|
+
const code = cluster.charCodeAt(MAX_CLUSTER - 1);
|
|
97
|
+
return cluster.slice(0, MAX_CLUSTER - (code >= 0xd800 && code <= 0xdbff ? 1 : 0));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* What two pieces of text become as one character, or null if they are two.
|
|
101
|
+
* The result is `a` itself where the character is already as long as it gets.
|
|
102
|
+
*/
|
|
103
|
+
export function join(a, b) {
|
|
104
|
+
const whole = a + b;
|
|
105
|
+
if (!segmenter || !mayJoin(whole))
|
|
106
|
+
return null;
|
|
107
|
+
const iterator = segmenter.segment(whole)[Symbol.iterator]();
|
|
108
|
+
if (iterator.next().value?.segment.length !== whole.length)
|
|
109
|
+
return null;
|
|
110
|
+
return whole.length > MAX_CLUSTER ? a : whole;
|
|
111
|
+
}
|