@alchemy.run/sigil 0.0.0-alpha.1 → 0.0.0-alpha.2
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 +299 -299
- package/dist/ansi.d.ts +223 -0
- package/dist/ansi.js +2 -0
- package/dist/{devtools-QpCMm9JH.mjs → devtools-BhYGjb7h.js} +1 -1
- package/dist/index-DDVME65c.d.ts +919 -0
- package/dist/index.d.ts +1657 -0
- package/dist/index.js +4643 -0
- package/dist/sgr-CMfEpjSk.d.ts +91 -0
- package/dist/truncate-CBiyyZzw.js +2156 -0
- package/dist/yoga-5jKhYCJC.js +3465 -0
- package/dist/yoga.d.ts +2 -0
- package/dist/yoga.js +2 -0
- package/package.json +37 -17
- package/src/ansi/chalk.ts +179 -0
- package/src/ansi/cursor.ts +48 -0
- package/src/ansi/east-asian-width.ts +215 -0
- package/src/ansi/escapes.ts +128 -0
- package/src/ansi/index.ts +27 -0
- package/src/ansi/sgr.ts +237 -0
- package/src/ansi/slice.ts +43 -0
- package/src/ansi/string-width.ts +236 -0
- package/src/ansi/strip.ts +33 -0
- package/src/ansi/supports-color.ts +213 -0
- package/src/ansi/tokenize.ts +453 -0
- package/src/ansi/truncate.ts +194 -0
- package/src/ansi/widest-line.ts +12 -0
- package/src/ansi/wrap.ts +766 -0
- package/src/ansi-tokenizer.ts +510 -0
- package/src/auto-bind.ts +41 -0
- package/src/boxes.ts +100 -0
- package/src/code-excerpt.ts +39 -0
- package/src/colorize.ts +60 -0
- package/src/components/AccessibilityContext.ts +5 -0
- package/src/components/AnimationContext.ts +24 -0
- package/src/components/App.tsx +781 -0
- package/src/components/AppContext.ts +111 -0
- package/src/components/BackgroundContext.ts +8 -0
- package/src/components/Box.tsx +116 -0
- package/src/components/CursorContext.ts +19 -0
- package/src/components/ErrorBoundary.tsx +38 -0
- package/src/components/ErrorOverview.tsx +133 -0
- package/src/components/FocusContext.ts +30 -0
- package/src/components/Newline.tsx +15 -0
- package/src/components/Spacer.tsx +10 -0
- package/src/components/Static.tsx +59 -0
- package/src/components/StderrContext.ts +26 -0
- package/src/components/StdinContext.ts +49 -0
- package/src/components/StdoutContext.ts +28 -0
- package/src/components/Text.tsx +144 -0
- package/src/components/Transform.tsx +37 -0
- package/src/cursor-position.ts +103 -0
- package/src/devtools-window-polyfill.ts +73 -0
- package/src/devtools.ts +43 -0
- package/src/dom.ts +292 -0
- package/src/get-max-width.ts +11 -0
- package/src/global.d.ts +36 -0
- package/src/hooks/use-animation.ts +142 -0
- package/src/hooks/use-app.ts +8 -0
- package/src/hooks/use-box-metrics.ts +134 -0
- package/src/hooks/use-cursor.ts +33 -0
- package/src/hooks/use-focus-manager.ts +62 -0
- package/src/hooks/use-focus.ts +83 -0
- package/src/hooks/use-input.ts +267 -0
- package/src/hooks/use-is-screen-reader-enabled.ts +12 -0
- package/src/hooks/use-paste.ts +78 -0
- package/src/hooks/use-stderr.ts +8 -0
- package/src/hooks/use-stdin.ts +10 -0
- package/src/hooks/use-stdout.ts +8 -0
- package/src/hooks/use-window-size.ts +41 -0
- package/src/indent-string.ts +16 -0
- package/src/index.ts +44 -0
- package/src/ink.tsx +1506 -0
- package/src/input-parser.ts +283 -0
- package/src/instances.ts +9 -0
- package/src/is-in-ci.ts +7 -0
- package/src/kitty-keyboard.ts +57 -0
- package/src/log-update.ts +370 -0
- package/src/measure-element.ts +62 -0
- package/src/measure-text.ts +31 -0
- package/src/output.ts +308 -0
- package/src/parse-keypress.ts +516 -0
- package/src/parse-stack-line.ts +139 -0
- package/src/patch-console.ts +62 -0
- package/src/quick-lru.ts +85 -0
- package/src/reconciler.ts +451 -0
- package/src/render-background.ts +38 -0
- package/src/render-border.ts +134 -0
- package/src/render-node-to-output.ts +191 -0
- package/src/render-to-string.ts +131 -0
- package/src/render.ts +276 -0
- package/src/renderer.ts +73 -0
- package/src/sanitize-ansi.ts +33 -0
- package/src/signal-exit.ts +107 -0
- package/src/squash-text-nodes.ts +40 -0
- package/src/stream.ts +30 -0
- package/src/styles.ts +748 -0
- package/src/terminal-size.ts +57 -0
- package/src/throttle.ts +73 -0
- package/src/types.ts +15 -0
- package/src/utils.ts +40 -0
- package/src/wrap-text.ts +50 -0
- package/src/write-synchronized.ts +9 -0
- package/src/yoga/config.ts +57 -0
- package/src/yoga/core/absoluteLayout.ts +626 -0
- package/src/yoga/core/baseline.ts +66 -0
- package/src/yoga/core/cache.ts +136 -0
- package/src/yoga/core/calculateLayout.ts +2920 -0
- package/src/yoga/core/config.ts +104 -0
- package/src/yoga/core/flexLine.ts +177 -0
- package/src/yoga/core/helpers.ts +293 -0
- package/src/yoga/core/layoutResults.ts +167 -0
- package/src/yoga/core/node.ts +611 -0
- package/src/yoga/core/numeric.ts +44 -0
- package/src/yoga/core/pixelGrid.ts +151 -0
- package/src/yoga/core/style.ts +887 -0
- package/src/yoga/core/types.ts +224 -0
- package/src/yoga/generated/YGEnums.ts +263 -0
- package/src/yoga/index.ts +19 -0
- package/src/yoga/node.ts +1140 -0
- package/dist/index.d.mts +0 -2379
- package/dist/index.mjs +0 -10072
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Terminal color support detection.
|
|
2
|
+
// Ported from `supports-color` (MIT, Sindre Sorhus & Josh Junon).
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import tty from "node:tty";
|
|
6
|
+
|
|
7
|
+
export type ColorSupportLevel = 0 | 1 | 2 | 3;
|
|
8
|
+
|
|
9
|
+
export type ColorSupport = {
|
|
10
|
+
readonly level: ColorSupportLevel;
|
|
11
|
+
readonly hasBasic: boolean;
|
|
12
|
+
readonly has256: boolean;
|
|
13
|
+
readonly has16m: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type ColorInfo = ColorSupport | false;
|
|
17
|
+
|
|
18
|
+
function hasFlag(flag: string, argv: readonly string[] = process.argv): boolean {
|
|
19
|
+
const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
|
|
20
|
+
const position = argv.indexOf(prefix + flag);
|
|
21
|
+
const terminatorPosition = argv.indexOf("--");
|
|
22
|
+
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const { env } = process;
|
|
26
|
+
|
|
27
|
+
let flagForceColor: number | undefined;
|
|
28
|
+
if (
|
|
29
|
+
hasFlag("no-color") ||
|
|
30
|
+
hasFlag("no-colors") ||
|
|
31
|
+
hasFlag("color=false") ||
|
|
32
|
+
hasFlag("color=never")
|
|
33
|
+
) {
|
|
34
|
+
flagForceColor = 0;
|
|
35
|
+
} else if (
|
|
36
|
+
hasFlag("color") ||
|
|
37
|
+
hasFlag("colors") ||
|
|
38
|
+
hasFlag("color=true") ||
|
|
39
|
+
hasFlag("color=always")
|
|
40
|
+
) {
|
|
41
|
+
flagForceColor = 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function envForceColor(): number | undefined {
|
|
45
|
+
if (!("FORCE_COLOR" in env)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (env["FORCE_COLOR"] === "true") {
|
|
50
|
+
return 1;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (env["FORCE_COLOR"] === "false") {
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return env["FORCE_COLOR"]!.length === 0
|
|
58
|
+
? 1
|
|
59
|
+
: Math.min(Number.parseInt(env["FORCE_COLOR"]!, 10), 3);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function translateLevel(level: number): ColorInfo {
|
|
63
|
+
if (level === 0) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
level: level as ColorSupportLevel,
|
|
69
|
+
hasBasic: true,
|
|
70
|
+
has256: level >= 2,
|
|
71
|
+
has16m: level >= 3,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type Options = {
|
|
76
|
+
readonly streamIsTTY?: boolean;
|
|
77
|
+
readonly sniffFlags?: boolean;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// eslint-disable-next-line complexity
|
|
81
|
+
function supportsColorLevel(
|
|
82
|
+
haveStream: boolean,
|
|
83
|
+
{ streamIsTTY, sniffFlags = true }: Options = {},
|
|
84
|
+
): number {
|
|
85
|
+
const noFlagForceColor = envForceColor();
|
|
86
|
+
if (noFlagForceColor !== undefined) {
|
|
87
|
+
flagForceColor = noFlagForceColor;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
91
|
+
|
|
92
|
+
if (forceColor === 0) {
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (sniffFlags) {
|
|
97
|
+
if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) {
|
|
98
|
+
return 3;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (hasFlag("color=256")) {
|
|
102
|
+
return 2;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Check for Azure DevOps pipelines. Has to be above the `!streamIsTTY` check.
|
|
107
|
+
if ("TF_BUILD" in env && "AGENT_NAME" in env) {
|
|
108
|
+
return 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (haveStream && !streamIsTTY && forceColor === undefined) {
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const min = forceColor ?? 0;
|
|
116
|
+
|
|
117
|
+
if (env["TERM"] === "dumb") {
|
|
118
|
+
return min;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (process.platform === "win32") {
|
|
122
|
+
// Windows 10 build 10586 is the first Windows release that supports 256 colors.
|
|
123
|
+
// Windows 10 build 14931 is the first release that supports 16m/TrueColor.
|
|
124
|
+
const osRelease = os.release().split(".");
|
|
125
|
+
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10_586) {
|
|
126
|
+
return Number(osRelease[2]) >= 14_931 ? 3 : 2;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if ("CI" in env) {
|
|
133
|
+
if (["GITHUB_ACTIONS", "GITEA_ACTIONS", "CIRCLECI"].some((key) => key in env)) {
|
|
134
|
+
return 3;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (
|
|
138
|
+
["TRAVIS", "APPVEYOR", "GITLAB_CI", "BUILDKITE", "DRONE"].some((sign) => sign in env) ||
|
|
139
|
+
env["CI_NAME"] === "codeship"
|
|
140
|
+
) {
|
|
141
|
+
return 1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return min;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if ("TEAMCITY_VERSION" in env) {
|
|
148
|
+
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env["TEAMCITY_VERSION"]!) ? 1 : 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (env["COLORTERM"] === "truecolor") {
|
|
152
|
+
return 3;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (env["TERM"] === "xterm-kitty") {
|
|
156
|
+
return 3;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (env["TERM"] === "xterm-ghostty") {
|
|
160
|
+
return 3;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (env["TERM"] === "wezterm") {
|
|
164
|
+
return 3;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if ("TERM_PROGRAM" in env) {
|
|
168
|
+
const version = Number.parseInt((env["TERM_PROGRAM_VERSION"] ?? "").split(".")[0]!, 10);
|
|
169
|
+
|
|
170
|
+
switch (env["TERM_PROGRAM"]) {
|
|
171
|
+
case "iTerm.app": {
|
|
172
|
+
return version >= 3 ? 3 : 2;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
case "Apple_Terminal": {
|
|
176
|
+
return 2;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
default:
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (/-256(color)?$/i.test(env["TERM"] ?? "")) {
|
|
184
|
+
return 2;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env["TERM"] ?? "")) {
|
|
188
|
+
return 1;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if ("COLORTERM" in env) {
|
|
192
|
+
return 1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return min;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function createSupportsColor(
|
|
199
|
+
stream: { isTTY?: boolean } | undefined,
|
|
200
|
+
options: Options = {},
|
|
201
|
+
): ColorInfo {
|
|
202
|
+
const level = supportsColorLevel(Boolean(stream), {
|
|
203
|
+
streamIsTTY: Boolean(stream?.isTTY),
|
|
204
|
+
...options,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
return translateLevel(level);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export const supportsColor = {
|
|
211
|
+
stdout: createSupportsColor({ isTTY: tty.isatty(1) }),
|
|
212
|
+
stderr: createSupportsColor({ isTTY: tty.isatty(2) }),
|
|
213
|
+
};
|
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { isFullwidthCodePoint } from "./east-asian-width.ts";
|
|
2
|
+
// ANSI-aware tokenizer: splits a string into grapheme clusters and ANSI
|
|
3
|
+
// codes, tracks active styles per character, and re-emits minimal escape
|
|
4
|
+
// sequences. This is the style model Ink's renderer is built on.
|
|
5
|
+
// Ported from `@alcalzone/ansi-tokenize` (MIT, AlCalzone).
|
|
6
|
+
import { BEL, C1_ST, ESC } from "./escapes.ts";
|
|
7
|
+
import { codes as sgrCodes, foreground, background, styles } from "./sgr.ts";
|
|
8
|
+
|
|
9
|
+
// Single-character introducer suffixes (`ESC [` = CSI, `ESC ]` = OSC).
|
|
10
|
+
const BACKSLASH = "\\";
|
|
11
|
+
const CSI = "[";
|
|
12
|
+
const OSC = "]";
|
|
13
|
+
|
|
14
|
+
// Char codes
|
|
15
|
+
const CC_BEL = BEL.charCodeAt(0);
|
|
16
|
+
const CC_ESC = ESC.charCodeAt(0);
|
|
17
|
+
const CC_BACKSLASH = BACKSLASH.charCodeAt(0);
|
|
18
|
+
const CC_CSI = CSI.charCodeAt(0);
|
|
19
|
+
const CC_OSC = OSC.charCodeAt(0);
|
|
20
|
+
const CC_C1_ST = C1_ST.charCodeAt(0);
|
|
21
|
+
const CC_0 = "0".charCodeAt(0);
|
|
22
|
+
const CC_9 = "9".charCodeAt(0);
|
|
23
|
+
const CC_SEMI = ";".charCodeAt(0);
|
|
24
|
+
const CC_M = "m".charCodeAt(0);
|
|
25
|
+
|
|
26
|
+
// Escape code points: \u001B and \u009B
|
|
27
|
+
const ESCAPES = new Set([CC_ESC, 0x9b]);
|
|
28
|
+
|
|
29
|
+
// OSC 8 hyperlink constants
|
|
30
|
+
const linkCodePrefix = `${ESC}${OSC}8;`;
|
|
31
|
+
const linkCodePrefixCharCodes = linkCodePrefix.split("").map((char) => char.charCodeAt(0));
|
|
32
|
+
const linkCodeSuffix = BEL;
|
|
33
|
+
const linkEndCode = `${ESC}${OSC}8;;${BEL}`;
|
|
34
|
+
const linkEndCodeST = `${ESC}${OSC}8;;${ESC}${BACKSLASH}`;
|
|
35
|
+
const linkEndCodeC1ST = `${ESC}${OSC}8;;${C1_ST}`;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
An ANSI code paired with the code that ends its effect.
|
|
39
|
+
*/
|
|
40
|
+
export type AnsiCode = {
|
|
41
|
+
readonly code: string;
|
|
42
|
+
readonly endCode: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type AnsiToken = AnsiCode & {
|
|
46
|
+
readonly type: "ansi";
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type ControlToken = {
|
|
50
|
+
readonly type: "control";
|
|
51
|
+
readonly code: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type CharToken = {
|
|
55
|
+
readonly type: "char";
|
|
56
|
+
readonly value: string;
|
|
57
|
+
readonly fullWidth: boolean;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type Token = AnsiToken | ControlToken | CharToken;
|
|
61
|
+
|
|
62
|
+
export type StyledChar = CharToken & {
|
|
63
|
+
readonly styles: AnsiCode[];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const endCodesSet = new Set<string>();
|
|
67
|
+
const endCodesMap = new Map<string, string>();
|
|
68
|
+
|
|
69
|
+
for (const [start, end] of sgrCodes) {
|
|
70
|
+
endCodesSet.add(foreground.ansi(end));
|
|
71
|
+
endCodesMap.set(foreground.ansi(start), foreground.ansi(end));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function getLinkStartCode(url: string, params?: Record<string, string>): string {
|
|
75
|
+
const paramsString = params
|
|
76
|
+
? Object.entries(params)
|
|
77
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
78
|
+
.join(":")
|
|
79
|
+
: "";
|
|
80
|
+
return `${linkCodePrefix}${paramsString};${url}${linkCodeSuffix}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function getEndCode(code: string): string {
|
|
84
|
+
if (endCodesSet.has(code)) return code;
|
|
85
|
+
if (endCodesMap.has(code)) return endCodesMap.get(code)!;
|
|
86
|
+
|
|
87
|
+
// We have a few special cases to handle here:
|
|
88
|
+
// Links:
|
|
89
|
+
if (code.startsWith(linkCodePrefix)) {
|
|
90
|
+
if (code.endsWith(`${ESC}${BACKSLASH}`)) return linkEndCodeST;
|
|
91
|
+
if (code.endsWith(C1_ST)) return linkEndCodeC1ST;
|
|
92
|
+
return linkEndCode; // BEL (\u0007)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
code = code.slice(2);
|
|
96
|
+
|
|
97
|
+
// 8-bit/24-bit colors:
|
|
98
|
+
if (code.startsWith("38")) {
|
|
99
|
+
return foreground.close;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (code.startsWith("48")) {
|
|
103
|
+
return background.close;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Otherwise find the reset code in the SGR code map
|
|
107
|
+
const endCode = sgrCodes.get(Number.parseInt(code, 10));
|
|
108
|
+
if (endCode !== undefined) {
|
|
109
|
+
return foreground.ansi(endCode);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return styles.reset.open;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function ansiCodesToString(codes: readonly AnsiCode[]): string {
|
|
116
|
+
// Deduplicate ANSI code strings before joining
|
|
117
|
+
const deduplicated = new Set(codes.map((code) => code.code));
|
|
118
|
+
return [...deduplicated].join("");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
Check if a code is an intensity code (bold or dim) — these share the end code
|
|
123
|
+
`22m` but can coexist.
|
|
124
|
+
*/
|
|
125
|
+
export function isIntensityCode(code: AnsiCode): boolean {
|
|
126
|
+
return code.code === styles.bold.open || code.code === styles.dim.open;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
130
|
+
|
|
131
|
+
function isFullwidthGrapheme(grapheme: string, baseCodePoint: number): boolean {
|
|
132
|
+
if (isFullwidthCodePoint(baseCodePoint)) return true;
|
|
133
|
+
// Variation Selector 16 forces emoji presentation (2 columns wide)
|
|
134
|
+
if (grapheme.includes("\uFE0F")) return true;
|
|
135
|
+
// Regional indicator pairs form flag emoji (2 columns wide)
|
|
136
|
+
if (baseCodePoint >= 0x1f1e6 && baseCodePoint <= 0x1f1ff) return true;
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// HOT PATH: Use only basic string/char code operations for maximum performance
|
|
141
|
+
function parseLinkCode(string: string, offset: number): string | undefined {
|
|
142
|
+
string = string.slice(offset);
|
|
143
|
+
for (let index = 1; index < linkCodePrefixCharCodes.length; index++) {
|
|
144
|
+
if (string.charCodeAt(index) !== linkCodePrefixCharCodes[index]) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Find the semicolon that ends params
|
|
150
|
+
const paramsEndIndex = string.indexOf(";", linkCodePrefix.length);
|
|
151
|
+
if (paramsEndIndex === -1) return;
|
|
152
|
+
|
|
153
|
+
// This is a link code (with or without the URL part). Find the end of it.
|
|
154
|
+
const endIndex = findOscTerminatorIndex(string, paramsEndIndex + 1);
|
|
155
|
+
if (endIndex === -1) return;
|
|
156
|
+
|
|
157
|
+
return string.slice(0, endIndex + 1);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// HOT PATH: Generic fallback for non-link OSC sequences (window title, etc.)
|
|
161
|
+
function parseOscSequence(string: string, offset: number): string | undefined {
|
|
162
|
+
string = string.slice(offset);
|
|
163
|
+
// Find the OSC terminator (starting after "ESC ]")
|
|
164
|
+
const endIndex = findOscTerminatorIndex(string, 2);
|
|
165
|
+
if (endIndex === -1) return;
|
|
166
|
+
return string.slice(0, endIndex + 1);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
Finds the index of the last character of the first OSC terminator at or after
|
|
171
|
+
`startIndex`. Recognizes BEL (\u0007), C1 ST (\u009C), and ESC+backslash.
|
|
172
|
+
Returns -1 if no terminator is found.
|
|
173
|
+
*/
|
|
174
|
+
function findOscTerminatorIndex(string: string, startIndex: number): number {
|
|
175
|
+
for (let index = startIndex; index < string.length; index++) {
|
|
176
|
+
const charCode = string.charCodeAt(index);
|
|
177
|
+
if (charCode === CC_BEL) return index;
|
|
178
|
+
if (charCode === CC_C1_ST) return index;
|
|
179
|
+
if (
|
|
180
|
+
charCode === CC_ESC &&
|
|
181
|
+
index + 1 < string.length &&
|
|
182
|
+
string.charCodeAt(index + 1) === CC_BACKSLASH
|
|
183
|
+
) {
|
|
184
|
+
return index + 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return -1;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
Scans through the given string and finds the index of the last character of an
|
|
193
|
+
SGR sequence like `\u001B[38;2;123;123;123m`. This assumes that the string has
|
|
194
|
+
been checked to start with `\u001B[`. Returns -1 if no valid SGR sequence is
|
|
195
|
+
found.
|
|
196
|
+
*/
|
|
197
|
+
function findSgrSequenceEndIndex(string: string): number {
|
|
198
|
+
for (let index = 2; index < string.length; index++) {
|
|
199
|
+
const charCode = string.charCodeAt(index);
|
|
200
|
+
// m marks the end of the SGR sequence
|
|
201
|
+
if (charCode === CC_M) return index;
|
|
202
|
+
// Digits and semicolons are valid
|
|
203
|
+
if (charCode === CC_SEMI) continue;
|
|
204
|
+
if (charCode >= CC_0 && charCode <= CC_9) continue;
|
|
205
|
+
// Everything else is invalid
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return -1;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// HOT PATH: Use only basic string/char code operations for maximum performance
|
|
213
|
+
function parseSgrSequence(string: string, offset: number): string | undefined {
|
|
214
|
+
string = string.slice(offset);
|
|
215
|
+
const endIndex = findSgrSequenceEndIndex(string);
|
|
216
|
+
if (endIndex === -1) return;
|
|
217
|
+
return string.slice(0, endIndex + 1);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
Splits compound SGR sequences like `\u001B[1;3;31m` into individual components.
|
|
222
|
+
*/
|
|
223
|
+
function splitCompoundSgrSequences(code: string): string[] {
|
|
224
|
+
if (!code.includes(";")) {
|
|
225
|
+
// Not a compound code
|
|
226
|
+
return [code];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const codeParts = code
|
|
230
|
+
// Strip off the escape sequences \u001B[ and m
|
|
231
|
+
.slice(2, -1)
|
|
232
|
+
.split(";");
|
|
233
|
+
|
|
234
|
+
const result: string[] = [];
|
|
235
|
+
for (let index = 0; index < codeParts.length; index++) {
|
|
236
|
+
const rawCode = codeParts[index]!;
|
|
237
|
+
// Keep 8-bit and 24-bit color codes (containing multiple ";") together
|
|
238
|
+
if (rawCode === "38" || rawCode === "48") {
|
|
239
|
+
if (index + 2 < codeParts.length && codeParts[index + 1] === "5") {
|
|
240
|
+
// 8-bit color, followed by another number
|
|
241
|
+
result.push(codeParts.slice(index, index + 3).join(";"));
|
|
242
|
+
index += 2;
|
|
243
|
+
continue;
|
|
244
|
+
} else if (index + 4 < codeParts.length && codeParts[index + 1] === "2") {
|
|
245
|
+
// 24-bit color, followed by three numbers
|
|
246
|
+
result.push(codeParts.slice(index, index + 5).join(";"));
|
|
247
|
+
index += 4;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Not a (valid) 8/24-bit color code, push as is
|
|
253
|
+
result.push(rawCode);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return result.map((part) => `${ESC}[${part}m`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function tokenize(string: string, endChar = Number.POSITIVE_INFINITY): Token[] {
|
|
260
|
+
const result: Token[] = [];
|
|
261
|
+
let visible = 0;
|
|
262
|
+
let codeEndIndex = 0;
|
|
263
|
+
|
|
264
|
+
for (const { segment, index } of segmenter.segment(string)) {
|
|
265
|
+
// Skip segments consumed as part of an ANSI sequence
|
|
266
|
+
if (index < codeEndIndex) continue;
|
|
267
|
+
|
|
268
|
+
const codePoint = segment.codePointAt(0)!;
|
|
269
|
+
if (ESCAPES.has(codePoint)) {
|
|
270
|
+
let code: string | undefined;
|
|
271
|
+
// Peek the next code point to determine the type of ANSI sequence
|
|
272
|
+
const nextCodePoint = string.codePointAt(index + 1);
|
|
273
|
+
if (nextCodePoint === CC_OSC) {
|
|
274
|
+
// ] = operating system commands
|
|
275
|
+
code = parseLinkCode(string, index);
|
|
276
|
+
if (code) {
|
|
277
|
+
// OSC 8 hyperlinks are paired codes with an endCode
|
|
278
|
+
result.push({
|
|
279
|
+
type: "ansi",
|
|
280
|
+
code,
|
|
281
|
+
endCode: getEndCode(code),
|
|
282
|
+
});
|
|
283
|
+
} else {
|
|
284
|
+
// Other OSC sequences (window title, etc.) are self-contained
|
|
285
|
+
// control codes with no endCode.
|
|
286
|
+
code = parseOscSequence(string, index);
|
|
287
|
+
if (code) {
|
|
288
|
+
result.push({
|
|
289
|
+
type: "control",
|
|
290
|
+
code,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
} else if (nextCodePoint === CC_CSI) {
|
|
295
|
+
// [ = control sequence introducer, like SGR sequences [...m
|
|
296
|
+
code = parseSgrSequence(string, index);
|
|
297
|
+
if (code) {
|
|
298
|
+
// Split compound codes into individual tokens
|
|
299
|
+
for (const individualCode of splitCompoundSgrSequences(code)) {
|
|
300
|
+
result.push({
|
|
301
|
+
type: "ansi",
|
|
302
|
+
code: individualCode,
|
|
303
|
+
endCode: getEndCode(individualCode),
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (code) {
|
|
310
|
+
codeEndIndex = index + code.length;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const fullWidth = isFullwidthGrapheme(segment, codePoint);
|
|
316
|
+
result.push({
|
|
317
|
+
type: "char",
|
|
318
|
+
value: segment,
|
|
319
|
+
fullWidth,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
visible += fullWidth ? 2 : 1;
|
|
323
|
+
if (visible >= endChar) {
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
Reduces the given array of ANSI codes to the minimum necessary to render with
|
|
333
|
+
the same style.
|
|
334
|
+
*/
|
|
335
|
+
export function reduceAnsiCodes(codes: readonly AnsiCode[]): AnsiCode[] {
|
|
336
|
+
return reduceAnsiCodesIncremental([], codes);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
Like {@link reduceAnsiCodes}, but assumes that `codes` is already reduced.
|
|
341
|
+
Further reductions are only done for the items in `newCodes`.
|
|
342
|
+
*/
|
|
343
|
+
export function reduceAnsiCodesIncremental(
|
|
344
|
+
codes: readonly AnsiCode[],
|
|
345
|
+
newCodes: readonly AnsiCode[],
|
|
346
|
+
): AnsiCode[] {
|
|
347
|
+
let result = [...codes];
|
|
348
|
+
|
|
349
|
+
for (const code of newCodes) {
|
|
350
|
+
if (code.code === styles.reset.open) {
|
|
351
|
+
// Reset code, disable all codes
|
|
352
|
+
result = [];
|
|
353
|
+
} else if (endCodesSet.has(code.code)) {
|
|
354
|
+
// This is an end code, disable all matching start codes
|
|
355
|
+
result = result.filter((existing) => existing.endCode !== code.code);
|
|
356
|
+
} else if (isIntensityCode(code)) {
|
|
357
|
+
// Intensity codes (1m, 2m) can coexist (both end with 22m). Only add
|
|
358
|
+
// if the exact same code is not already present.
|
|
359
|
+
if (
|
|
360
|
+
!result.some((existing) => existing.code === code.code && existing.endCode === code.endCode)
|
|
361
|
+
) {
|
|
362
|
+
result.push(code);
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
// This is a start code. Remove codes it "overrides" (same endCode),
|
|
366
|
+
// then add it.
|
|
367
|
+
result = result.filter((existing) => existing.endCode !== code.endCode);
|
|
368
|
+
result.push(code);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return result;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
Returns the combination of ANSI codes needed to undo the given ANSI codes.
|
|
377
|
+
*/
|
|
378
|
+
export function undoAnsiCodes(codes: readonly AnsiCode[]): AnsiCode[] {
|
|
379
|
+
return reduceAnsiCodes(codes)
|
|
380
|
+
.reverse()
|
|
381
|
+
.map((code) => ({
|
|
382
|
+
...code,
|
|
383
|
+
code: code.endCode,
|
|
384
|
+
}));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
Returns the minimum amount of ANSI codes necessary to get from the compound
|
|
389
|
+
style `from` to `to`. Both are expected to be reduced.
|
|
390
|
+
*/
|
|
391
|
+
export function diffAnsiCodes(from: readonly AnsiCode[], to: readonly AnsiCode[]): AnsiCode[] {
|
|
392
|
+
const endCodesInTo = new Set(to.map((code) => code.endCode));
|
|
393
|
+
const startCodesInTo = new Set(to.map((code) => code.code));
|
|
394
|
+
const startCodesInFrom = new Set(from.map((code) => code.code));
|
|
395
|
+
|
|
396
|
+
return [
|
|
397
|
+
// Disable all styles in `from` that are removed in `to`; keep the rest.
|
|
398
|
+
...undoAnsiCodes(
|
|
399
|
+
from.filter((code) => {
|
|
400
|
+
// Intensity codes (1m, 2m) can coexist (both end with 22m), so
|
|
401
|
+
// check the start codes for those to not miss a reset.
|
|
402
|
+
if (isIntensityCode(code)) {
|
|
403
|
+
return !startCodesInTo.has(code.code);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return !endCodesInTo.has(code.endCode);
|
|
407
|
+
}),
|
|
408
|
+
),
|
|
409
|
+
// Add all styles in `to` that don't exist in `from`
|
|
410
|
+
...to.filter((code) => !startCodesInFrom.has(code.code)),
|
|
411
|
+
];
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export function styledCharsFromTokens(tokens: readonly Token[]): StyledChar[] {
|
|
415
|
+
let codes: AnsiCode[] = [];
|
|
416
|
+
const result: StyledChar[] = [];
|
|
417
|
+
|
|
418
|
+
for (const token of tokens) {
|
|
419
|
+
if (token.type === "ansi") {
|
|
420
|
+
codes = reduceAnsiCodesIncremental(codes, [token]);
|
|
421
|
+
} else if (token.type === "char") {
|
|
422
|
+
result.push({
|
|
423
|
+
...token,
|
|
424
|
+
styles: [...codes],
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return result;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export function styledCharsToString(chars: readonly StyledChar[]): string {
|
|
433
|
+
let result = "";
|
|
434
|
+
|
|
435
|
+
for (let index = 0; index < chars.length; index++) {
|
|
436
|
+
const char = chars[index]!;
|
|
437
|
+
|
|
438
|
+
if (index === 0) {
|
|
439
|
+
result += ansiCodesToString(char.styles);
|
|
440
|
+
} else {
|
|
441
|
+
result += ansiCodesToString(diffAnsiCodes(chars[index - 1]!.styles, char.styles));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
result += char.value;
|
|
445
|
+
|
|
446
|
+
// Reset active styles at the end of the string
|
|
447
|
+
if (index === chars.length - 1) {
|
|
448
|
+
result += ansiCodesToString(diffAnsiCodes(char.styles, []));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return result;
|
|
453
|
+
}
|