@alchemy.run/sigil 0.0.0-alpha.4 → 0.0.0-alpha.6
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/README.md +21 -9
- package/THIRD_PARTY_NOTICES.md +39 -1
- package/dist/Text-BobFKi74.d.ts +452 -0
- package/dist/ansi.d.ts +122 -131
- package/dist/ansi.js +86 -6
- package/dist/capabilities.d.ts +5 -0
- package/dist/capabilities.js +3 -0
- package/dist/cell-_ZVhbfl0.js +44 -0
- package/dist/color-CkbalRqK.js +2 -0
- package/dist/color-policy-BMzMwV7Q.d.ts +22 -0
- package/dist/color-policy-SVj1pYTA.js +560 -0
- package/dist/color-profile-DHhQHY55.js +36 -0
- package/dist/color-profile-u0Nhe9Nv.d.ts +97 -0
- package/dist/color.d.ts +21 -0
- package/dist/color.js +3 -0
- package/dist/cursor-position-D2LAkRG0.d.ts +7 -0
- package/dist/detect-Bh4yGP6w.d.ts +186 -0
- package/dist/detect-BuTXtY6e.js +373 -0
- package/dist/env-YVw64yZS.js +9 -0
- package/dist/escapes-CB_6CWOE.d.ts +72 -0
- package/dist/geometry-BxXOzJgo.d.ts +11 -0
- package/dist/index-DZ88EXJv.d.ts +21 -0
- package/dist/index.d.ts +143 -498
- package/dist/index.js +1026 -2244
- package/dist/osc-CCH7xDoS.js +71 -0
- package/dist/osc-Cn0fw77g.d.ts +23 -0
- package/dist/paint-C19minOS.d.ts +81 -0
- package/dist/query-vaIeGOkH.d.ts +152 -0
- package/dist/router.d.ts +392 -0
- package/dist/router.js +709 -0
- package/dist/sample-Cqw1bjUL.js +445 -0
- package/dist/screen-BOSLQ8fF.d.ts +49 -0
- package/dist/screen-CiPytswf.js +342 -0
- package/dist/screen.d.ts +5 -0
- package/dist/screen.js +5 -0
- package/dist/semantic-text-style-DIMzC7xt.js +91 -0
- package/dist/serialize-BTkAZgw1.js +79 -0
- package/dist/session-aZr9O8h3.js +665 -0
- package/dist/sgr-BhwaWAJB.js +246 -0
- package/dist/store-CgrG9K4y.d.ts +72 -0
- package/dist/string-width-CijQwpIk.js +69 -0
- package/dist/strip-BvU4toXG.js +6 -0
- package/dist/terminal.d.ts +118 -0
- package/dist/terminal.js +2 -0
- package/dist/tokenize-AjqbvtiT.js +1242 -0
- package/dist/tokenize-Dx1y_l5H.d.ts +57 -0
- package/dist/truncate-D31fhU6i.js +562 -0
- package/dist/use-focus-BzqAJi0n.js +1337 -0
- package/package.json +41 -9
- package/src/ansi/chalk.ts +5 -3
- package/src/ansi/escapes.ts +14 -0
- package/src/ansi/graphemes.ts +8 -0
- package/src/ansi/hyperlink.ts +44 -0
- package/src/ansi/index.ts +3 -1
- package/src/ansi/osc.ts +77 -0
- package/src/ansi/tokenize.ts +3 -4
- package/src/capabilities/color-policy.ts +34 -0
- package/src/capabilities/detect.ts +594 -0
- package/src/capabilities/index.ts +37 -0
- package/src/capabilities/query.ts +657 -0
- package/src/capabilities/store.ts +379 -0
- package/src/color/index.ts +3 -0
- package/src/color/paint.ts +169 -0
- package/src/color/palette.ts +48 -0
- package/src/color/sample.ts +323 -0
- package/src/color.ts +1 -0
- package/src/components/AnsiText.tsx +42 -0
- package/src/components/App.tsx +98 -10
- package/src/components/BackgroundContext.ts +2 -3
- package/src/components/Box.tsx +0 -8
- package/src/components/CursorContext.ts +1 -1
- package/src/components/Hyperlink.tsx +56 -0
- package/src/components/TerminalOscContext.ts +25 -0
- package/src/components/Text.tsx +22 -45
- package/src/components/Transform.tsx +1 -1
- package/src/dom.ts +11 -2
- package/src/global.d.ts +3 -0
- package/src/hooks/use-capabilities.ts +73 -0
- package/src/hooks/use-cursor.ts +2 -2
- package/src/hooks/use-terminal-osc.ts +59 -0
- package/src/index.ts +45 -1
- package/src/ink.tsx +213 -153
- package/src/{render-node-to-output.ts → paint-tree.ts} +75 -48
- package/src/reconciler.ts +24 -3
- package/src/render-background.ts +36 -15
- package/src/render-border.ts +94 -61
- package/src/render-frame.ts +83 -0
- package/src/render-to-string.ts +21 -6
- package/src/render.ts +15 -6
- package/src/router/components.tsx +343 -0
- package/src/router/context.ts +41 -0
- package/src/router/history.ts +194 -0
- package/src/router/hooks.tsx +391 -0
- package/src/router/index.ts +34 -0
- package/src/router/matcher.ts +571 -0
- package/src/screen/ansi.ts +184 -0
- package/src/screen/canvas.ts +160 -0
- package/src/screen/cell.ts +138 -0
- package/src/screen/color-profile.ts +47 -0
- package/src/screen/geometry.ts +9 -0
- package/src/screen/index.ts +6 -0
- package/src/screen/screen.ts +305 -0
- package/src/screen/serialize.ts +129 -0
- package/src/screen.ts +1 -0
- package/src/semantic-text-style.ts +118 -0
- package/src/squash-text-nodes.ts +2 -5
- package/src/structured-text.ts +325 -0
- package/src/styles.ts +19 -14
- package/src/terminal/index.ts +2 -0
- package/src/terminal/inline-presenter.ts +120 -0
- package/src/terminal/input.ts +86 -0
- package/src/terminal/render-scheduler.ts +37 -0
- package/src/terminal/screen-presenter.ts +188 -0
- package/src/terminal/session.ts +407 -0
- package/src/terminal.ts +1 -0
- package/src/testing/browser.ts +588 -0
- package/src/testing/emulators.ts +205 -0
- package/src/testing/explorer-app/index.html +12 -0
- package/src/testing/explorer-app/main.ts +381 -0
- package/src/testing/explorer-app/style.css +194 -0
- package/src/testing/explorer-app/tsconfig.json +15 -0
- package/src/testing/explorer-app/vite-env.d.ts +1 -0
- package/src/testing/index.ts +26 -0
- package/src/testing/keys.ts +56 -0
- package/src/testing/live.ts +85 -0
- package/src/testing/matchers.ts +70 -0
- package/src/testing/public.ts +94 -0
- package/src/testing/terminal.ts +349 -0
- package/src/testing/vitest.ts +157 -0
- package/src/transform-adapter.ts +14 -0
- package/src/wrap-text.ts +4 -0
- package/dist/sgr-CMfEpjSk.d.ts +0 -91
- package/dist/truncate-Cr6xVFMa.js +0 -2330
- package/src/ansi/supports-color.ts +0 -207
- package/src/colorize.ts +0 -60
- package/src/log-update.ts +0 -370
- package/src/output.ts +0 -308
- package/src/renderer.ts +0 -73
|
@@ -1,2330 +0,0 @@
|
|
|
1
|
-
import { isatty } from "node:tty";
|
|
2
|
-
//#region src/ansi/escapes.ts
|
|
3
|
-
const ESC = "\x1B";
|
|
4
|
-
const BEL = "\x07";
|
|
5
|
-
const DEL = "";
|
|
6
|
-
/** Control Sequence Introducer. */
|
|
7
|
-
const CSI$1 = `[`;
|
|
8
|
-
/** Operating System Command. */
|
|
9
|
-
const OSC$1 = `]`;
|
|
10
|
-
/** String Terminator. */
|
|
11
|
-
const ST = `\\`;
|
|
12
|
-
/** Single-byte C1 forms of CSI and ST. */
|
|
13
|
-
const C1_CSI = "";
|
|
14
|
-
const C1_ST = "";
|
|
15
|
-
const sep = ";";
|
|
16
|
-
const cursorTo = (x, y) => {
|
|
17
|
-
if (typeof y !== "number") return CSI$1 + (x + 1) + "G";
|
|
18
|
-
return CSI$1 + (y + 1) + sep + (x + 1) + "H";
|
|
19
|
-
};
|
|
20
|
-
const cursorUp = (count = 1) => CSI$1 + count + "A";
|
|
21
|
-
const cursorDown = (count = 1) => CSI$1 + count + "B";
|
|
22
|
-
const cursorLeft = CSI$1 + "G";
|
|
23
|
-
const cursorNextLine = CSI$1 + "E";
|
|
24
|
-
const eraseEndLine = CSI$1 + "K";
|
|
25
|
-
const eraseLine = CSI$1 + "2K";
|
|
26
|
-
const eraseScreen = CSI$1 + "2J";
|
|
27
|
-
const eraseLines = (count) => {
|
|
28
|
-
let clear = "";
|
|
29
|
-
for (let i = 0; i < count; i++) clear += eraseLine + (i < count - 1 ? cursorUp() : "");
|
|
30
|
-
if (count) clear += cursorLeft;
|
|
31
|
-
return clear;
|
|
32
|
-
};
|
|
33
|
-
const clearTerminal = `${eraseScreen}${CSI$1}3J${CSI$1}H`;
|
|
34
|
-
const enterAlternativeScreen = CSI$1 + "?1049h";
|
|
35
|
-
const exitAlternativeScreen = CSI$1 + "?1049l";
|
|
36
|
-
const cursorShow = CSI$1 + "?25h";
|
|
37
|
-
const cursorHide = CSI$1 + "?25l";
|
|
38
|
-
const enableBracketedPaste = CSI$1 + "?2004h";
|
|
39
|
-
const disableBracketedPaste = CSI$1 + "?2004l";
|
|
40
|
-
const pasteStart = CSI$1 + "200~";
|
|
41
|
-
const pasteEnd = CSI$1 + "201~";
|
|
42
|
-
const bsu = CSI$1 + "?2026h";
|
|
43
|
-
const esu = CSI$1 + "?2026l";
|
|
44
|
-
const kittyQuery = CSI$1 + "?u";
|
|
45
|
-
const pushKittyKeyboard = (flags) => `${CSI$1}>${flags}u`;
|
|
46
|
-
const popKittyKeyboard = CSI$1 + "<u";
|
|
47
|
-
const link = (text, url) => [
|
|
48
|
-
OSC$1,
|
|
49
|
-
"8",
|
|
50
|
-
sep,
|
|
51
|
-
sep,
|
|
52
|
-
url,
|
|
53
|
-
"\x07",
|
|
54
|
-
text,
|
|
55
|
-
OSC$1,
|
|
56
|
-
"8",
|
|
57
|
-
sep,
|
|
58
|
-
sep,
|
|
59
|
-
"\x07"
|
|
60
|
-
].join("");
|
|
61
|
-
const ansiEscapes = {
|
|
62
|
-
cursorTo,
|
|
63
|
-
cursorUp,
|
|
64
|
-
cursorDown,
|
|
65
|
-
cursorLeft,
|
|
66
|
-
cursorNextLine,
|
|
67
|
-
eraseEndLine,
|
|
68
|
-
eraseLine,
|
|
69
|
-
eraseScreen,
|
|
70
|
-
eraseLines,
|
|
71
|
-
clearTerminal,
|
|
72
|
-
enterAlternativeScreen,
|
|
73
|
-
exitAlternativeScreen,
|
|
74
|
-
cursorShow,
|
|
75
|
-
cursorHide,
|
|
76
|
-
enableBracketedPaste,
|
|
77
|
-
disableBracketedPaste,
|
|
78
|
-
pasteStart,
|
|
79
|
-
pasteEnd,
|
|
80
|
-
bsu,
|
|
81
|
-
esu,
|
|
82
|
-
kittyQuery,
|
|
83
|
-
pushKittyKeyboard,
|
|
84
|
-
popKittyKeyboard,
|
|
85
|
-
link
|
|
86
|
-
};
|
|
87
|
-
//#endregion
|
|
88
|
-
//#region src/ansi/sgr.ts
|
|
89
|
-
const ANSI_BACKGROUND_OFFSET = 10;
|
|
90
|
-
const wrapAnsi16 = (offset = 0) => (code) => `${CSI$1}${code + offset}m`;
|
|
91
|
-
const wrapAnsi256 = (offset = 0) => (code) => `${CSI$1}${38 + offset};5;${code}m`;
|
|
92
|
-
const wrapAnsi16m = (offset = 0) => (red, green, blue) => `${CSI$1}${38 + offset};2;${red};${green};${blue}m`;
|
|
93
|
-
const modifierCodes = {
|
|
94
|
-
reset: [0, 0],
|
|
95
|
-
bold: [1, 22],
|
|
96
|
-
dim: [2, 22],
|
|
97
|
-
italic: [3, 23],
|
|
98
|
-
underline: [4, 24],
|
|
99
|
-
overline: [53, 55],
|
|
100
|
-
inverse: [7, 27],
|
|
101
|
-
hidden: [8, 28],
|
|
102
|
-
strikethrough: [9, 29]
|
|
103
|
-
};
|
|
104
|
-
const colorCodes = {
|
|
105
|
-
black: [30, 39],
|
|
106
|
-
red: [31, 39],
|
|
107
|
-
green: [32, 39],
|
|
108
|
-
yellow: [33, 39],
|
|
109
|
-
blue: [34, 39],
|
|
110
|
-
magenta: [35, 39],
|
|
111
|
-
cyan: [36, 39],
|
|
112
|
-
white: [37, 39],
|
|
113
|
-
blackBright: [90, 39],
|
|
114
|
-
gray: [90, 39],
|
|
115
|
-
grey: [90, 39],
|
|
116
|
-
redBright: [91, 39],
|
|
117
|
-
greenBright: [92, 39],
|
|
118
|
-
yellowBright: [93, 39],
|
|
119
|
-
blueBright: [94, 39],
|
|
120
|
-
magentaBright: [95, 39],
|
|
121
|
-
cyanBright: [96, 39],
|
|
122
|
-
whiteBright: [97, 39]
|
|
123
|
-
};
|
|
124
|
-
const bgColorCodes = {
|
|
125
|
-
bgBlack: [40, 49],
|
|
126
|
-
bgRed: [41, 49],
|
|
127
|
-
bgGreen: [42, 49],
|
|
128
|
-
bgYellow: [43, 49],
|
|
129
|
-
bgBlue: [44, 49],
|
|
130
|
-
bgMagenta: [45, 49],
|
|
131
|
-
bgCyan: [46, 49],
|
|
132
|
-
bgWhite: [47, 49],
|
|
133
|
-
bgBlackBright: [100, 49],
|
|
134
|
-
bgGray: [100, 49],
|
|
135
|
-
bgGrey: [100, 49],
|
|
136
|
-
bgRedBright: [101, 49],
|
|
137
|
-
bgGreenBright: [102, 49],
|
|
138
|
-
bgYellowBright: [103, 49],
|
|
139
|
-
bgBlueBright: [104, 49],
|
|
140
|
-
bgMagentaBright: [105, 49],
|
|
141
|
-
bgCyanBright: [106, 49],
|
|
142
|
-
bgWhiteBright: [107, 49]
|
|
143
|
-
};
|
|
144
|
-
const modifierNames = Object.keys(modifierCodes);
|
|
145
|
-
const foregroundColorNames = Object.keys(colorCodes);
|
|
146
|
-
const backgroundColorNames = Object.keys(bgColorCodes);
|
|
147
|
-
const toPairs = (codes) => {
|
|
148
|
-
const result = {};
|
|
149
|
-
for (const [name, [open, close]] of Object.entries(codes)) result[name] = {
|
|
150
|
-
open: `${CSI$1}${open}m`,
|
|
151
|
-
close: `${CSI$1}${close}m`
|
|
152
|
-
};
|
|
153
|
-
return result;
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
Named SGR styles as `{open, close}` escape sequence pairs.
|
|
157
|
-
*/
|
|
158
|
-
const styles = {
|
|
159
|
-
...toPairs(modifierCodes),
|
|
160
|
-
...toPairs(colorCodes),
|
|
161
|
-
...toPairs(bgColorCodes)
|
|
162
|
-
};
|
|
163
|
-
/**
|
|
164
|
-
Raw SGR code numbers: open code → close code.
|
|
165
|
-
*/
|
|
166
|
-
const codes = new Map([
|
|
167
|
-
...Object.values(modifierCodes),
|
|
168
|
-
...Object.values(colorCodes),
|
|
169
|
-
...Object.values(bgColorCodes)
|
|
170
|
-
].map(([open, close]) => [open, close]));
|
|
171
|
-
const foreground = {
|
|
172
|
-
close: `${CSI$1}39m`,
|
|
173
|
-
ansi: wrapAnsi16(),
|
|
174
|
-
ansi256: wrapAnsi256(),
|
|
175
|
-
ansi16m: wrapAnsi16m()
|
|
176
|
-
};
|
|
177
|
-
const background = {
|
|
178
|
-
close: `${CSI$1}49m`,
|
|
179
|
-
ansi: wrapAnsi16(ANSI_BACKGROUND_OFFSET),
|
|
180
|
-
ansi256: wrapAnsi256(ANSI_BACKGROUND_OFFSET),
|
|
181
|
-
ansi16m: wrapAnsi16m(ANSI_BACKGROUND_OFFSET)
|
|
182
|
-
};
|
|
183
|
-
const rgbToAnsi256 = (red, green, blue) => {
|
|
184
|
-
if (red === green && green === blue) {
|
|
185
|
-
if (red < 8) return 16;
|
|
186
|
-
if (red > 248) return 231;
|
|
187
|
-
return Math.round((red - 8) / 247 * 24) + 232;
|
|
188
|
-
}
|
|
189
|
-
return 16 + 36 * Math.round(red / 255 * 5) + 6 * Math.round(green / 255 * 5) + Math.round(blue / 255 * 5);
|
|
190
|
-
};
|
|
191
|
-
const hexToRgb = (hex) => {
|
|
192
|
-
const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex);
|
|
193
|
-
if (!matches) return [
|
|
194
|
-
0,
|
|
195
|
-
0,
|
|
196
|
-
0
|
|
197
|
-
];
|
|
198
|
-
let [colorString] = matches;
|
|
199
|
-
if (colorString.length === 3) colorString = colorString.replaceAll(/./g, "$&$&");
|
|
200
|
-
const integer = Number.parseInt(colorString, 16);
|
|
201
|
-
return [
|
|
202
|
-
integer >> 16 & 255,
|
|
203
|
-
integer >> 8 & 255,
|
|
204
|
-
integer & 255
|
|
205
|
-
];
|
|
206
|
-
};
|
|
207
|
-
const hexToAnsi256 = (hex) => rgbToAnsi256(...hexToRgb(hex));
|
|
208
|
-
const ansi256ToAnsi = (code) => {
|
|
209
|
-
if (code < 8) return 30 + code;
|
|
210
|
-
if (code < 16) return 90 + (code - 8);
|
|
211
|
-
let red;
|
|
212
|
-
let green;
|
|
213
|
-
let blue;
|
|
214
|
-
if (code >= 232) {
|
|
215
|
-
red = ((code - 232) * 10 + 8) / 255;
|
|
216
|
-
green = red;
|
|
217
|
-
blue = red;
|
|
218
|
-
} else {
|
|
219
|
-
code -= 16;
|
|
220
|
-
const remainder = code % 36;
|
|
221
|
-
red = Math.floor(code / 36) / 5;
|
|
222
|
-
green = Math.floor(remainder / 6) / 5;
|
|
223
|
-
blue = remainder % 6 / 5;
|
|
224
|
-
}
|
|
225
|
-
const value = Math.max(red, green, blue) * 2;
|
|
226
|
-
if (value === 0) return 30;
|
|
227
|
-
let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
|
|
228
|
-
if (value === 2) result += 60;
|
|
229
|
-
return result;
|
|
230
|
-
};
|
|
231
|
-
const rgbToAnsi = (red, green, blue) => ansi256ToAnsi(rgbToAnsi256(red, green, blue));
|
|
232
|
-
const hexToAnsi = (hex) => ansi256ToAnsi(hexToAnsi256(hex));
|
|
233
|
-
//#endregion
|
|
234
|
-
//#region src/ansi/east-asian-width.ts
|
|
235
|
-
const isInRange = (ranges, codePoint) => {
|
|
236
|
-
let low = 0;
|
|
237
|
-
let high = Math.floor(ranges.length / 2) - 1;
|
|
238
|
-
while (low <= high) {
|
|
239
|
-
const mid = Math.floor((low + high) / 2);
|
|
240
|
-
const i = mid * 2;
|
|
241
|
-
if (codePoint < ranges[i]) high = mid - 1;
|
|
242
|
-
else if (codePoint > ranges[i + 1]) low = mid + 1;
|
|
243
|
-
else return true;
|
|
244
|
-
}
|
|
245
|
-
return false;
|
|
246
|
-
};
|
|
247
|
-
const ambiguousMinimalCodePoint = 161;
|
|
248
|
-
const ambiguousMaximumCodePoint = 1114109;
|
|
249
|
-
const ambiguousRanges = [
|
|
250
|
-
161,
|
|
251
|
-
161,
|
|
252
|
-
164,
|
|
253
|
-
164,
|
|
254
|
-
167,
|
|
255
|
-
168,
|
|
256
|
-
170,
|
|
257
|
-
170,
|
|
258
|
-
173,
|
|
259
|
-
174,
|
|
260
|
-
176,
|
|
261
|
-
180,
|
|
262
|
-
182,
|
|
263
|
-
186,
|
|
264
|
-
188,
|
|
265
|
-
191,
|
|
266
|
-
198,
|
|
267
|
-
198,
|
|
268
|
-
208,
|
|
269
|
-
208,
|
|
270
|
-
215,
|
|
271
|
-
216,
|
|
272
|
-
222,
|
|
273
|
-
225,
|
|
274
|
-
230,
|
|
275
|
-
230,
|
|
276
|
-
232,
|
|
277
|
-
234,
|
|
278
|
-
236,
|
|
279
|
-
237,
|
|
280
|
-
240,
|
|
281
|
-
240,
|
|
282
|
-
242,
|
|
283
|
-
243,
|
|
284
|
-
247,
|
|
285
|
-
250,
|
|
286
|
-
252,
|
|
287
|
-
252,
|
|
288
|
-
254,
|
|
289
|
-
254,
|
|
290
|
-
257,
|
|
291
|
-
257,
|
|
292
|
-
273,
|
|
293
|
-
273,
|
|
294
|
-
275,
|
|
295
|
-
275,
|
|
296
|
-
283,
|
|
297
|
-
283,
|
|
298
|
-
294,
|
|
299
|
-
295,
|
|
300
|
-
299,
|
|
301
|
-
299,
|
|
302
|
-
305,
|
|
303
|
-
307,
|
|
304
|
-
312,
|
|
305
|
-
312,
|
|
306
|
-
319,
|
|
307
|
-
322,
|
|
308
|
-
324,
|
|
309
|
-
324,
|
|
310
|
-
328,
|
|
311
|
-
331,
|
|
312
|
-
333,
|
|
313
|
-
333,
|
|
314
|
-
338,
|
|
315
|
-
339,
|
|
316
|
-
358,
|
|
317
|
-
359,
|
|
318
|
-
363,
|
|
319
|
-
363,
|
|
320
|
-
462,
|
|
321
|
-
462,
|
|
322
|
-
464,
|
|
323
|
-
464,
|
|
324
|
-
466,
|
|
325
|
-
466,
|
|
326
|
-
468,
|
|
327
|
-
468,
|
|
328
|
-
470,
|
|
329
|
-
470,
|
|
330
|
-
472,
|
|
331
|
-
472,
|
|
332
|
-
474,
|
|
333
|
-
474,
|
|
334
|
-
476,
|
|
335
|
-
476,
|
|
336
|
-
593,
|
|
337
|
-
593,
|
|
338
|
-
609,
|
|
339
|
-
609,
|
|
340
|
-
708,
|
|
341
|
-
708,
|
|
342
|
-
711,
|
|
343
|
-
711,
|
|
344
|
-
713,
|
|
345
|
-
715,
|
|
346
|
-
717,
|
|
347
|
-
717,
|
|
348
|
-
720,
|
|
349
|
-
720,
|
|
350
|
-
728,
|
|
351
|
-
731,
|
|
352
|
-
733,
|
|
353
|
-
733,
|
|
354
|
-
735,
|
|
355
|
-
735,
|
|
356
|
-
768,
|
|
357
|
-
879,
|
|
358
|
-
913,
|
|
359
|
-
929,
|
|
360
|
-
931,
|
|
361
|
-
937,
|
|
362
|
-
945,
|
|
363
|
-
961,
|
|
364
|
-
963,
|
|
365
|
-
969,
|
|
366
|
-
1025,
|
|
367
|
-
1025,
|
|
368
|
-
1040,
|
|
369
|
-
1103,
|
|
370
|
-
1105,
|
|
371
|
-
1105,
|
|
372
|
-
8208,
|
|
373
|
-
8208,
|
|
374
|
-
8211,
|
|
375
|
-
8214,
|
|
376
|
-
8216,
|
|
377
|
-
8217,
|
|
378
|
-
8220,
|
|
379
|
-
8221,
|
|
380
|
-
8224,
|
|
381
|
-
8226,
|
|
382
|
-
8228,
|
|
383
|
-
8231,
|
|
384
|
-
8240,
|
|
385
|
-
8240,
|
|
386
|
-
8242,
|
|
387
|
-
8243,
|
|
388
|
-
8245,
|
|
389
|
-
8245,
|
|
390
|
-
8251,
|
|
391
|
-
8251,
|
|
392
|
-
8254,
|
|
393
|
-
8254,
|
|
394
|
-
8308,
|
|
395
|
-
8308,
|
|
396
|
-
8319,
|
|
397
|
-
8319,
|
|
398
|
-
8321,
|
|
399
|
-
8324,
|
|
400
|
-
8364,
|
|
401
|
-
8364,
|
|
402
|
-
8451,
|
|
403
|
-
8451,
|
|
404
|
-
8453,
|
|
405
|
-
8453,
|
|
406
|
-
8457,
|
|
407
|
-
8457,
|
|
408
|
-
8467,
|
|
409
|
-
8467,
|
|
410
|
-
8470,
|
|
411
|
-
8470,
|
|
412
|
-
8481,
|
|
413
|
-
8482,
|
|
414
|
-
8486,
|
|
415
|
-
8486,
|
|
416
|
-
8491,
|
|
417
|
-
8491,
|
|
418
|
-
8531,
|
|
419
|
-
8532,
|
|
420
|
-
8539,
|
|
421
|
-
8542,
|
|
422
|
-
8544,
|
|
423
|
-
8555,
|
|
424
|
-
8560,
|
|
425
|
-
8569,
|
|
426
|
-
8585,
|
|
427
|
-
8585,
|
|
428
|
-
8592,
|
|
429
|
-
8601,
|
|
430
|
-
8632,
|
|
431
|
-
8633,
|
|
432
|
-
8658,
|
|
433
|
-
8658,
|
|
434
|
-
8660,
|
|
435
|
-
8660,
|
|
436
|
-
8679,
|
|
437
|
-
8679,
|
|
438
|
-
8704,
|
|
439
|
-
8704,
|
|
440
|
-
8706,
|
|
441
|
-
8707,
|
|
442
|
-
8711,
|
|
443
|
-
8712,
|
|
444
|
-
8715,
|
|
445
|
-
8715,
|
|
446
|
-
8719,
|
|
447
|
-
8719,
|
|
448
|
-
8721,
|
|
449
|
-
8721,
|
|
450
|
-
8725,
|
|
451
|
-
8725,
|
|
452
|
-
8730,
|
|
453
|
-
8730,
|
|
454
|
-
8733,
|
|
455
|
-
8736,
|
|
456
|
-
8739,
|
|
457
|
-
8739,
|
|
458
|
-
8741,
|
|
459
|
-
8741,
|
|
460
|
-
8743,
|
|
461
|
-
8748,
|
|
462
|
-
8750,
|
|
463
|
-
8750,
|
|
464
|
-
8756,
|
|
465
|
-
8759,
|
|
466
|
-
8764,
|
|
467
|
-
8765,
|
|
468
|
-
8776,
|
|
469
|
-
8776,
|
|
470
|
-
8780,
|
|
471
|
-
8780,
|
|
472
|
-
8786,
|
|
473
|
-
8786,
|
|
474
|
-
8800,
|
|
475
|
-
8801,
|
|
476
|
-
8804,
|
|
477
|
-
8807,
|
|
478
|
-
8810,
|
|
479
|
-
8811,
|
|
480
|
-
8814,
|
|
481
|
-
8815,
|
|
482
|
-
8834,
|
|
483
|
-
8835,
|
|
484
|
-
8838,
|
|
485
|
-
8839,
|
|
486
|
-
8853,
|
|
487
|
-
8853,
|
|
488
|
-
8857,
|
|
489
|
-
8857,
|
|
490
|
-
8869,
|
|
491
|
-
8869,
|
|
492
|
-
8895,
|
|
493
|
-
8895,
|
|
494
|
-
8978,
|
|
495
|
-
8978,
|
|
496
|
-
9312,
|
|
497
|
-
9449,
|
|
498
|
-
9451,
|
|
499
|
-
9547,
|
|
500
|
-
9552,
|
|
501
|
-
9587,
|
|
502
|
-
9600,
|
|
503
|
-
9615,
|
|
504
|
-
9618,
|
|
505
|
-
9621,
|
|
506
|
-
9632,
|
|
507
|
-
9633,
|
|
508
|
-
9635,
|
|
509
|
-
9641,
|
|
510
|
-
9650,
|
|
511
|
-
9651,
|
|
512
|
-
9654,
|
|
513
|
-
9655,
|
|
514
|
-
9660,
|
|
515
|
-
9661,
|
|
516
|
-
9664,
|
|
517
|
-
9665,
|
|
518
|
-
9670,
|
|
519
|
-
9672,
|
|
520
|
-
9675,
|
|
521
|
-
9675,
|
|
522
|
-
9678,
|
|
523
|
-
9681,
|
|
524
|
-
9698,
|
|
525
|
-
9701,
|
|
526
|
-
9711,
|
|
527
|
-
9711,
|
|
528
|
-
9733,
|
|
529
|
-
9734,
|
|
530
|
-
9737,
|
|
531
|
-
9737,
|
|
532
|
-
9742,
|
|
533
|
-
9743,
|
|
534
|
-
9756,
|
|
535
|
-
9756,
|
|
536
|
-
9758,
|
|
537
|
-
9758,
|
|
538
|
-
9792,
|
|
539
|
-
9792,
|
|
540
|
-
9794,
|
|
541
|
-
9794,
|
|
542
|
-
9824,
|
|
543
|
-
9825,
|
|
544
|
-
9827,
|
|
545
|
-
9829,
|
|
546
|
-
9831,
|
|
547
|
-
9834,
|
|
548
|
-
9836,
|
|
549
|
-
9837,
|
|
550
|
-
9839,
|
|
551
|
-
9839,
|
|
552
|
-
9886,
|
|
553
|
-
9887,
|
|
554
|
-
9919,
|
|
555
|
-
9919,
|
|
556
|
-
9926,
|
|
557
|
-
9933,
|
|
558
|
-
9935,
|
|
559
|
-
9939,
|
|
560
|
-
9941,
|
|
561
|
-
9953,
|
|
562
|
-
9955,
|
|
563
|
-
9955,
|
|
564
|
-
9960,
|
|
565
|
-
9961,
|
|
566
|
-
9963,
|
|
567
|
-
9969,
|
|
568
|
-
9972,
|
|
569
|
-
9972,
|
|
570
|
-
9974,
|
|
571
|
-
9977,
|
|
572
|
-
9979,
|
|
573
|
-
9980,
|
|
574
|
-
9982,
|
|
575
|
-
9983,
|
|
576
|
-
10045,
|
|
577
|
-
10045,
|
|
578
|
-
10102,
|
|
579
|
-
10111,
|
|
580
|
-
11094,
|
|
581
|
-
11097,
|
|
582
|
-
12872,
|
|
583
|
-
12879,
|
|
584
|
-
57344,
|
|
585
|
-
63743,
|
|
586
|
-
65024,
|
|
587
|
-
65039,
|
|
588
|
-
65533,
|
|
589
|
-
65533,
|
|
590
|
-
127232,
|
|
591
|
-
127242,
|
|
592
|
-
127248,
|
|
593
|
-
127277,
|
|
594
|
-
127280,
|
|
595
|
-
127337,
|
|
596
|
-
127344,
|
|
597
|
-
127373,
|
|
598
|
-
127375,
|
|
599
|
-
127376,
|
|
600
|
-
127387,
|
|
601
|
-
127404,
|
|
602
|
-
917760,
|
|
603
|
-
917999,
|
|
604
|
-
983040,
|
|
605
|
-
1048573,
|
|
606
|
-
1048576,
|
|
607
|
-
1114109
|
|
608
|
-
];
|
|
609
|
-
const fullwidthMinimalCodePoint = 12288;
|
|
610
|
-
const fullwidthMaximumCodePoint = 65510;
|
|
611
|
-
const fullwidthRanges = [
|
|
612
|
-
12288,
|
|
613
|
-
12288,
|
|
614
|
-
65281,
|
|
615
|
-
65376,
|
|
616
|
-
65504,
|
|
617
|
-
65510
|
|
618
|
-
];
|
|
619
|
-
const halfwidthMinimalCodePoint = 8361;
|
|
620
|
-
const halfwidthMaximumCodePoint = 65518;
|
|
621
|
-
const halfwidthRanges = [
|
|
622
|
-
8361,
|
|
623
|
-
8361,
|
|
624
|
-
65377,
|
|
625
|
-
65470,
|
|
626
|
-
65474,
|
|
627
|
-
65479,
|
|
628
|
-
65482,
|
|
629
|
-
65487,
|
|
630
|
-
65490,
|
|
631
|
-
65495,
|
|
632
|
-
65498,
|
|
633
|
-
65500,
|
|
634
|
-
65512,
|
|
635
|
-
65518
|
|
636
|
-
];
|
|
637
|
-
const narrowMinimalCodePoint = 32;
|
|
638
|
-
const narrowMaximumCodePoint = 10630;
|
|
639
|
-
const narrowRanges = [
|
|
640
|
-
32,
|
|
641
|
-
126,
|
|
642
|
-
162,
|
|
643
|
-
163,
|
|
644
|
-
165,
|
|
645
|
-
166,
|
|
646
|
-
172,
|
|
647
|
-
172,
|
|
648
|
-
175,
|
|
649
|
-
175,
|
|
650
|
-
10214,
|
|
651
|
-
10221,
|
|
652
|
-
10629,
|
|
653
|
-
10630
|
|
654
|
-
];
|
|
655
|
-
const wideMinimalCodePoint = 4352;
|
|
656
|
-
const wideMaximumCodePoint = 262141;
|
|
657
|
-
const wideRanges = [
|
|
658
|
-
4352,
|
|
659
|
-
4447,
|
|
660
|
-
8986,
|
|
661
|
-
8987,
|
|
662
|
-
9001,
|
|
663
|
-
9002,
|
|
664
|
-
9193,
|
|
665
|
-
9196,
|
|
666
|
-
9200,
|
|
667
|
-
9200,
|
|
668
|
-
9203,
|
|
669
|
-
9203,
|
|
670
|
-
9725,
|
|
671
|
-
9726,
|
|
672
|
-
9748,
|
|
673
|
-
9749,
|
|
674
|
-
9776,
|
|
675
|
-
9783,
|
|
676
|
-
9800,
|
|
677
|
-
9811,
|
|
678
|
-
9855,
|
|
679
|
-
9855,
|
|
680
|
-
9866,
|
|
681
|
-
9871,
|
|
682
|
-
9875,
|
|
683
|
-
9875,
|
|
684
|
-
9889,
|
|
685
|
-
9889,
|
|
686
|
-
9898,
|
|
687
|
-
9899,
|
|
688
|
-
9917,
|
|
689
|
-
9918,
|
|
690
|
-
9924,
|
|
691
|
-
9925,
|
|
692
|
-
9934,
|
|
693
|
-
9934,
|
|
694
|
-
9940,
|
|
695
|
-
9940,
|
|
696
|
-
9962,
|
|
697
|
-
9962,
|
|
698
|
-
9970,
|
|
699
|
-
9971,
|
|
700
|
-
9973,
|
|
701
|
-
9973,
|
|
702
|
-
9978,
|
|
703
|
-
9978,
|
|
704
|
-
9981,
|
|
705
|
-
9981,
|
|
706
|
-
9989,
|
|
707
|
-
9989,
|
|
708
|
-
9994,
|
|
709
|
-
9995,
|
|
710
|
-
10024,
|
|
711
|
-
10024,
|
|
712
|
-
10060,
|
|
713
|
-
10060,
|
|
714
|
-
10062,
|
|
715
|
-
10062,
|
|
716
|
-
10067,
|
|
717
|
-
10069,
|
|
718
|
-
10071,
|
|
719
|
-
10071,
|
|
720
|
-
10133,
|
|
721
|
-
10135,
|
|
722
|
-
10160,
|
|
723
|
-
10160,
|
|
724
|
-
10175,
|
|
725
|
-
10175,
|
|
726
|
-
11035,
|
|
727
|
-
11036,
|
|
728
|
-
11088,
|
|
729
|
-
11088,
|
|
730
|
-
11093,
|
|
731
|
-
11093,
|
|
732
|
-
11904,
|
|
733
|
-
11929,
|
|
734
|
-
11931,
|
|
735
|
-
12019,
|
|
736
|
-
12032,
|
|
737
|
-
12245,
|
|
738
|
-
12272,
|
|
739
|
-
12287,
|
|
740
|
-
12289,
|
|
741
|
-
12350,
|
|
742
|
-
12353,
|
|
743
|
-
12438,
|
|
744
|
-
12441,
|
|
745
|
-
12543,
|
|
746
|
-
12549,
|
|
747
|
-
12591,
|
|
748
|
-
12593,
|
|
749
|
-
12686,
|
|
750
|
-
12688,
|
|
751
|
-
12773,
|
|
752
|
-
12783,
|
|
753
|
-
12830,
|
|
754
|
-
12832,
|
|
755
|
-
12871,
|
|
756
|
-
12880,
|
|
757
|
-
42124,
|
|
758
|
-
42128,
|
|
759
|
-
42182,
|
|
760
|
-
43360,
|
|
761
|
-
43388,
|
|
762
|
-
44032,
|
|
763
|
-
55203,
|
|
764
|
-
63744,
|
|
765
|
-
64255,
|
|
766
|
-
65040,
|
|
767
|
-
65049,
|
|
768
|
-
65072,
|
|
769
|
-
65106,
|
|
770
|
-
65108,
|
|
771
|
-
65126,
|
|
772
|
-
65128,
|
|
773
|
-
65131,
|
|
774
|
-
94176,
|
|
775
|
-
94180,
|
|
776
|
-
94192,
|
|
777
|
-
94198,
|
|
778
|
-
94208,
|
|
779
|
-
101589,
|
|
780
|
-
101631,
|
|
781
|
-
101662,
|
|
782
|
-
101760,
|
|
783
|
-
101874,
|
|
784
|
-
110576,
|
|
785
|
-
110579,
|
|
786
|
-
110581,
|
|
787
|
-
110587,
|
|
788
|
-
110589,
|
|
789
|
-
110590,
|
|
790
|
-
110592,
|
|
791
|
-
110882,
|
|
792
|
-
110898,
|
|
793
|
-
110898,
|
|
794
|
-
110928,
|
|
795
|
-
110930,
|
|
796
|
-
110933,
|
|
797
|
-
110933,
|
|
798
|
-
110948,
|
|
799
|
-
110951,
|
|
800
|
-
110960,
|
|
801
|
-
111355,
|
|
802
|
-
119552,
|
|
803
|
-
119638,
|
|
804
|
-
119648,
|
|
805
|
-
119670,
|
|
806
|
-
126980,
|
|
807
|
-
126980,
|
|
808
|
-
127183,
|
|
809
|
-
127183,
|
|
810
|
-
127374,
|
|
811
|
-
127374,
|
|
812
|
-
127377,
|
|
813
|
-
127386,
|
|
814
|
-
127488,
|
|
815
|
-
127490,
|
|
816
|
-
127504,
|
|
817
|
-
127547,
|
|
818
|
-
127552,
|
|
819
|
-
127560,
|
|
820
|
-
127568,
|
|
821
|
-
127569,
|
|
822
|
-
127584,
|
|
823
|
-
127589,
|
|
824
|
-
127744,
|
|
825
|
-
127776,
|
|
826
|
-
127789,
|
|
827
|
-
127797,
|
|
828
|
-
127799,
|
|
829
|
-
127868,
|
|
830
|
-
127870,
|
|
831
|
-
127891,
|
|
832
|
-
127904,
|
|
833
|
-
127946,
|
|
834
|
-
127951,
|
|
835
|
-
127955,
|
|
836
|
-
127968,
|
|
837
|
-
127984,
|
|
838
|
-
127988,
|
|
839
|
-
127988,
|
|
840
|
-
127992,
|
|
841
|
-
128062,
|
|
842
|
-
128064,
|
|
843
|
-
128064,
|
|
844
|
-
128066,
|
|
845
|
-
128252,
|
|
846
|
-
128255,
|
|
847
|
-
128317,
|
|
848
|
-
128331,
|
|
849
|
-
128334,
|
|
850
|
-
128336,
|
|
851
|
-
128359,
|
|
852
|
-
128378,
|
|
853
|
-
128378,
|
|
854
|
-
128405,
|
|
855
|
-
128406,
|
|
856
|
-
128420,
|
|
857
|
-
128420,
|
|
858
|
-
128507,
|
|
859
|
-
128591,
|
|
860
|
-
128640,
|
|
861
|
-
128709,
|
|
862
|
-
128716,
|
|
863
|
-
128716,
|
|
864
|
-
128720,
|
|
865
|
-
128722,
|
|
866
|
-
128725,
|
|
867
|
-
128728,
|
|
868
|
-
128732,
|
|
869
|
-
128735,
|
|
870
|
-
128747,
|
|
871
|
-
128748,
|
|
872
|
-
128756,
|
|
873
|
-
128764,
|
|
874
|
-
128992,
|
|
875
|
-
129003,
|
|
876
|
-
129008,
|
|
877
|
-
129008,
|
|
878
|
-
129292,
|
|
879
|
-
129338,
|
|
880
|
-
129340,
|
|
881
|
-
129349,
|
|
882
|
-
129351,
|
|
883
|
-
129535,
|
|
884
|
-
129648,
|
|
885
|
-
129660,
|
|
886
|
-
129664,
|
|
887
|
-
129674,
|
|
888
|
-
129678,
|
|
889
|
-
129734,
|
|
890
|
-
129736,
|
|
891
|
-
129736,
|
|
892
|
-
129741,
|
|
893
|
-
129756,
|
|
894
|
-
129759,
|
|
895
|
-
129770,
|
|
896
|
-
129775,
|
|
897
|
-
129784,
|
|
898
|
-
131072,
|
|
899
|
-
196605,
|
|
900
|
-
196608,
|
|
901
|
-
262141
|
|
902
|
-
];
|
|
903
|
-
const commonCjkCodePoint = 19968;
|
|
904
|
-
const [wideFastPathStart, wideFastPathEnd] = /* #__PURE__ */ findWideFastPathRange(wideRanges);
|
|
905
|
-
function findWideFastPathRange(ranges) {
|
|
906
|
-
let fastPathStart = ranges[0];
|
|
907
|
-
let fastPathEnd = ranges[1];
|
|
908
|
-
for (let index = 0; index < ranges.length; index += 2) {
|
|
909
|
-
const start = ranges[index];
|
|
910
|
-
const end = ranges[index + 1];
|
|
911
|
-
if (commonCjkCodePoint >= start && commonCjkCodePoint <= end) return [start, end];
|
|
912
|
-
if (end - start > fastPathEnd - fastPathStart) {
|
|
913
|
-
fastPathStart = start;
|
|
914
|
-
fastPathEnd = end;
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
return [fastPathStart, fastPathEnd];
|
|
918
|
-
}
|
|
919
|
-
const isAmbiguous = (codePoint) => {
|
|
920
|
-
if (codePoint < 161 || codePoint > 1114109) return false;
|
|
921
|
-
return isInRange(ambiguousRanges, codePoint);
|
|
922
|
-
};
|
|
923
|
-
const isFullWidth = (codePoint) => {
|
|
924
|
-
if (codePoint < 12288 || codePoint > 65510) return false;
|
|
925
|
-
return isInRange(fullwidthRanges, codePoint);
|
|
926
|
-
};
|
|
927
|
-
const isHalfWidth = (codePoint) => {
|
|
928
|
-
if (codePoint < 8361 || codePoint > 65518) return false;
|
|
929
|
-
return isInRange(halfwidthRanges, codePoint);
|
|
930
|
-
};
|
|
931
|
-
const isNarrow = (codePoint) => {
|
|
932
|
-
if (codePoint < 32 || codePoint > 10630) return false;
|
|
933
|
-
return isInRange(narrowRanges, codePoint);
|
|
934
|
-
};
|
|
935
|
-
const isWide = (codePoint) => {
|
|
936
|
-
if (codePoint >= wideFastPathStart && codePoint <= wideFastPathEnd) return true;
|
|
937
|
-
if (codePoint < 4352 || codePoint > 262141) return false;
|
|
938
|
-
return isInRange(wideRanges, codePoint);
|
|
939
|
-
};
|
|
940
|
-
function getCategory(codePoint) {
|
|
941
|
-
if (isAmbiguous(codePoint)) return "ambiguous";
|
|
942
|
-
if (isFullWidth(codePoint)) return "fullwidth";
|
|
943
|
-
if (isHalfWidth(codePoint)) return "halfwidth";
|
|
944
|
-
if (isNarrow(codePoint)) return "narrow";
|
|
945
|
-
if (isWide(codePoint)) return "wide";
|
|
946
|
-
return "neutral";
|
|
947
|
-
}
|
|
948
|
-
function eastAsianWidthType(codePoint) {
|
|
949
|
-
return getCategory(codePoint);
|
|
950
|
-
}
|
|
951
|
-
function eastAsianWidth(codePoint, { ambiguousAsWide = false } = {}) {
|
|
952
|
-
if (isFullWidth(codePoint) || isWide(codePoint) || ambiguousAsWide && isAmbiguous(codePoint)) return 2;
|
|
953
|
-
return 1;
|
|
954
|
-
}
|
|
955
|
-
/**
|
|
956
|
-
Grapheme-cluster width test shared by the tokenizer (slice/clip path): the
|
|
957
|
-
base code point's East Asian Width, plus emoji presentation rules.
|
|
958
|
-
*/
|
|
959
|
-
function isFullwidthGrapheme(grapheme, baseCodePoint) {
|
|
960
|
-
if (isFullwidthCodePoint(baseCodePoint)) return true;
|
|
961
|
-
if (grapheme.includes("️")) return true;
|
|
962
|
-
if (baseCodePoint >= 127462 && baseCodePoint <= 127487) return true;
|
|
963
|
-
return false;
|
|
964
|
-
}
|
|
965
|
-
/**
|
|
966
|
-
Wide code points not covered by string-width's CJKT script regex or emoji
|
|
967
|
-
regex — the fallback bucket of its block parser. A disjoint decomposition of
|
|
968
|
-
the `wide` table above, kept alongside it so the width data lives in one
|
|
969
|
-
module.
|
|
970
|
-
*/
|
|
971
|
-
const isWideNotCJKTNotEmoji = (x) => {
|
|
972
|
-
return x === 8987 || x === 9001 || x >= 12272 && x <= 12287 || x >= 12289 && x <= 12350 || x >= 12441 && x <= 12543 || x >= 12549 && x <= 12591 || x >= 12593 && x <= 12686 || x >= 12688 && x <= 12771 || x >= 12783 && x <= 12830 || x >= 12832 && x <= 12871 || x >= 12880 && x <= 19903 || x >= 65040 && x <= 65049 || x >= 65072 && x <= 65106 || x >= 65108 && x <= 65126 || x >= 65128 && x <= 65131 || x >= 127488 && x <= 127490 || x >= 127504 && x <= 127547 || x >= 127552 && x <= 127560 || x >= 131072 && x <= 196605 || x >= 196608 && x <= 262141;
|
|
973
|
-
};
|
|
974
|
-
function isFullwidthCodePoint(codePoint) {
|
|
975
|
-
if (!Number.isInteger(codePoint)) return false;
|
|
976
|
-
return isFullWidth(codePoint) || isWide(codePoint);
|
|
977
|
-
}
|
|
978
|
-
//#endregion
|
|
979
|
-
//#region src/ansi/string-width.ts
|
|
980
|
-
const getCodePointsLength = (input) => {
|
|
981
|
-
let length = 0;
|
|
982
|
-
for (const _ of input) length += 1;
|
|
983
|
-
return length;
|
|
984
|
-
};
|
|
985
|
-
const ANSI_RE = /[\u001b\u009b][[()#;?]*[0-9;:]*[0-9A-ORZcf-nqry=><]|\u001b\]8;[^;]*;.*?(?:\u0007|\u001b\u005c)/y;
|
|
986
|
-
const CONTROL_RE = /[\x00-\x08\x0A-\x1F\x7F-\x9F]{1,1000}/y;
|
|
987
|
-
const CJKT_WIDE_RE = /(?:(?![\uFF61-\uFF9F\uFF00-\uFFEF])[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Tangut}]){1,1000}/uy;
|
|
988
|
-
const TAB_RE = /\t{1,1000}/y;
|
|
989
|
-
const EMOJI_RE = /[\u{1F1E6}-\u{1F1FF}]{2}|\u{1F3F4}[\u{E0061}-\u{E007A}]{2}[\u{E0030}-\u{E0039}\u{E0061}-\u{E007A}]{1,3}\u{E007F}|(?:\p{Emoji}\uFE0F\u20E3?|\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation})(?:\u200D(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F\u20E3?))*/uy;
|
|
990
|
-
const LATIN_RE = /(?:[\x20-\x7E\xA0-\xFF](?!\uFE0F)){1,1000}/y;
|
|
991
|
-
const MODIFIER_RE = /\p{M}+/gu;
|
|
992
|
-
const ANSI_WIDTH = 0;
|
|
993
|
-
const CONTROL_WIDTH = 0;
|
|
994
|
-
const TAB_WIDTH = 8;
|
|
995
|
-
const EMOJI_WIDTH = 2;
|
|
996
|
-
const FULL_WIDTH_WIDTH = 2;
|
|
997
|
-
const REGULAR_WIDTH = 1;
|
|
998
|
-
const WIDE_WIDTH = 2;
|
|
999
|
-
const PARSE_BLOCKS = [
|
|
1000
|
-
[LATIN_RE, REGULAR_WIDTH],
|
|
1001
|
-
[ANSI_RE, ANSI_WIDTH],
|
|
1002
|
-
[CONTROL_RE, CONTROL_WIDTH],
|
|
1003
|
-
[TAB_RE, TAB_WIDTH],
|
|
1004
|
-
[EMOJI_RE, EMOJI_WIDTH],
|
|
1005
|
-
[CJKT_WIDE_RE, WIDE_WIDTH]
|
|
1006
|
-
];
|
|
1007
|
-
const stringWidth = (input) => {
|
|
1008
|
-
let indexPrev = 0;
|
|
1009
|
-
let index = 0;
|
|
1010
|
-
const length = input.length;
|
|
1011
|
-
let unmatchedStart = 0;
|
|
1012
|
-
let unmatchedEnd = 0;
|
|
1013
|
-
let width = 0;
|
|
1014
|
-
outer: while (true) {
|
|
1015
|
-
if (unmatchedEnd > unmatchedStart || index >= length && index > indexPrev) {
|
|
1016
|
-
const unmatched = input.slice(unmatchedStart, unmatchedEnd) || input.slice(indexPrev, index);
|
|
1017
|
-
for (const char of unmatched.replaceAll(MODIFIER_RE, "")) {
|
|
1018
|
-
const codePoint = char.codePointAt(0) || 0;
|
|
1019
|
-
width += isFullWidth(codePoint) ? FULL_WIDTH_WIDTH : isWideNotCJKTNotEmoji(codePoint) ? WIDE_WIDTH : REGULAR_WIDTH;
|
|
1020
|
-
}
|
|
1021
|
-
unmatchedStart = unmatchedEnd = 0;
|
|
1022
|
-
}
|
|
1023
|
-
if (index >= length) break;
|
|
1024
|
-
for (let i = 0, l = PARSE_BLOCKS.length; i < l; i++) {
|
|
1025
|
-
const [BLOCK_RE, BLOCK_WIDTH] = PARSE_BLOCKS[i];
|
|
1026
|
-
BLOCK_RE.lastIndex = index;
|
|
1027
|
-
if (BLOCK_RE.test(input)) {
|
|
1028
|
-
const lengthExtra = BLOCK_RE === CJKT_WIDE_RE ? getCodePointsLength(input.slice(index, BLOCK_RE.lastIndex)) : BLOCK_RE === EMOJI_RE ? 1 : BLOCK_RE.lastIndex - index;
|
|
1029
|
-
width += lengthExtra * BLOCK_WIDTH;
|
|
1030
|
-
unmatchedStart = indexPrev;
|
|
1031
|
-
unmatchedEnd = index;
|
|
1032
|
-
index = indexPrev = BLOCK_RE.lastIndex;
|
|
1033
|
-
continue outer;
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
index += 1;
|
|
1037
|
-
}
|
|
1038
|
-
return width;
|
|
1039
|
-
};
|
|
1040
|
-
const widestLine = (text) => {
|
|
1041
|
-
let lineWidth = 0;
|
|
1042
|
-
for (const line of text.split("\n")) lineWidth = Math.max(lineWidth, stringWidth(line));
|
|
1043
|
-
return lineWidth;
|
|
1044
|
-
};
|
|
1045
|
-
//#endregion
|
|
1046
|
-
//#region src/ansi/wrap.ts
|
|
1047
|
-
const ANSI_ESCAPE = "\x1B";
|
|
1048
|
-
const ANSI_ESCAPE_BELL = "\x07";
|
|
1049
|
-
const ANSI_CSI = "[";
|
|
1050
|
-
const ANSI_OSC = "]";
|
|
1051
|
-
const ANSI_SGR_TERMINATOR = "m";
|
|
1052
|
-
const ANSI_SGR_RESET = 0;
|
|
1053
|
-
const ANSI_SGR_RESET_FOREGROUND = 39;
|
|
1054
|
-
const ANSI_SGR_RESET_BACKGROUND = 49;
|
|
1055
|
-
const ANSI_SGR_RESET_UNDERLINE_COLOR = 59;
|
|
1056
|
-
const ANSI_SGR_FOREGROUND_EXTENDED = 38;
|
|
1057
|
-
const ANSI_SGR_BACKGROUND_EXTENDED = 48;
|
|
1058
|
-
const ANSI_SGR_UNDERLINE_COLOR_EXTENDED = 58;
|
|
1059
|
-
const ANSI_SGR_COLOR_MODE_RGB = 2;
|
|
1060
|
-
const ANSI_SGR_COLOR_MODE_256 = 5;
|
|
1061
|
-
const ANSI_ESCAPE_LINK = `${ANSI_OSC}8;`;
|
|
1062
|
-
const ESCAPES = /* @__PURE__ */ new Set([ANSI_ESCAPE, ""]);
|
|
1063
|
-
const ESCAPE_CHARACTERS = [...ESCAPES].join("");
|
|
1064
|
-
const CSI_INTRODUCER = `(?:${ANSI_ESCAPE}\\${ANSI_CSI}|)`;
|
|
1065
|
-
const CSI_PARAMETERS = "[0-?]*[ -/]*[@-~]";
|
|
1066
|
-
const SGR_PARAMETERS = `(?<sgr>[0-9;:]*)${ANSI_SGR_TERMINATOR}`;
|
|
1067
|
-
const OSC_STRING_TERMINATOR = `(?:${ANSI_ESCAPE_BELL}|${ANSI_ESCAPE}\\\\)`;
|
|
1068
|
-
const OSC_STRING_PAYLOAD = String.raw`[^\u0000-\u001F\u007F-\u009F]*`;
|
|
1069
|
-
const LINK_PARAMETERS = String.raw`8;(?<parameters>[^;\u0000-\u001F\u007F-\u009F]*);(?<uri>${OSC_STRING_PAYLOAD})${OSC_STRING_TERMINATOR}`;
|
|
1070
|
-
const OSC_STRING = `${OSC_STRING_PAYLOAD}${OSC_STRING_TERMINATOR}`;
|
|
1071
|
-
const ANSI_ESCAPE_REGEX = new RegExp(`${CSI_INTRODUCER}(?:${SGR_PARAMETERS}|${CSI_PARAMETERS})|${ANSI_ESCAPE}\\${ANSI_OSC}(?:${LINK_PARAMETERS}|${OSC_STRING})`, "y");
|
|
1072
|
-
const ANSI_SGR_MODIFIER_CLOSE_CODES = new Set(codes.values());
|
|
1073
|
-
ANSI_SGR_MODIFIER_CLOSE_CODES.delete(ANSI_SGR_RESET);
|
|
1074
|
-
const segmenter$1 = new Intl.Segmenter();
|
|
1075
|
-
const getStringWidth = (string) => stringWidth(string);
|
|
1076
|
-
const TAB_SIZE = 8;
|
|
1077
|
-
const ESCAPE_INTRODUCER_REGEX = new RegExp(`[${ESCAPE_CHARACTERS}]`, "g");
|
|
1078
|
-
const ROW_BOUNDARY_REGEX = new RegExp(`[\\n${ESCAPE_CHARACTERS}]`, "g");
|
|
1079
|
-
const ASCII_PRINTABLE_REGEX = /^[ -~]*$/;
|
|
1080
|
-
const wrapAnsiCode = (code) => `${ANSI_ESCAPE}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
|
|
1081
|
-
const wrapAnsiHyperlink = (url, parameters = "") => `${ANSI_ESCAPE}${ANSI_ESCAPE_LINK}${parameters};${url}${ANSI_ESCAPE_BELL}`;
|
|
1082
|
-
const matchAnsiEscape = (string, index) => {
|
|
1083
|
-
if (!ESCAPES.has(string[index])) return;
|
|
1084
|
-
ANSI_ESCAPE_REGEX.lastIndex = index;
|
|
1085
|
-
return ANSI_ESCAPE_REGEX.exec(string) ?? void 0;
|
|
1086
|
-
};
|
|
1087
|
-
const forEachSegment = (string, onPlainText, onEscape = () => {}) => {
|
|
1088
|
-
let plainStart = 0;
|
|
1089
|
-
let index = 0;
|
|
1090
|
-
while (index < string.length) {
|
|
1091
|
-
ESCAPE_INTRODUCER_REGEX.lastIndex = index;
|
|
1092
|
-
const introducer = ESCAPE_INTRODUCER_REGEX.exec(string);
|
|
1093
|
-
if (!introducer) break;
|
|
1094
|
-
const escape = matchAnsiEscape(string, introducer.index);
|
|
1095
|
-
if (!escape) {
|
|
1096
|
-
index = introducer.index + 1;
|
|
1097
|
-
continue;
|
|
1098
|
-
}
|
|
1099
|
-
if (introducer.index > plainStart) onPlainText(string.slice(plainStart, introducer.index));
|
|
1100
|
-
onEscape(escape[0]);
|
|
1101
|
-
index = introducer.index + escape[0].length;
|
|
1102
|
-
plainStart = index;
|
|
1103
|
-
}
|
|
1104
|
-
if (plainStart < string.length) onPlainText(string.slice(plainStart));
|
|
1105
|
-
};
|
|
1106
|
-
const getWidth = (string) => {
|
|
1107
|
-
let plainText = "";
|
|
1108
|
-
forEachSegment(string, (part) => {
|
|
1109
|
-
plainText += part;
|
|
1110
|
-
});
|
|
1111
|
-
return getStringWidth(plainText);
|
|
1112
|
-
};
|
|
1113
|
-
const getTokens = (string) => {
|
|
1114
|
-
const tokens = [];
|
|
1115
|
-
forEachSegment(string, (plainText) => {
|
|
1116
|
-
if (ASCII_PRINTABLE_REGEX.test(plainText)) {
|
|
1117
|
-
for (const character of plainText) tokens.push({
|
|
1118
|
-
value: character,
|
|
1119
|
-
width: 1
|
|
1120
|
-
});
|
|
1121
|
-
return;
|
|
1122
|
-
}
|
|
1123
|
-
for (const { segment } of segmenter$1.segment(plainText)) tokens.push({
|
|
1124
|
-
value: segment,
|
|
1125
|
-
width: getStringWidth(segment)
|
|
1126
|
-
});
|
|
1127
|
-
}, (escape) => {
|
|
1128
|
-
tokens.push({
|
|
1129
|
-
value: escape,
|
|
1130
|
-
width: 0
|
|
1131
|
-
});
|
|
1132
|
-
});
|
|
1133
|
-
return tokens;
|
|
1134
|
-
};
|
|
1135
|
-
const splitWords = (string) => {
|
|
1136
|
-
let currentWord = {
|
|
1137
|
-
value: "",
|
|
1138
|
-
plainText: "",
|
|
1139
|
-
width: 0
|
|
1140
|
-
};
|
|
1141
|
-
const words = [currentWord];
|
|
1142
|
-
forEachSegment(string, (plainText) => {
|
|
1143
|
-
const parts = plainText.split(" ");
|
|
1144
|
-
currentWord.value += parts[0];
|
|
1145
|
-
currentWord.plainText += parts[0];
|
|
1146
|
-
for (let index = 1; index < parts.length; index++) {
|
|
1147
|
-
currentWord = {
|
|
1148
|
-
value: parts[index],
|
|
1149
|
-
plainText: parts[index],
|
|
1150
|
-
width: 0
|
|
1151
|
-
};
|
|
1152
|
-
words.push(currentWord);
|
|
1153
|
-
}
|
|
1154
|
-
}, (escape) => {
|
|
1155
|
-
currentWord.value += escape;
|
|
1156
|
-
});
|
|
1157
|
-
for (const word of words) word.width = getStringWidth(word.plainText);
|
|
1158
|
-
return words;
|
|
1159
|
-
};
|
|
1160
|
-
const EXTENDED_COLOR_CODES = /* @__PURE__ */ new Set([
|
|
1161
|
-
ANSI_SGR_FOREGROUND_EXTENDED,
|
|
1162
|
-
ANSI_SGR_BACKGROUND_EXTENDED,
|
|
1163
|
-
ANSI_SGR_UNDERLINE_COLOR_EXTENDED
|
|
1164
|
-
]);
|
|
1165
|
-
const getColonColorToken = (parameter) => {
|
|
1166
|
-
const parts = parameter.split(":");
|
|
1167
|
-
const code = Number.parseInt(parts[0], 10);
|
|
1168
|
-
const mode = Number.parseInt(parts[1], 10);
|
|
1169
|
-
if (!EXTENDED_COLOR_CODES.has(code)) return;
|
|
1170
|
-
if (mode === ANSI_SGR_COLOR_MODE_256 && parts.length === 3 && /^\d+$/.test(parts[2])) return {
|
|
1171
|
-
code,
|
|
1172
|
-
open: parameter,
|
|
1173
|
-
hasArguments: true
|
|
1174
|
-
};
|
|
1175
|
-
if (mode !== ANSI_SGR_COLOR_MODE_RGB) return;
|
|
1176
|
-
const components = parts.length === 6 ? parts.slice(3) : parts.slice(2);
|
|
1177
|
-
const colorSpace = parts.length === 6 ? parts[2] : void 0;
|
|
1178
|
-
if (components.length === 3 && components.every((component) => /^\d+$/.test(component)) && (colorSpace === void 0 || /^\d*$/.test(colorSpace))) return {
|
|
1179
|
-
code,
|
|
1180
|
-
open: parameter,
|
|
1181
|
-
hasArguments: true
|
|
1182
|
-
};
|
|
1183
|
-
};
|
|
1184
|
-
const getSgrTokens = (sgrParameters) => {
|
|
1185
|
-
const parameters = sgrParameters.split(";");
|
|
1186
|
-
const sgrTokens = [];
|
|
1187
|
-
for (let index = 0; index < parameters.length; index++) {
|
|
1188
|
-
const parameter = parameters[index];
|
|
1189
|
-
if (parameter.includes(":")) {
|
|
1190
|
-
const colonColorToken = getColonColorToken(parameter);
|
|
1191
|
-
if (colonColorToken) sgrTokens.push(colonColorToken);
|
|
1192
|
-
continue;
|
|
1193
|
-
}
|
|
1194
|
-
const code = parameter === "" ? ANSI_SGR_RESET : Number.parseInt(parameter, 10);
|
|
1195
|
-
if (!Number.isFinite(code)) continue;
|
|
1196
|
-
if (code === ANSI_SGR_FOREGROUND_EXTENDED || code === ANSI_SGR_BACKGROUND_EXTENDED || code === ANSI_SGR_UNDERLINE_COLOR_EXTENDED) {
|
|
1197
|
-
if (index + 1 >= parameters.length) break;
|
|
1198
|
-
const mode = Number.parseInt(parameters[index + 1], 10);
|
|
1199
|
-
const colorIndex = Number.parseInt(parameters[index + 2] ?? "", 10);
|
|
1200
|
-
if (mode === ANSI_SGR_COLOR_MODE_256 && Number.isFinite(colorIndex)) {
|
|
1201
|
-
sgrTokens.push({
|
|
1202
|
-
code,
|
|
1203
|
-
open: [
|
|
1204
|
-
code,
|
|
1205
|
-
mode,
|
|
1206
|
-
colorIndex
|
|
1207
|
-
].join(";"),
|
|
1208
|
-
hasArguments: true
|
|
1209
|
-
});
|
|
1210
|
-
index += 2;
|
|
1211
|
-
continue;
|
|
1212
|
-
}
|
|
1213
|
-
const red = Number.parseInt(parameters[index + 2] ?? "", 10);
|
|
1214
|
-
const green = Number.parseInt(parameters[index + 3] ?? "", 10);
|
|
1215
|
-
const blue = Number.parseInt(parameters[index + 4] ?? "", 10);
|
|
1216
|
-
if (mode === ANSI_SGR_COLOR_MODE_RGB && Number.isFinite(red) && Number.isFinite(green) && Number.isFinite(blue)) {
|
|
1217
|
-
sgrTokens.push({
|
|
1218
|
-
code,
|
|
1219
|
-
open: [
|
|
1220
|
-
code,
|
|
1221
|
-
mode,
|
|
1222
|
-
red,
|
|
1223
|
-
green,
|
|
1224
|
-
blue
|
|
1225
|
-
].join(";"),
|
|
1226
|
-
hasArguments: true
|
|
1227
|
-
});
|
|
1228
|
-
index += 4;
|
|
1229
|
-
continue;
|
|
1230
|
-
}
|
|
1231
|
-
break;
|
|
1232
|
-
}
|
|
1233
|
-
sgrTokens.push({
|
|
1234
|
-
code,
|
|
1235
|
-
open: String(code),
|
|
1236
|
-
hasArguments: false
|
|
1237
|
-
});
|
|
1238
|
-
}
|
|
1239
|
-
return sgrTokens;
|
|
1240
|
-
};
|
|
1241
|
-
const removeActiveStyle = (activeStyles, family) => {
|
|
1242
|
-
const activeStyleIndex = activeStyles.findIndex((activeStyle) => activeStyle.family === family);
|
|
1243
|
-
if (activeStyleIndex !== -1) activeStyles.splice(activeStyleIndex, 1);
|
|
1244
|
-
};
|
|
1245
|
-
const upsertActiveStyle = (activeStyles, nextActiveStyle) => {
|
|
1246
|
-
removeActiveStyle(activeStyles, nextActiveStyle.family);
|
|
1247
|
-
activeStyles.push(nextActiveStyle);
|
|
1248
|
-
};
|
|
1249
|
-
const removeModifierStylesByClose = (activeStyles, closeCode) => {
|
|
1250
|
-
for (let index = activeStyles.length - 1; index >= 0; index--) {
|
|
1251
|
-
const activeStyle = activeStyles[index];
|
|
1252
|
-
if (activeStyle.family.startsWith("modifier-") && activeStyle.close === closeCode) activeStyles.splice(index, 1);
|
|
1253
|
-
}
|
|
1254
|
-
};
|
|
1255
|
-
const getColorStyle = (sgrToken) => {
|
|
1256
|
-
const { code, open, hasArguments } = sgrToken;
|
|
1257
|
-
if (code >= 30 && code <= 37 || code >= 90 && code <= 97 || code === ANSI_SGR_FOREGROUND_EXTENDED && hasArguments) return {
|
|
1258
|
-
family: "foreground",
|
|
1259
|
-
open,
|
|
1260
|
-
close: ANSI_SGR_RESET_FOREGROUND
|
|
1261
|
-
};
|
|
1262
|
-
if (code >= 40 && code <= 47 || code >= 100 && code <= 107 || code === ANSI_SGR_BACKGROUND_EXTENDED && hasArguments) return {
|
|
1263
|
-
family: "background",
|
|
1264
|
-
open,
|
|
1265
|
-
close: ANSI_SGR_RESET_BACKGROUND
|
|
1266
|
-
};
|
|
1267
|
-
if (code === ANSI_SGR_UNDERLINE_COLOR_EXTENDED && hasArguments) return {
|
|
1268
|
-
family: "underlineColor",
|
|
1269
|
-
open,
|
|
1270
|
-
close: ANSI_SGR_RESET_UNDERLINE_COLOR
|
|
1271
|
-
};
|
|
1272
|
-
};
|
|
1273
|
-
const applySgrResetCode = (code, activeStyles) => {
|
|
1274
|
-
if (code === ANSI_SGR_RESET) {
|
|
1275
|
-
activeStyles.length = 0;
|
|
1276
|
-
return true;
|
|
1277
|
-
}
|
|
1278
|
-
if (code === ANSI_SGR_RESET_FOREGROUND) {
|
|
1279
|
-
removeActiveStyle(activeStyles, "foreground");
|
|
1280
|
-
return true;
|
|
1281
|
-
}
|
|
1282
|
-
if (code === ANSI_SGR_RESET_BACKGROUND) {
|
|
1283
|
-
removeActiveStyle(activeStyles, "background");
|
|
1284
|
-
return true;
|
|
1285
|
-
}
|
|
1286
|
-
if (code === ANSI_SGR_RESET_UNDERLINE_COLOR) {
|
|
1287
|
-
removeActiveStyle(activeStyles, "underlineColor");
|
|
1288
|
-
return true;
|
|
1289
|
-
}
|
|
1290
|
-
if (ANSI_SGR_MODIFIER_CLOSE_CODES.has(code)) {
|
|
1291
|
-
removeModifierStylesByClose(activeStyles, code);
|
|
1292
|
-
return true;
|
|
1293
|
-
}
|
|
1294
|
-
return false;
|
|
1295
|
-
};
|
|
1296
|
-
const applySgrToken = (sgrToken, activeStyles) => {
|
|
1297
|
-
const { code } = sgrToken;
|
|
1298
|
-
if (applySgrResetCode(code, activeStyles)) return;
|
|
1299
|
-
const colorStyle = getColorStyle(sgrToken);
|
|
1300
|
-
if (colorStyle) {
|
|
1301
|
-
upsertActiveStyle(activeStyles, colorStyle);
|
|
1302
|
-
return;
|
|
1303
|
-
}
|
|
1304
|
-
const close = codes.get(code);
|
|
1305
|
-
if (close !== void 0 && close !== ANSI_SGR_RESET) upsertActiveStyle(activeStyles, {
|
|
1306
|
-
family: `modifier-${code}`,
|
|
1307
|
-
open: sgrToken.open,
|
|
1308
|
-
close
|
|
1309
|
-
});
|
|
1310
|
-
};
|
|
1311
|
-
const applySgrParameters = (sgrParameters, activeStyles) => {
|
|
1312
|
-
for (const sgrToken of getSgrTokens(sgrParameters)) applySgrToken(sgrToken, activeStyles);
|
|
1313
|
-
};
|
|
1314
|
-
const applySgrResets = (sgrParameters, activeStyles) => {
|
|
1315
|
-
for (const { code } of getSgrTokens(sgrParameters)) applySgrResetCode(code, activeStyles);
|
|
1316
|
-
};
|
|
1317
|
-
const applyLeadingSgrResets = (string, startIndex, activeStyles) => {
|
|
1318
|
-
let index = startIndex;
|
|
1319
|
-
while (index < string.length) {
|
|
1320
|
-
const match = matchAnsiEscape(string, index);
|
|
1321
|
-
if (!match) break;
|
|
1322
|
-
if (match.groups?.["sgr"] !== void 0) applySgrResets(match.groups["sgr"], activeStyles);
|
|
1323
|
-
index += match[0].length;
|
|
1324
|
-
}
|
|
1325
|
-
};
|
|
1326
|
-
const getClosingSgrSequence = (activeStyles) => activeStyles.toReversed().map((activeStyle) => wrapAnsiCode(activeStyle.close)).join("");
|
|
1327
|
-
const getOpeningSgrSequence = (activeStyles) => activeStyles.map((activeStyle) => wrapAnsiCode(activeStyle.open)).join("");
|
|
1328
|
-
const wrapWord = (rows, word, columns, rowWidth) => {
|
|
1329
|
-
const tokens = getTokens(word);
|
|
1330
|
-
let visible = rowWidth;
|
|
1331
|
-
for (let index = 0; index < tokens.length; index++) {
|
|
1332
|
-
const token = tokens[index];
|
|
1333
|
-
if (token.width > 0 && visible > 0 && visible + token.width > columns) {
|
|
1334
|
-
rows.push("");
|
|
1335
|
-
visible = 0;
|
|
1336
|
-
}
|
|
1337
|
-
rows[rows.length - 1] += token.value;
|
|
1338
|
-
visible += token.width;
|
|
1339
|
-
if (visible === columns && index < tokens.length - 1) {
|
|
1340
|
-
rows.push("");
|
|
1341
|
-
visible = 0;
|
|
1342
|
-
}
|
|
1343
|
-
}
|
|
1344
|
-
if (!visible && rows.at(-1).length > 0 && rows.length > 1) {
|
|
1345
|
-
const lastRow = rows.pop();
|
|
1346
|
-
rows[rows.length - 1] = rows[rows.length - 1] + lastRow;
|
|
1347
|
-
}
|
|
1348
|
-
return getWidth(rows.at(-1));
|
|
1349
|
-
};
|
|
1350
|
-
const stringVisibleTrimSpacesRight = (string) => {
|
|
1351
|
-
if (!string.includes(" ")) return string;
|
|
1352
|
-
const segments = [];
|
|
1353
|
-
forEachSegment(string, (plainText) => {
|
|
1354
|
-
segments.push({
|
|
1355
|
-
value: plainText,
|
|
1356
|
-
isEscape: false
|
|
1357
|
-
});
|
|
1358
|
-
}, (escape) => {
|
|
1359
|
-
segments.push({
|
|
1360
|
-
value: escape,
|
|
1361
|
-
isEscape: true
|
|
1362
|
-
});
|
|
1363
|
-
});
|
|
1364
|
-
for (let index = segments.length - 1; index >= 0; index--) {
|
|
1365
|
-
const segment = segments[index];
|
|
1366
|
-
if (segment.isEscape) continue;
|
|
1367
|
-
let end = segment.value.length;
|
|
1368
|
-
while (end > 0 && segment.value[end - 1] === " ") end--;
|
|
1369
|
-
segment.value = segment.value.slice(0, end);
|
|
1370
|
-
if (getStringWidth(segment.value) > 0) break;
|
|
1371
|
-
}
|
|
1372
|
-
return segments.map((segment) => segment.value).join("");
|
|
1373
|
-
};
|
|
1374
|
-
const expandTabs = (line) => {
|
|
1375
|
-
if (!line.includes(" ")) return line;
|
|
1376
|
-
let visible = 0;
|
|
1377
|
-
let expandedLine = "";
|
|
1378
|
-
let plainTextSinceTab = "";
|
|
1379
|
-
const expandPlainText = (plainText) => {
|
|
1380
|
-
const segments = plainText.split(" ");
|
|
1381
|
-
for (const [index, segment] of segments.entries()) {
|
|
1382
|
-
expandedLine += segment;
|
|
1383
|
-
plainTextSinceTab += segment;
|
|
1384
|
-
if (index < segments.length - 1) {
|
|
1385
|
-
visible += getStringWidth(plainTextSinceTab);
|
|
1386
|
-
plainTextSinceTab = "";
|
|
1387
|
-
const spaces = TAB_SIZE - visible % TAB_SIZE;
|
|
1388
|
-
expandedLine += " ".repeat(spaces);
|
|
1389
|
-
visible += spaces;
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
};
|
|
1393
|
-
forEachSegment(line, expandPlainText, (escape) => {
|
|
1394
|
-
expandedLine += escape;
|
|
1395
|
-
});
|
|
1396
|
-
return expandedLine;
|
|
1397
|
-
};
|
|
1398
|
-
const restoreStylesAcrossRows = (preString) => {
|
|
1399
|
-
let returnValue = "";
|
|
1400
|
-
let activeHyperlink;
|
|
1401
|
-
const activeStyles = [];
|
|
1402
|
-
let index = 0;
|
|
1403
|
-
let copiedIndex = 0;
|
|
1404
|
-
while (index < preString.length) {
|
|
1405
|
-
ROW_BOUNDARY_REGEX.lastIndex = index;
|
|
1406
|
-
const boundary = ROW_BOUNDARY_REGEX.exec(preString);
|
|
1407
|
-
if (!boundary) break;
|
|
1408
|
-
index = boundary.index;
|
|
1409
|
-
if (boundary[0] !== "\n") {
|
|
1410
|
-
const escape = matchAnsiEscape(preString, index);
|
|
1411
|
-
if (!escape) {
|
|
1412
|
-
index++;
|
|
1413
|
-
continue;
|
|
1414
|
-
}
|
|
1415
|
-
const groups = escape.groups ?? {};
|
|
1416
|
-
if (groups["sgr"] !== void 0) applySgrParameters(groups["sgr"], activeStyles);
|
|
1417
|
-
else if (groups["uri"] !== void 0) activeHyperlink = groups["uri"].length === 0 ? void 0 : {
|
|
1418
|
-
parameters: groups["parameters"] ?? "",
|
|
1419
|
-
uri: groups["uri"]
|
|
1420
|
-
};
|
|
1421
|
-
index += escape[0].length;
|
|
1422
|
-
continue;
|
|
1423
|
-
}
|
|
1424
|
-
returnValue += preString.slice(copiedIndex, index);
|
|
1425
|
-
if (index > copiedIndex) {
|
|
1426
|
-
if (activeHyperlink) returnValue += wrapAnsiHyperlink("");
|
|
1427
|
-
returnValue += getClosingSgrSequence(activeStyles);
|
|
1428
|
-
}
|
|
1429
|
-
returnValue += "\n";
|
|
1430
|
-
index++;
|
|
1431
|
-
copiedIndex = index;
|
|
1432
|
-
if (index < preString.length && preString[index] !== "\n") {
|
|
1433
|
-
const openingStyles = [...activeStyles];
|
|
1434
|
-
applyLeadingSgrResets(preString, index, openingStyles);
|
|
1435
|
-
returnValue += getOpeningSgrSequence(openingStyles);
|
|
1436
|
-
if (activeHyperlink) returnValue += wrapAnsiHyperlink(activeHyperlink.uri, activeHyperlink.parameters);
|
|
1437
|
-
}
|
|
1438
|
-
}
|
|
1439
|
-
return returnValue + preString.slice(copiedIndex);
|
|
1440
|
-
};
|
|
1441
|
-
const exec = (string, columns, options = {}) => {
|
|
1442
|
-
if (options.trim !== false && string.trim() === "") return "";
|
|
1443
|
-
const words = splitWords(string);
|
|
1444
|
-
let rows = [""];
|
|
1445
|
-
let rowLength = 0;
|
|
1446
|
-
let trimmedRowIndex = -1;
|
|
1447
|
-
let isFirstWord = true;
|
|
1448
|
-
for (const word of words) {
|
|
1449
|
-
const rowIndex = rows.length - 1;
|
|
1450
|
-
if (options.trim !== false && trimmedRowIndex !== rowIndex) {
|
|
1451
|
-
const row = rows[rowIndex];
|
|
1452
|
-
const trimmedRow = row.trimStart();
|
|
1453
|
-
if (trimmedRow.length !== row.length) {
|
|
1454
|
-
rows[rowIndex] = trimmedRow;
|
|
1455
|
-
rowLength = getWidth(trimmedRow);
|
|
1456
|
-
}
|
|
1457
|
-
if (trimmedRow.length > 0) trimmedRowIndex = rowIndex;
|
|
1458
|
-
}
|
|
1459
|
-
if (isFirstWord) isFirstWord = false;
|
|
1460
|
-
else {
|
|
1461
|
-
if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
|
|
1462
|
-
rows.push("");
|
|
1463
|
-
rowLength = 0;
|
|
1464
|
-
}
|
|
1465
|
-
if (rowLength > 0 || options.trim === false) {
|
|
1466
|
-
rows[rows.length - 1] += " ";
|
|
1467
|
-
rowLength++;
|
|
1468
|
-
}
|
|
1469
|
-
}
|
|
1470
|
-
if (options.hard && options.wordWrap !== false && word.width > columns) {
|
|
1471
|
-
const remainingColumns = columns - rowLength;
|
|
1472
|
-
const breaksStartingThisLine = 1 + Math.floor((word.width - remainingColumns - 1) / columns);
|
|
1473
|
-
if (Math.floor((word.width - 1) / columns) < breaksStartingThisLine) {
|
|
1474
|
-
rows.push("");
|
|
1475
|
-
rowLength = 0;
|
|
1476
|
-
}
|
|
1477
|
-
rowLength = wrapWord(rows, word.value, columns, rowLength);
|
|
1478
|
-
continue;
|
|
1479
|
-
}
|
|
1480
|
-
if (rowLength + word.width > columns && rowLength > 0 && word.width > 0) {
|
|
1481
|
-
if (options.wordWrap === false && rowLength < columns) {
|
|
1482
|
-
rowLength = wrapWord(rows, word.value, columns, rowLength);
|
|
1483
|
-
continue;
|
|
1484
|
-
}
|
|
1485
|
-
rows.push("");
|
|
1486
|
-
rowLength = 0;
|
|
1487
|
-
}
|
|
1488
|
-
if (rowLength + word.width > columns && options.wordWrap === false) {
|
|
1489
|
-
rowLength = wrapWord(rows, word.value, columns, rowLength);
|
|
1490
|
-
continue;
|
|
1491
|
-
}
|
|
1492
|
-
rows[rows.length - 1] += word.value;
|
|
1493
|
-
rowLength += word.width;
|
|
1494
|
-
}
|
|
1495
|
-
if (options.trim !== false) rows = rows.map((row) => stringVisibleTrimSpacesRight(row));
|
|
1496
|
-
return restoreStylesAcrossRows(rows.join("\n"));
|
|
1497
|
-
};
|
|
1498
|
-
function wrapAnsi(string, columns, options) {
|
|
1499
|
-
return string.normalize().replaceAll("\r\n", "\n").split("\n").map((line) => exec(expandTabs(line), columns, options)).join("\n");
|
|
1500
|
-
}
|
|
1501
|
-
//#endregion
|
|
1502
|
-
//#region src/env.ts
|
|
1503
|
-
const isInCi = Boolean(process.env.CI);
|
|
1504
|
-
const isSigilDev = process.env.SIGIL_DEV === "true";
|
|
1505
|
-
const isScreenReader = process.env.SIGIL_SCREEN_READER === "true";
|
|
1506
|
-
const isWindows = process.platform === "win32";
|
|
1507
|
-
const isMacos = process.platform === "darwin";
|
|
1508
|
-
const isTty = (stream) => "isTTY" in stream && stream.isTTY === true;
|
|
1509
|
-
//#endregion
|
|
1510
|
-
//#region src/signal-exit.ts
|
|
1511
|
-
const signals = isWindows ? [
|
|
1512
|
-
"SIGHUP",
|
|
1513
|
-
"SIGINT",
|
|
1514
|
-
"SIGTERM",
|
|
1515
|
-
"SIGBREAK"
|
|
1516
|
-
] : [
|
|
1517
|
-
"SIGHUP",
|
|
1518
|
-
"SIGINT",
|
|
1519
|
-
"SIGTERM",
|
|
1520
|
-
"SIGQUIT",
|
|
1521
|
-
"SIGUSR2"
|
|
1522
|
-
];
|
|
1523
|
-
const registrations = /* @__PURE__ */ new Set();
|
|
1524
|
-
const signalListeners = /* @__PURE__ */ new Map();
|
|
1525
|
-
let loaded = false;
|
|
1526
|
-
let emitted = false;
|
|
1527
|
-
const emitExit = (code, signal) => {
|
|
1528
|
-
if (emitted) return;
|
|
1529
|
-
emitted = true;
|
|
1530
|
-
const ordered = [...registrations].toSorted((a, b) => Number(a.alwaysLast) - Number(b.alwaysLast));
|
|
1531
|
-
for (const registration of ordered) registration.handler(code, signal);
|
|
1532
|
-
};
|
|
1533
|
-
const onExitEvent = (code) => {
|
|
1534
|
-
emitExit(code, null);
|
|
1535
|
-
};
|
|
1536
|
-
const unload = () => {
|
|
1537
|
-
if (!loaded) return;
|
|
1538
|
-
loaded = false;
|
|
1539
|
-
process.off("exit", onExitEvent);
|
|
1540
|
-
for (const [signal, listener] of signalListeners) process.off(signal, listener);
|
|
1541
|
-
signalListeners.clear();
|
|
1542
|
-
};
|
|
1543
|
-
const load = () => {
|
|
1544
|
-
if (loaded) return;
|
|
1545
|
-
loaded = true;
|
|
1546
|
-
process.on("exit", onExitEvent);
|
|
1547
|
-
for (const signal of signals) {
|
|
1548
|
-
const listener = () => {
|
|
1549
|
-
if (process.listenerCount(signal) === 1) {
|
|
1550
|
-
unload();
|
|
1551
|
-
emitExit(null, signal);
|
|
1552
|
-
process.kill(process.pid, signal);
|
|
1553
|
-
}
|
|
1554
|
-
};
|
|
1555
|
-
try {
|
|
1556
|
-
process.on(signal, listener);
|
|
1557
|
-
signalListeners.set(signal, listener);
|
|
1558
|
-
} catch {}
|
|
1559
|
-
}
|
|
1560
|
-
};
|
|
1561
|
-
const signalExit = (handler, options = {}) => {
|
|
1562
|
-
const registration = {
|
|
1563
|
-
handler,
|
|
1564
|
-
alwaysLast: options.alwaysLast ?? false
|
|
1565
|
-
};
|
|
1566
|
-
registrations.add(registration);
|
|
1567
|
-
load();
|
|
1568
|
-
return () => {
|
|
1569
|
-
registrations.delete(registration);
|
|
1570
|
-
if (registrations.size === 0) unload();
|
|
1571
|
-
};
|
|
1572
|
-
};
|
|
1573
|
-
//#endregion
|
|
1574
|
-
//#region src/ansi/cursor.ts
|
|
1575
|
-
let restoreRegistered = false;
|
|
1576
|
-
const registerRestore = () => {
|
|
1577
|
-
if (restoreRegistered) return;
|
|
1578
|
-
restoreRegistered = true;
|
|
1579
|
-
signalExit(() => {
|
|
1580
|
-
process.stderr.write(cursorShow);
|
|
1581
|
-
}, { alwaysLast: true });
|
|
1582
|
-
};
|
|
1583
|
-
const cliCursor = {
|
|
1584
|
-
show(writableStream = process.stderr) {
|
|
1585
|
-
if (!writableStream.isTTY) return;
|
|
1586
|
-
writableStream.write(cursorShow);
|
|
1587
|
-
},
|
|
1588
|
-
hide(writableStream = process.stderr) {
|
|
1589
|
-
if (!writableStream.isTTY) return;
|
|
1590
|
-
registerRestore();
|
|
1591
|
-
writableStream.write(cursorHide);
|
|
1592
|
-
}
|
|
1593
|
-
};
|
|
1594
|
-
//#endregion
|
|
1595
|
-
//#region src/ansi/supports-color.ts
|
|
1596
|
-
function hasFlag(flag, argv = process.argv) {
|
|
1597
|
-
const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
|
|
1598
|
-
const position = argv.indexOf(prefix + flag);
|
|
1599
|
-
const terminatorPosition = argv.indexOf("--");
|
|
1600
|
-
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
1601
|
-
}
|
|
1602
|
-
const { env } = process;
|
|
1603
|
-
let flagForceColor;
|
|
1604
|
-
if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) flagForceColor = 0;
|
|
1605
|
-
else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) flagForceColor = 1;
|
|
1606
|
-
function envForceColor() {
|
|
1607
|
-
if (!("FORCE_COLOR" in env)) return;
|
|
1608
|
-
if (env["FORCE_COLOR"] === "true") return 1;
|
|
1609
|
-
if (env["FORCE_COLOR"] === "false") return 0;
|
|
1610
|
-
return env["FORCE_COLOR"].length === 0 ? 1 : Math.min(Number.parseInt(env["FORCE_COLOR"], 10), 3);
|
|
1611
|
-
}
|
|
1612
|
-
function translateLevel(level) {
|
|
1613
|
-
if (level === 0) return false;
|
|
1614
|
-
return {
|
|
1615
|
-
level,
|
|
1616
|
-
hasBasic: true,
|
|
1617
|
-
has256: level >= 2,
|
|
1618
|
-
has16m: level >= 3
|
|
1619
|
-
};
|
|
1620
|
-
}
|
|
1621
|
-
function supportsColorLevel(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
|
|
1622
|
-
const noFlagForceColor = envForceColor();
|
|
1623
|
-
if (noFlagForceColor !== void 0) flagForceColor = noFlagForceColor;
|
|
1624
|
-
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
1625
|
-
if (forceColor === 0) return 0;
|
|
1626
|
-
if (sniffFlags) {
|
|
1627
|
-
if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) return 3;
|
|
1628
|
-
if (hasFlag("color=256")) return 2;
|
|
1629
|
-
}
|
|
1630
|
-
if ("TF_BUILD" in env && "AGENT_NAME" in env) return 1;
|
|
1631
|
-
if (haveStream && !streamIsTTY && forceColor === void 0) return 0;
|
|
1632
|
-
const min = forceColor ?? 0;
|
|
1633
|
-
if (env["TERM"] === "dumb") return min;
|
|
1634
|
-
if (isWindows) return 3;
|
|
1635
|
-
if ("CI" in env) {
|
|
1636
|
-
if ([
|
|
1637
|
-
"GITHUB_ACTIONS",
|
|
1638
|
-
"GITEA_ACTIONS",
|
|
1639
|
-
"CIRCLECI"
|
|
1640
|
-
].some((key) => key in env)) return 3;
|
|
1641
|
-
if ([
|
|
1642
|
-
"TRAVIS",
|
|
1643
|
-
"APPVEYOR",
|
|
1644
|
-
"GITLAB_CI",
|
|
1645
|
-
"BUILDKITE",
|
|
1646
|
-
"DRONE"
|
|
1647
|
-
].some((sign) => sign in env) || env["CI_NAME"] === "codeship") return 1;
|
|
1648
|
-
return min;
|
|
1649
|
-
}
|
|
1650
|
-
if ("TEAMCITY_VERSION" in env) return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env["TEAMCITY_VERSION"]) ? 1 : 0;
|
|
1651
|
-
if (env["COLORTERM"] === "truecolor") return 3;
|
|
1652
|
-
if (env["TERM"] === "xterm-kitty") return 3;
|
|
1653
|
-
if (env["TERM"] === "xterm-ghostty") return 3;
|
|
1654
|
-
if (env["TERM"] === "wezterm") return 3;
|
|
1655
|
-
if ("TERM_PROGRAM" in env) {
|
|
1656
|
-
const version = Number.parseInt((env["TERM_PROGRAM_VERSION"] ?? "").split(".")[0], 10);
|
|
1657
|
-
switch (env["TERM_PROGRAM"]) {
|
|
1658
|
-
case "iTerm.app": return version >= 3 ? 3 : 2;
|
|
1659
|
-
case "Apple_Terminal": return 2;
|
|
1660
|
-
}
|
|
1661
|
-
}
|
|
1662
|
-
if (/-256(color)?$/i.test(env["TERM"] ?? "")) return 2;
|
|
1663
|
-
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env["TERM"] ?? "")) return 1;
|
|
1664
|
-
if ("COLORTERM" in env) return 1;
|
|
1665
|
-
return min;
|
|
1666
|
-
}
|
|
1667
|
-
function createSupportsColor(stream, options = {}) {
|
|
1668
|
-
return translateLevel(supportsColorLevel(Boolean(stream), {
|
|
1669
|
-
streamIsTTY: Boolean(stream?.isTTY),
|
|
1670
|
-
...options
|
|
1671
|
-
}));
|
|
1672
|
-
}
|
|
1673
|
-
const supportsColor = {
|
|
1674
|
-
stdout: createSupportsColor({ isTTY: isatty(1) }),
|
|
1675
|
-
stderr: createSupportsColor({ isTTY: isatty(2) })
|
|
1676
|
-
};
|
|
1677
|
-
//#endregion
|
|
1678
|
-
//#region src/ansi/chalk.ts
|
|
1679
|
-
const { stdout: stdoutColor } = supportsColor;
|
|
1680
|
-
const levelMapping = [
|
|
1681
|
-
"ansi",
|
|
1682
|
-
"ansi",
|
|
1683
|
-
"ansi256",
|
|
1684
|
-
"ansi16m"
|
|
1685
|
-
];
|
|
1686
|
-
const state = { level: stdoutColor ? stdoutColor.level : 0 };
|
|
1687
|
-
const applyStyle = (open, close, text) => {
|
|
1688
|
-
if (state.level <= 0 || !text) return text;
|
|
1689
|
-
let string = text;
|
|
1690
|
-
if (string.includes("\x1B")) string = string.replaceAll(close, close + open);
|
|
1691
|
-
if (string.includes("\n")) string = string.replaceAll(/\r?\n/g, `${close}$&${open}`);
|
|
1692
|
-
return open + string + close;
|
|
1693
|
-
};
|
|
1694
|
-
const styleFunction = (open, close) => {
|
|
1695
|
-
return (text) => applyStyle(open, close, text);
|
|
1696
|
-
};
|
|
1697
|
-
const foregroundOpen = (model, args) => {
|
|
1698
|
-
return colorOpen(model, args, foreground, rgbToAnsi256, rgbToAnsi);
|
|
1699
|
-
};
|
|
1700
|
-
const backgroundOpen = (model, args) => {
|
|
1701
|
-
return colorOpen(model, args, background, rgbToAnsi256, rgbToAnsi);
|
|
1702
|
-
};
|
|
1703
|
-
const colorOpen = (model, args, space, toAnsi256, toAnsi) => {
|
|
1704
|
-
const rgb = typeof args[0] === "string" ? hexToRgb(args[0]) : args;
|
|
1705
|
-
if (model === "ansi16m") return space.ansi16m(...rgb);
|
|
1706
|
-
if (model === "ansi256") return space.ansi256(toAnsi256(...rgb));
|
|
1707
|
-
return space.ansi(toAnsi(...rgb));
|
|
1708
|
-
};
|
|
1709
|
-
const named = {};
|
|
1710
|
-
for (const name of [
|
|
1711
|
-
...modifierNames,
|
|
1712
|
-
...foregroundColorNames,
|
|
1713
|
-
...backgroundColorNames
|
|
1714
|
-
]) named[name] = styleFunction(styles[name].open, styles[name].close);
|
|
1715
|
-
const chalk = {
|
|
1716
|
-
...named,
|
|
1717
|
-
get level() {
|
|
1718
|
-
return state.level;
|
|
1719
|
-
},
|
|
1720
|
-
set level(level) {
|
|
1721
|
-
state.level = level;
|
|
1722
|
-
},
|
|
1723
|
-
hex: (color) => styleFunction(foregroundOpen(levelMapping[state.level], [color]), foreground.close),
|
|
1724
|
-
bgHex: (color) => styleFunction(backgroundOpen(levelMapping[state.level], [color]), background.close),
|
|
1725
|
-
rgb: (red, green, blue) => styleFunction(foregroundOpen(levelMapping[state.level], [
|
|
1726
|
-
red,
|
|
1727
|
-
green,
|
|
1728
|
-
blue
|
|
1729
|
-
]), foreground.close),
|
|
1730
|
-
bgRgb: (red, green, blue) => styleFunction(backgroundOpen(levelMapping[state.level], [
|
|
1731
|
-
red,
|
|
1732
|
-
green,
|
|
1733
|
-
blue
|
|
1734
|
-
]), background.close),
|
|
1735
|
-
ansi256: (code) => styleFunction(foreground.ansi256(code), foreground.close),
|
|
1736
|
-
bgAnsi256: (code) => styleFunction(background.ansi256(code), background.close)
|
|
1737
|
-
};
|
|
1738
|
-
//#endregion
|
|
1739
|
-
//#region src/ansi-tokenizer.ts
|
|
1740
|
-
const bellCharacter = "\x07";
|
|
1741
|
-
const escapeCharacter = "\x1B";
|
|
1742
|
-
const stringTerminatorCharacter = "";
|
|
1743
|
-
const csiCharacter = "";
|
|
1744
|
-
const oscCharacter = "";
|
|
1745
|
-
const dcsCharacter = "";
|
|
1746
|
-
const pmCharacter = "";
|
|
1747
|
-
const apcCharacter = "";
|
|
1748
|
-
const sosCharacter = "";
|
|
1749
|
-
const isCsiParameterCharacter = (character) => {
|
|
1750
|
-
const codePoint = character.codePointAt(0);
|
|
1751
|
-
return codePoint !== void 0 && codePoint >= 48 && codePoint <= 63;
|
|
1752
|
-
};
|
|
1753
|
-
const isCsiIntermediateCharacter = (character) => {
|
|
1754
|
-
const codePoint = character.codePointAt(0);
|
|
1755
|
-
return codePoint !== void 0 && codePoint >= 32 && codePoint <= 47;
|
|
1756
|
-
};
|
|
1757
|
-
const isCsiFinalCharacter = (character) => {
|
|
1758
|
-
const codePoint = character.codePointAt(0);
|
|
1759
|
-
return codePoint !== void 0 && codePoint >= 64 && codePoint <= 126;
|
|
1760
|
-
};
|
|
1761
|
-
const isEscapeIntermediateCharacter = (character) => {
|
|
1762
|
-
const codePoint = character.codePointAt(0);
|
|
1763
|
-
return codePoint !== void 0 && codePoint >= 32 && codePoint <= 47;
|
|
1764
|
-
};
|
|
1765
|
-
const isEscapeFinalCharacter = (character) => {
|
|
1766
|
-
const codePoint = character.codePointAt(0);
|
|
1767
|
-
return codePoint !== void 0 && codePoint >= 48 && codePoint <= 126;
|
|
1768
|
-
};
|
|
1769
|
-
const isC1ControlCharacter = (character) => {
|
|
1770
|
-
const codePoint = character.codePointAt(0);
|
|
1771
|
-
return codePoint !== void 0 && codePoint >= 128 && codePoint <= 159;
|
|
1772
|
-
};
|
|
1773
|
-
const readCsiSequence = (text, fromIndex) => {
|
|
1774
|
-
let index = fromIndex;
|
|
1775
|
-
while (index < text.length) {
|
|
1776
|
-
const character = text[index];
|
|
1777
|
-
if (!isCsiParameterCharacter(character)) break;
|
|
1778
|
-
index++;
|
|
1779
|
-
}
|
|
1780
|
-
const parameterString = text.slice(fromIndex, index);
|
|
1781
|
-
const intermediateStartIndex = index;
|
|
1782
|
-
while (index < text.length) {
|
|
1783
|
-
const character = text[index];
|
|
1784
|
-
if (!isCsiIntermediateCharacter(character)) break;
|
|
1785
|
-
index++;
|
|
1786
|
-
}
|
|
1787
|
-
const intermediateString = text.slice(intermediateStartIndex, index);
|
|
1788
|
-
const finalCharacter = text[index];
|
|
1789
|
-
if (finalCharacter === void 0 || !isCsiFinalCharacter(finalCharacter)) return;
|
|
1790
|
-
return {
|
|
1791
|
-
endIndex: index + 1,
|
|
1792
|
-
parameterString,
|
|
1793
|
-
intermediateString,
|
|
1794
|
-
finalCharacter
|
|
1795
|
-
};
|
|
1796
|
-
};
|
|
1797
|
-
const findControlStringTerminatorIndex = (text, fromIndex, allowBellTerminator) => {
|
|
1798
|
-
for (let index = fromIndex; index < text.length; index++) {
|
|
1799
|
-
const character = text[index];
|
|
1800
|
-
if (allowBellTerminator && character === bellCharacter) return index + 1;
|
|
1801
|
-
if (character === stringTerminatorCharacter) return index + 1;
|
|
1802
|
-
if (character === escapeCharacter) {
|
|
1803
|
-
const followingCharacter = text[index + 1];
|
|
1804
|
-
if (followingCharacter === escapeCharacter) {
|
|
1805
|
-
index++;
|
|
1806
|
-
continue;
|
|
1807
|
-
}
|
|
1808
|
-
if (followingCharacter === "\\") return index + 2;
|
|
1809
|
-
}
|
|
1810
|
-
}
|
|
1811
|
-
};
|
|
1812
|
-
const readEscapeSequence = (text, fromIndex) => {
|
|
1813
|
-
let index = fromIndex;
|
|
1814
|
-
while (index < text.length) {
|
|
1815
|
-
const character = text[index];
|
|
1816
|
-
if (!isEscapeIntermediateCharacter(character)) break;
|
|
1817
|
-
index++;
|
|
1818
|
-
}
|
|
1819
|
-
const intermediateString = text.slice(fromIndex, index);
|
|
1820
|
-
const finalCharacter = text[index];
|
|
1821
|
-
if (finalCharacter === void 0 || !isEscapeFinalCharacter(finalCharacter)) return;
|
|
1822
|
-
return {
|
|
1823
|
-
endIndex: index + 1,
|
|
1824
|
-
intermediateString,
|
|
1825
|
-
finalCharacter
|
|
1826
|
-
};
|
|
1827
|
-
};
|
|
1828
|
-
const getControlStringFromEscapeIntroducer = (character) => {
|
|
1829
|
-
switch (character) {
|
|
1830
|
-
case "]": return {
|
|
1831
|
-
type: "osc",
|
|
1832
|
-
allowBellTerminator: true
|
|
1833
|
-
};
|
|
1834
|
-
case "P": return {
|
|
1835
|
-
type: "dcs",
|
|
1836
|
-
allowBellTerminator: false
|
|
1837
|
-
};
|
|
1838
|
-
case "^": return {
|
|
1839
|
-
type: "pm",
|
|
1840
|
-
allowBellTerminator: false
|
|
1841
|
-
};
|
|
1842
|
-
case "_": return {
|
|
1843
|
-
type: "apc",
|
|
1844
|
-
allowBellTerminator: false
|
|
1845
|
-
};
|
|
1846
|
-
case "X": return {
|
|
1847
|
-
type: "sos",
|
|
1848
|
-
allowBellTerminator: false
|
|
1849
|
-
};
|
|
1850
|
-
default: return;
|
|
1851
|
-
}
|
|
1852
|
-
};
|
|
1853
|
-
const getControlStringFromC1Introducer = (character) => {
|
|
1854
|
-
switch (character) {
|
|
1855
|
-
case oscCharacter: return {
|
|
1856
|
-
type: "osc",
|
|
1857
|
-
allowBellTerminator: true
|
|
1858
|
-
};
|
|
1859
|
-
case dcsCharacter: return {
|
|
1860
|
-
type: "dcs",
|
|
1861
|
-
allowBellTerminator: false
|
|
1862
|
-
};
|
|
1863
|
-
case pmCharacter: return {
|
|
1864
|
-
type: "pm",
|
|
1865
|
-
allowBellTerminator: false
|
|
1866
|
-
};
|
|
1867
|
-
case apcCharacter: return {
|
|
1868
|
-
type: "apc",
|
|
1869
|
-
allowBellTerminator: false
|
|
1870
|
-
};
|
|
1871
|
-
case sosCharacter: return {
|
|
1872
|
-
type: "sos",
|
|
1873
|
-
allowBellTerminator: false
|
|
1874
|
-
};
|
|
1875
|
-
default: return;
|
|
1876
|
-
}
|
|
1877
|
-
};
|
|
1878
|
-
const hasAnsiControlCharacters = (text) => {
|
|
1879
|
-
if (text.includes(escapeCharacter)) return true;
|
|
1880
|
-
for (const character of text) if (isC1ControlCharacter(character)) return true;
|
|
1881
|
-
return false;
|
|
1882
|
-
};
|
|
1883
|
-
const malformedFromIndex = (tokens, text, textStartIndex, fromIndex) => {
|
|
1884
|
-
if (fromIndex > textStartIndex) tokens.push({
|
|
1885
|
-
type: "text",
|
|
1886
|
-
value: text.slice(textStartIndex, fromIndex)
|
|
1887
|
-
});
|
|
1888
|
-
tokens.push({
|
|
1889
|
-
type: "invalid",
|
|
1890
|
-
value: text.slice(fromIndex)
|
|
1891
|
-
});
|
|
1892
|
-
return tokens;
|
|
1893
|
-
};
|
|
1894
|
-
const tokenizeAnsi = (text) => {
|
|
1895
|
-
if (!hasAnsiControlCharacters(text)) return [{
|
|
1896
|
-
type: "text",
|
|
1897
|
-
value: text
|
|
1898
|
-
}];
|
|
1899
|
-
const tokens = [];
|
|
1900
|
-
let textStartIndex = 0;
|
|
1901
|
-
for (let index = 0; index < text.length;) {
|
|
1902
|
-
const character = text[index];
|
|
1903
|
-
if (character === void 0) break;
|
|
1904
|
-
if (character === escapeCharacter) {
|
|
1905
|
-
const followingCharacter = text[index + 1];
|
|
1906
|
-
if (followingCharacter === void 0) return malformedFromIndex(tokens, text, textStartIndex, index);
|
|
1907
|
-
if (followingCharacter === "[") {
|
|
1908
|
-
const csiSequence = readCsiSequence(text, index + 2);
|
|
1909
|
-
if (csiSequence === void 0) return malformedFromIndex(tokens, text, textStartIndex, index);
|
|
1910
|
-
if (index > textStartIndex) tokens.push({
|
|
1911
|
-
type: "text",
|
|
1912
|
-
value: text.slice(textStartIndex, index)
|
|
1913
|
-
});
|
|
1914
|
-
tokens.push({
|
|
1915
|
-
type: "csi",
|
|
1916
|
-
value: text.slice(index, csiSequence.endIndex),
|
|
1917
|
-
parameterString: csiSequence.parameterString,
|
|
1918
|
-
intermediateString: csiSequence.intermediateString,
|
|
1919
|
-
finalCharacter: csiSequence.finalCharacter
|
|
1920
|
-
});
|
|
1921
|
-
index = csiSequence.endIndex;
|
|
1922
|
-
textStartIndex = index;
|
|
1923
|
-
continue;
|
|
1924
|
-
}
|
|
1925
|
-
const escapeControlString = getControlStringFromEscapeIntroducer(followingCharacter);
|
|
1926
|
-
if (escapeControlString !== void 0) {
|
|
1927
|
-
const controlStringTerminatorIndex = findControlStringTerminatorIndex(text, index + 2, escapeControlString.allowBellTerminator);
|
|
1928
|
-
if (controlStringTerminatorIndex === void 0) return malformedFromIndex(tokens, text, textStartIndex, index);
|
|
1929
|
-
if (index > textStartIndex) tokens.push({
|
|
1930
|
-
type: "text",
|
|
1931
|
-
value: text.slice(textStartIndex, index)
|
|
1932
|
-
});
|
|
1933
|
-
tokens.push({
|
|
1934
|
-
type: escapeControlString.type,
|
|
1935
|
-
value: text.slice(index, controlStringTerminatorIndex)
|
|
1936
|
-
});
|
|
1937
|
-
index = controlStringTerminatorIndex;
|
|
1938
|
-
textStartIndex = index;
|
|
1939
|
-
continue;
|
|
1940
|
-
}
|
|
1941
|
-
const escapeSequence = readEscapeSequence(text, index + 1);
|
|
1942
|
-
if (escapeSequence === void 0) {
|
|
1943
|
-
if (isEscapeIntermediateCharacter(followingCharacter)) return malformedFromIndex(tokens, text, textStartIndex, index);
|
|
1944
|
-
if (index > textStartIndex) tokens.push({
|
|
1945
|
-
type: "text",
|
|
1946
|
-
value: text.slice(textStartIndex, index)
|
|
1947
|
-
});
|
|
1948
|
-
index++;
|
|
1949
|
-
textStartIndex = index;
|
|
1950
|
-
continue;
|
|
1951
|
-
}
|
|
1952
|
-
if (index > textStartIndex) tokens.push({
|
|
1953
|
-
type: "text",
|
|
1954
|
-
value: text.slice(textStartIndex, index)
|
|
1955
|
-
});
|
|
1956
|
-
tokens.push({
|
|
1957
|
-
type: "esc",
|
|
1958
|
-
value: text.slice(index, escapeSequence.endIndex),
|
|
1959
|
-
intermediateString: escapeSequence.intermediateString,
|
|
1960
|
-
finalCharacter: escapeSequence.finalCharacter
|
|
1961
|
-
});
|
|
1962
|
-
index = escapeSequence.endIndex;
|
|
1963
|
-
textStartIndex = index;
|
|
1964
|
-
continue;
|
|
1965
|
-
}
|
|
1966
|
-
if (character === csiCharacter) {
|
|
1967
|
-
const csiSequence = readCsiSequence(text, index + 1);
|
|
1968
|
-
if (csiSequence === void 0) return malformedFromIndex(tokens, text, textStartIndex, index);
|
|
1969
|
-
if (index > textStartIndex) tokens.push({
|
|
1970
|
-
type: "text",
|
|
1971
|
-
value: text.slice(textStartIndex, index)
|
|
1972
|
-
});
|
|
1973
|
-
tokens.push({
|
|
1974
|
-
type: "csi",
|
|
1975
|
-
value: text.slice(index, csiSequence.endIndex),
|
|
1976
|
-
parameterString: csiSequence.parameterString,
|
|
1977
|
-
intermediateString: csiSequence.intermediateString,
|
|
1978
|
-
finalCharacter: csiSequence.finalCharacter
|
|
1979
|
-
});
|
|
1980
|
-
index = csiSequence.endIndex;
|
|
1981
|
-
textStartIndex = index;
|
|
1982
|
-
continue;
|
|
1983
|
-
}
|
|
1984
|
-
const c1ControlString = getControlStringFromC1Introducer(character);
|
|
1985
|
-
if (c1ControlString !== void 0) {
|
|
1986
|
-
const controlStringTerminatorIndex = findControlStringTerminatorIndex(text, index + 1, c1ControlString.allowBellTerminator);
|
|
1987
|
-
if (controlStringTerminatorIndex === void 0) return malformedFromIndex(tokens, text, textStartIndex, index);
|
|
1988
|
-
if (index > textStartIndex) tokens.push({
|
|
1989
|
-
type: "text",
|
|
1990
|
-
value: text.slice(textStartIndex, index)
|
|
1991
|
-
});
|
|
1992
|
-
tokens.push({
|
|
1993
|
-
type: c1ControlString.type,
|
|
1994
|
-
value: text.slice(index, controlStringTerminatorIndex)
|
|
1995
|
-
});
|
|
1996
|
-
index = controlStringTerminatorIndex;
|
|
1997
|
-
textStartIndex = index;
|
|
1998
|
-
continue;
|
|
1999
|
-
}
|
|
2000
|
-
if (character === stringTerminatorCharacter) {
|
|
2001
|
-
if (index > textStartIndex) tokens.push({
|
|
2002
|
-
type: "text",
|
|
2003
|
-
value: text.slice(textStartIndex, index)
|
|
2004
|
-
});
|
|
2005
|
-
tokens.push({
|
|
2006
|
-
type: "st",
|
|
2007
|
-
value: character
|
|
2008
|
-
});
|
|
2009
|
-
index++;
|
|
2010
|
-
textStartIndex = index;
|
|
2011
|
-
continue;
|
|
2012
|
-
}
|
|
2013
|
-
if (isC1ControlCharacter(character)) {
|
|
2014
|
-
if (index > textStartIndex) tokens.push({
|
|
2015
|
-
type: "text",
|
|
2016
|
-
value: text.slice(textStartIndex, index)
|
|
2017
|
-
});
|
|
2018
|
-
tokens.push({
|
|
2019
|
-
type: "c1",
|
|
2020
|
-
value: character
|
|
2021
|
-
});
|
|
2022
|
-
index++;
|
|
2023
|
-
textStartIndex = index;
|
|
2024
|
-
continue;
|
|
2025
|
-
}
|
|
2026
|
-
index++;
|
|
2027
|
-
}
|
|
2028
|
-
if (textStartIndex < text.length) tokens.push({
|
|
2029
|
-
type: "text",
|
|
2030
|
-
value: text.slice(textStartIndex)
|
|
2031
|
-
});
|
|
2032
|
-
return tokens;
|
|
2033
|
-
};
|
|
2034
|
-
//#endregion
|
|
2035
|
-
//#region src/ansi/tokenize.ts
|
|
2036
|
-
const BACKSLASH = "\\";
|
|
2037
|
-
const CSI = "[";
|
|
2038
|
-
const OSC = "]";
|
|
2039
|
-
const linkCodePrefix = `${OSC}8;`;
|
|
2040
|
-
const linkCodeSuffix = "\x07";
|
|
2041
|
-
const linkEndCode = `${OSC}8;;`;
|
|
2042
|
-
const linkEndCodeST = `${OSC}8;;${BACKSLASH}`;
|
|
2043
|
-
const linkEndCodeC1ST = `${OSC}8;;`;
|
|
2044
|
-
const endCodesSet = /* @__PURE__ */ new Set();
|
|
2045
|
-
const endCodesMap = /* @__PURE__ */ new Map();
|
|
2046
|
-
for (const [start, end] of codes) {
|
|
2047
|
-
endCodesSet.add(foreground.ansi(end));
|
|
2048
|
-
endCodesMap.set(foreground.ansi(start), foreground.ansi(end));
|
|
2049
|
-
}
|
|
2050
|
-
function getLinkStartCode(url, params) {
|
|
2051
|
-
const paramsString = params ? Object.entries(params).map(([key, value]) => `${key}=${value}`).join(":") : "";
|
|
2052
|
-
return `${linkCodePrefix}${paramsString};${url}${linkCodeSuffix}`;
|
|
2053
|
-
}
|
|
2054
|
-
function getEndCode(code) {
|
|
2055
|
-
if (endCodesSet.has(code)) return code;
|
|
2056
|
-
if (endCodesMap.has(code)) return endCodesMap.get(code);
|
|
2057
|
-
if (code.startsWith(linkCodePrefix)) {
|
|
2058
|
-
if (code.endsWith(`${BACKSLASH}`)) return linkEndCodeST;
|
|
2059
|
-
if (code.endsWith("")) return linkEndCodeC1ST;
|
|
2060
|
-
return linkEndCode;
|
|
2061
|
-
}
|
|
2062
|
-
code = code.slice(2);
|
|
2063
|
-
if (code.startsWith("38")) return foreground.close;
|
|
2064
|
-
if (code.startsWith("48")) return background.close;
|
|
2065
|
-
if (code.startsWith("58")) return `[59m`;
|
|
2066
|
-
const endCode = codes.get(Number.parseInt(code, 10));
|
|
2067
|
-
if (endCode !== void 0) return foreground.ansi(endCode);
|
|
2068
|
-
return styles.reset.open;
|
|
2069
|
-
}
|
|
2070
|
-
function ansiCodesToString(codes) {
|
|
2071
|
-
return [...new Set(codes.map((code) => code.code))].join("");
|
|
2072
|
-
}
|
|
2073
|
-
/**
|
|
2074
|
-
Check if a code is an intensity code (bold or dim) — these share the end code
|
|
2075
|
-
`22m` but can coexist.
|
|
2076
|
-
*/
|
|
2077
|
-
function isIntensityCode(code) {
|
|
2078
|
-
return code.code === styles.bold.open || code.code === styles.dim.open;
|
|
2079
|
-
}
|
|
2080
|
-
const segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
|
|
2081
|
-
/**
|
|
2082
|
-
Splits compound SGR sequences like `\u001B[1;3;31m` into individual components.
|
|
2083
|
-
*/
|
|
2084
|
-
function splitCompoundSgrSequences(code) {
|
|
2085
|
-
if (!code.includes(";")) return [code];
|
|
2086
|
-
const codeParts = code.slice(2, -1).split(";");
|
|
2087
|
-
const result = [];
|
|
2088
|
-
for (let index = 0; index < codeParts.length; index++) {
|
|
2089
|
-
const rawCode = codeParts[index];
|
|
2090
|
-
if (rawCode === "38" || rawCode === "48") {
|
|
2091
|
-
if (index + 2 < codeParts.length && codeParts[index + 1] === "5") {
|
|
2092
|
-
result.push(codeParts.slice(index, index + 3).join(";"));
|
|
2093
|
-
index += 2;
|
|
2094
|
-
continue;
|
|
2095
|
-
} else if (index + 4 < codeParts.length && codeParts[index + 1] === "2") {
|
|
2096
|
-
result.push(codeParts.slice(index, index + 5).join(";"));
|
|
2097
|
-
index += 4;
|
|
2098
|
-
continue;
|
|
2099
|
-
}
|
|
2100
|
-
}
|
|
2101
|
-
result.push(rawCode);
|
|
2102
|
-
}
|
|
2103
|
-
return result.map((part) => `[${part}m`);
|
|
2104
|
-
}
|
|
2105
|
-
const SGR_PARAMETERS_RE = /^[\d;:]*$/;
|
|
2106
|
-
const c1LinkCodePrefix = "8;";
|
|
2107
|
-
function tokenize(string, endChar = Number.POSITIVE_INFINITY) {
|
|
2108
|
-
const result = [];
|
|
2109
|
-
let visible = 0;
|
|
2110
|
-
outer: for (const ansiToken of tokenizeAnsi(string)) {
|
|
2111
|
-
if (ansiToken.type === "text") {
|
|
2112
|
-
for (const { segment } of segmenter.segment(ansiToken.value)) {
|
|
2113
|
-
const fullWidth = isFullwidthGrapheme(segment, segment.codePointAt(0));
|
|
2114
|
-
result.push({
|
|
2115
|
-
type: "char",
|
|
2116
|
-
value: segment,
|
|
2117
|
-
fullWidth
|
|
2118
|
-
});
|
|
2119
|
-
visible += fullWidth ? 2 : 1;
|
|
2120
|
-
if (visible >= endChar) break outer;
|
|
2121
|
-
}
|
|
2122
|
-
continue;
|
|
2123
|
-
}
|
|
2124
|
-
if (ansiToken.type === "csi") {
|
|
2125
|
-
if (ansiToken.finalCharacter === "m" && ansiToken.intermediateString === "" && SGR_PARAMETERS_RE.test(ansiToken.parameterString)) {
|
|
2126
|
-
const code = `${CSI}${ansiToken.parameterString}m`;
|
|
2127
|
-
for (const individualCode of splitCompoundSgrSequences(code)) result.push({
|
|
2128
|
-
type: "ansi",
|
|
2129
|
-
code: individualCode,
|
|
2130
|
-
endCode: getEndCode(individualCode)
|
|
2131
|
-
});
|
|
2132
|
-
} else result.push({
|
|
2133
|
-
type: "control",
|
|
2134
|
-
code: ansiToken.value
|
|
2135
|
-
});
|
|
2136
|
-
continue;
|
|
2137
|
-
}
|
|
2138
|
-
if (ansiToken.type === "osc") {
|
|
2139
|
-
const { value } = ansiToken;
|
|
2140
|
-
const prefix = value.startsWith(linkCodePrefix) ? linkCodePrefix : value.startsWith(c1LinkCodePrefix) ? c1LinkCodePrefix : void 0;
|
|
2141
|
-
if (prefix !== void 0 && value.includes(";", prefix.length)) {
|
|
2142
|
-
const code = prefix === linkCodePrefix ? value : `${linkCodePrefix}${value.slice(prefix.length)}`;
|
|
2143
|
-
result.push({
|
|
2144
|
-
type: "ansi",
|
|
2145
|
-
code,
|
|
2146
|
-
endCode: getEndCode(code)
|
|
2147
|
-
});
|
|
2148
|
-
} else result.push({
|
|
2149
|
-
type: "control",
|
|
2150
|
-
code: value
|
|
2151
|
-
});
|
|
2152
|
-
continue;
|
|
2153
|
-
}
|
|
2154
|
-
result.push({
|
|
2155
|
-
type: "control",
|
|
2156
|
-
code: ansiToken.value
|
|
2157
|
-
});
|
|
2158
|
-
}
|
|
2159
|
-
return result;
|
|
2160
|
-
}
|
|
2161
|
-
/**
|
|
2162
|
-
Reduces the given array of ANSI codes to the minimum necessary to render with
|
|
2163
|
-
the same style.
|
|
2164
|
-
*/
|
|
2165
|
-
function reduceAnsiCodes(codes) {
|
|
2166
|
-
return reduceAnsiCodesIncremental([], codes);
|
|
2167
|
-
}
|
|
2168
|
-
/**
|
|
2169
|
-
Like {@link reduceAnsiCodes}, but assumes that `codes` is already reduced.
|
|
2170
|
-
Further reductions are only done for the items in `newCodes`.
|
|
2171
|
-
*/
|
|
2172
|
-
function reduceAnsiCodesIncremental(codes, newCodes) {
|
|
2173
|
-
let result = [...codes];
|
|
2174
|
-
for (const code of newCodes) if (code.code === styles.reset.open) result = [];
|
|
2175
|
-
else if (endCodesSet.has(code.code)) result = result.filter((existing) => existing.endCode !== code.code);
|
|
2176
|
-
else if (isIntensityCode(code)) {
|
|
2177
|
-
if (!result.some((existing) => existing.code === code.code && existing.endCode === code.endCode)) result.push(code);
|
|
2178
|
-
} else {
|
|
2179
|
-
result = result.filter((existing) => existing.endCode !== code.endCode);
|
|
2180
|
-
result.push(code);
|
|
2181
|
-
}
|
|
2182
|
-
return result;
|
|
2183
|
-
}
|
|
2184
|
-
/**
|
|
2185
|
-
Returns the combination of ANSI codes needed to undo the given ANSI codes.
|
|
2186
|
-
*/
|
|
2187
|
-
function undoAnsiCodes(codes) {
|
|
2188
|
-
return reduceAnsiCodes(codes).toReversed().map((code) => ({
|
|
2189
|
-
...code,
|
|
2190
|
-
code: code.endCode
|
|
2191
|
-
}));
|
|
2192
|
-
}
|
|
2193
|
-
/**
|
|
2194
|
-
Returns the minimum amount of ANSI codes necessary to get from the compound
|
|
2195
|
-
style `from` to `to`. Both are expected to be reduced.
|
|
2196
|
-
*/
|
|
2197
|
-
function diffAnsiCodes(from, to) {
|
|
2198
|
-
const endCodesInTo = new Set(to.map((code) => code.endCode));
|
|
2199
|
-
const startCodesInTo = new Set(to.map((code) => code.code));
|
|
2200
|
-
const startCodesInFrom = new Set(from.map((code) => code.code));
|
|
2201
|
-
return [...undoAnsiCodes(from.filter((code) => {
|
|
2202
|
-
if (isIntensityCode(code)) return !startCodesInTo.has(code.code);
|
|
2203
|
-
return !endCodesInTo.has(code.endCode);
|
|
2204
|
-
})), ...to.filter((code) => !startCodesInFrom.has(code.code))];
|
|
2205
|
-
}
|
|
2206
|
-
function styledCharsFromTokens(tokens) {
|
|
2207
|
-
let codes = [];
|
|
2208
|
-
const result = [];
|
|
2209
|
-
for (const token of tokens) if (token.type === "ansi") codes = reduceAnsiCodesIncremental(codes, [token]);
|
|
2210
|
-
else if (token.type === "char") result.push({
|
|
2211
|
-
...token,
|
|
2212
|
-
styles: [...codes]
|
|
2213
|
-
});
|
|
2214
|
-
return result;
|
|
2215
|
-
}
|
|
2216
|
-
function styledCharsToString(chars) {
|
|
2217
|
-
let result = "";
|
|
2218
|
-
for (let index = 0; index < chars.length; index++) {
|
|
2219
|
-
const char = chars[index];
|
|
2220
|
-
if (index === 0) result += ansiCodesToString(char.styles);
|
|
2221
|
-
else result += ansiCodesToString(diffAnsiCodes(chars[index - 1].styles, char.styles));
|
|
2222
|
-
result += char.value;
|
|
2223
|
-
if (index === chars.length - 1) result += ansiCodesToString(diffAnsiCodes(char.styles, []));
|
|
2224
|
-
}
|
|
2225
|
-
return result;
|
|
2226
|
-
}
|
|
2227
|
-
//#endregion
|
|
2228
|
-
//#region src/ansi/slice.ts
|
|
2229
|
-
function sliceAnsi(string, start, end) {
|
|
2230
|
-
const sliceEnd = end ?? Number.POSITIVE_INFINITY;
|
|
2231
|
-
if (start >= sliceEnd || string === "") return "";
|
|
2232
|
-
if (start === 0 && sliceEnd === Number.POSITIVE_INFINITY) return string;
|
|
2233
|
-
const chars = styledCharsFromTokens(tokenize(string));
|
|
2234
|
-
const included = [];
|
|
2235
|
-
let column = 0;
|
|
2236
|
-
for (const char of chars) {
|
|
2237
|
-
const width = char.fullWidth ? 2 : 1;
|
|
2238
|
-
if (column + width > sliceEnd) break;
|
|
2239
|
-
if (column >= start) included.push(char);
|
|
2240
|
-
column += width;
|
|
2241
|
-
}
|
|
2242
|
-
return styledCharsToString(included);
|
|
2243
|
-
}
|
|
2244
|
-
//#endregion
|
|
2245
|
-
//#region src/ansi/truncate.ts
|
|
2246
|
-
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight = false) {
|
|
2247
|
-
if (string.charAt(wantedIndex) === " ") return wantedIndex;
|
|
2248
|
-
const direction = shouldSearchRight ? 1 : -1;
|
|
2249
|
-
for (let index = 0; index <= 3; index++) {
|
|
2250
|
-
const finalIndex = wantedIndex + index * direction;
|
|
2251
|
-
if (string.charAt(finalIndex) === " ") return finalIndex;
|
|
2252
|
-
}
|
|
2253
|
-
return wantedIndex;
|
|
2254
|
-
}
|
|
2255
|
-
const ANSI_ESC = 27;
|
|
2256
|
-
const ANSI_LEFT_BRACKET = 91;
|
|
2257
|
-
const ANSI_LETTER_M = 109;
|
|
2258
|
-
const isSgrParameter = (code) => code >= 48 && code <= 57 || code === 59 || code === 58;
|
|
2259
|
-
function leadingSgrSpanEndIndex(string) {
|
|
2260
|
-
let index = 0;
|
|
2261
|
-
while (index + 2 < string.length && string.codePointAt(index) === ANSI_ESC && string.codePointAt(index + 1) === ANSI_LEFT_BRACKET) {
|
|
2262
|
-
let scan = index + 2;
|
|
2263
|
-
while (scan < string.length && isSgrParameter(string.codePointAt(scan))) scan++;
|
|
2264
|
-
if (scan < string.length && string.codePointAt(scan) === ANSI_LETTER_M) {
|
|
2265
|
-
index = scan + 1;
|
|
2266
|
-
continue;
|
|
2267
|
-
}
|
|
2268
|
-
break;
|
|
2269
|
-
}
|
|
2270
|
-
return index;
|
|
2271
|
-
}
|
|
2272
|
-
function trailingSgrSpanStartIndex(string) {
|
|
2273
|
-
let start = string.length;
|
|
2274
|
-
while (start > 1 && string.codePointAt(start - 1) === ANSI_LETTER_M) {
|
|
2275
|
-
let scan = start - 2;
|
|
2276
|
-
while (scan >= 0 && isSgrParameter(string.codePointAt(scan))) scan--;
|
|
2277
|
-
if (scan >= 1 && string.codePointAt(scan - 1) === ANSI_ESC && string.codePointAt(scan) === ANSI_LEFT_BRACKET) {
|
|
2278
|
-
start = scan - 1;
|
|
2279
|
-
continue;
|
|
2280
|
-
}
|
|
2281
|
-
break;
|
|
2282
|
-
}
|
|
2283
|
-
return start;
|
|
2284
|
-
}
|
|
2285
|
-
function appendWithInheritedStyleFromEnd(visible, suffix) {
|
|
2286
|
-
const start = trailingSgrSpanStartIndex(visible);
|
|
2287
|
-
if (start === visible.length) return visible + suffix;
|
|
2288
|
-
return visible.slice(0, start) + suffix + visible.slice(start);
|
|
2289
|
-
}
|
|
2290
|
-
function prependWithInheritedStyleFromStart(prefix, visible) {
|
|
2291
|
-
const end = leadingSgrSpanEndIndex(visible);
|
|
2292
|
-
if (end === 0) return prefix + visible;
|
|
2293
|
-
return visible.slice(0, end) + prefix + visible.slice(end);
|
|
2294
|
-
}
|
|
2295
|
-
function cliTruncate(text, columns, options = {}) {
|
|
2296
|
-
const { position = "end", space = false, preferTruncationOnSpace = false } = options;
|
|
2297
|
-
let { truncationCharacter = "…" } = options;
|
|
2298
|
-
if (columns < 1) return "";
|
|
2299
|
-
const length = stringWidth(text);
|
|
2300
|
-
if (length <= columns) return text;
|
|
2301
|
-
if (columns === 1) return truncationCharacter;
|
|
2302
|
-
if (position === "start") {
|
|
2303
|
-
if (preferTruncationOnSpace) {
|
|
2304
|
-
const right = sliceAnsi(text, getIndexOfNearestSpace(text, length - columns + stringWidth(truncationCharacter), true), length).trim();
|
|
2305
|
-
return prependWithInheritedStyleFromStart(truncationCharacter, right);
|
|
2306
|
-
}
|
|
2307
|
-
if (space) truncationCharacter += " ";
|
|
2308
|
-
const right = sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
|
|
2309
|
-
return prependWithInheritedStyleFromStart(truncationCharacter, right);
|
|
2310
|
-
}
|
|
2311
|
-
if (position === "middle") {
|
|
2312
|
-
if (space) {
|
|
2313
|
-
truncationCharacter = ` ${truncationCharacter} `;
|
|
2314
|
-
if (stringWidth(truncationCharacter) >= columns) truncationCharacter = truncationCharacter.trim();
|
|
2315
|
-
}
|
|
2316
|
-
const truncationWidth = stringWidth(truncationCharacter);
|
|
2317
|
-
const half = Math.min(Math.floor(columns / 2), Math.max(0, columns - truncationWidth));
|
|
2318
|
-
if (preferTruncationOnSpace) {
|
|
2319
|
-
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
|
|
2320
|
-
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + truncationWidth, true);
|
|
2321
|
-
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
|
|
2322
|
-
}
|
|
2323
|
-
return sliceAnsi(text, 0, half) + truncationCharacter + sliceAnsi(text, length - (columns - half) + truncationWidth, length);
|
|
2324
|
-
}
|
|
2325
|
-
if (preferTruncationOnSpace) return appendWithInheritedStyleFromEnd(sliceAnsi(text, 0, getIndexOfNearestSpace(text, columns - stringWidth(truncationCharacter))), truncationCharacter);
|
|
2326
|
-
if (space) truncationCharacter = ` ${truncationCharacter}`;
|
|
2327
|
-
return appendWithInheritedStyleFromEnd(sliceAnsi(text, 0, columns - stringWidth(truncationCharacter)), truncationCharacter);
|
|
2328
|
-
}
|
|
2329
|
-
//#endregion
|
|
2330
|
-
export { wideRanges as $, ambiguousMaximumCodePoint as A, cursorUp as At, halfwidthMinimalCodePoint as B, kittyQuery as Bt, isScreenReader as C, clearTerminal as Ct, wrapAnsi as D, cursorNextLine as Dt, isWindows as E, cursorLeft as Et, fullwidthMaximumCodePoint as F, eraseLine as Ft, isFullwidthGrapheme as G, pushKittyKeyboard as Gt, isAmbiguous as H, pasteEnd as Ht, fullwidthMinimalCodePoint as I, eraseLines as It, narrowMaximumCodePoint as J, isWide as K, fullwidthRanges as L, eraseScreen as Lt, ambiguousRanges as M, enableBracketedPaste as Mt, eastAsianWidth as N, enterAlternativeScreen as Nt, stringWidth as O, cursorShow as Ot, eastAsianWidthType as P, eraseEndLine as Pt, wideMinimalCodePoint as Q, getCategory as R, esu as Rt, isMacos as S, bsu as St, isTty as T, cursorHide as Tt, isFullWidth as U, pasteStart as Ut, halfwidthRanges as V, link as Vt, isFullwidthCodePoint as W, popKittyKeyboard as Wt, narrowRanges as X, narrowMinimalCodePoint as Y, wideMaximumCodePoint as Z, chalk as _, DEL as _t, endCodesSet as a, foregroundColorNames as at, signalExit as b, ST as bt, isIntensityCode as c, hexToRgb as ct, styledCharsFromTokens as d, rgbToAnsi256 as dt, ansi256ToAnsi as et, styledCharsToString as f, styles as ft, tokenizeAnsi as g, CSI$1 as gt, hasAnsiControlCharacters as h, C1_ST as ht, diffAnsiCodes as i, foreground as it, ambiguousMinimalCodePoint as j, disableBracketedPaste as jt, widestLine as k, cursorTo as kt, reduceAnsiCodes as l, modifierNames as lt, undoAnsiCodes as m, C1_CSI as mt, sliceAnsi as n, backgroundColorNames as nt, getEndCode as o, hexToAnsi as ot, tokenize as p, BEL as pt, isWideNotCJKTNotEmoji as q, ansiCodesToString as r, codes as rt, getLinkStartCode as s, hexToAnsi256 as st, cliTruncate as t, background as tt, reduceAnsiCodesIncremental as u, rgbToAnsi as ut, supportsColor as v, ESC as vt, isSigilDev as w, cursorDown as wt, isInCi as x, ansiEscapes as xt, cliCursor as y, OSC$1 as yt, halfwidthMaximumCodePoint as z, exitAlternativeScreen as zt };
|