Repository navigation
Expand file tree
/
Copy pathtokenize-ansi.js
More file actions
397 lines (336 loc) · 11.7 KB
/
Copy pathtokenize-ansi.js
File metadata and controls
397 lines (336 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import ansiStyles from 'ansi-styles';
import stringWidth from 'string-width';
const ESCAPE_CODE_POINT = 27;
const C1_DCS_CODE_POINT = 144;
const C1_SOS_CODE_POINT = 152;
const C1_CSI_CODE_POINT = 155;
const C1_ST_CODE_POINT = 156;
const C1_OSC_CODE_POINT = 157;
const C1_PM_CODE_POINT = 158;
const C1_APC_CODE_POINT = 159;
const CONTROL_STRING_STARTS = new Set([
C1_DCS_CODE_POINT,
C1_SOS_CODE_POINT,
C1_OSC_CODE_POINT,
C1_PM_CODE_POINT,
C1_APC_CODE_POINT,
]);
const ESCAPES = new Set([
ESCAPE_CODE_POINT,
C1_CSI_CODE_POINT,
C1_ST_CODE_POINT,
...CONTROL_STRING_STARTS,
]);
const ESCAPE = '\u{1B}';
const ANSI_BELL = '\u{7}';
const ANSI_CSI = '[';
const ANSI_OSC = ']';
const ANSI_SGR_TERMINATOR = 'm';
const ANSI_OSC_TERMINATOR = '\\';
const ANSI_STRING_TERMINATOR = `${ESCAPE}${ANSI_OSC_TERMINATOR}`;
const C1_OSC = '\u{9D}';
const C1_STRING_TERMINATOR = '\u{9C}';
const ANSI_HYPERLINK_ESC_PREFIX = `${ESCAPE}${ANSI_OSC}8;`;
const ANSI_HYPERLINK_C1_PREFIX = `${C1_OSC}8;`;
// ECMA-48 CSI format: parameter bytes 0x30-0x3F, intermediates 0x20-0x2F, final 0x40-0x7E.
const CODE_POINT_CSI_PARAMETER_START = '0'.codePointAt(0);
const CODE_POINT_CSI_PARAMETER_END = '?'.codePointAt(0);
const CODE_POINT_CSI_INTERMEDIATE_START = ' '.codePointAt(0);
const CODE_POINT_CSI_INTERMEDIATE_END = '/'.codePointAt(0);
const CODE_POINT_CSI_FINAL_START = '@'.codePointAt(0);
const CODE_POINT_CSI_FINAL_END = '~'.codePointAt(0);
const SGR_RESET_CODE = 0;
const SGR_EXTENDED_COLOR_END_CODES = new Map([
[38, 39],
[48, 49],
[58, 59],
]);
const SGR_COLOR_LENGTHS = new Map([
[5, 3],
[2, 5],
]);
const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {granularity: 'grapheme'});
const endCodeNumbers = new Set();
for (const [, end] of ansiStyles.codes) {
endCodeNumbers.add(end);
}
function isCsiParameterCharacter(codePoint) {
return codePoint >= CODE_POINT_CSI_PARAMETER_START && codePoint <= CODE_POINT_CSI_PARAMETER_END;
}
function isCsiIntermediateCharacter(codePoint) {
return codePoint >= CODE_POINT_CSI_INTERMEDIATE_START && codePoint <= CODE_POINT_CSI_INTERMEDIATE_END;
}
function isCsiFinalCharacter(codePoint) {
return codePoint >= CODE_POINT_CSI_FINAL_START && codePoint <= CODE_POINT_CSI_FINAL_END;
}
function createControlParseResult(code, endIndex) {
return {
token: {
type: 'control',
code,
},
endIndex,
};
}
function getSgrPrefix(code) {
return code.startsWith('\u{9B}') ? '\u{9B}' : `${ESCAPE}${ANSI_CSI}`;
}
function createSgrCode(prefix, values) {
return `${prefix}${values.join(';')}${ANSI_SGR_TERMINATOR}`;
}
function getSgrFragments(code) {
const fragments = [];
const sgrPrefix = getSgrPrefix(code);
const rawCodes = code.slice(sgrPrefix.length, -1).split(';');
let index = 0;
while (index < rawCodes.length) {
// Read the leading numeric parameter while preserving colon-delimited subparameters in the emitted code.
// eslint-disable-next-line unicorn/prefer-number-coercion
const codeNumber = Number.parseInt(rawCodes[index] || '0', 10);
if (Number.isNaN(codeNumber)) {
index++;
continue;
}
if (codeNumber === SGR_RESET_CODE) {
fragments.push({type: 'reset'});
index++;
continue;
}
if (endCodeNumbers.has(codeNumber)) {
fragments.push({
type: 'end',
endCode: ansiStyles.color.ansi(codeNumber),
});
index++;
continue;
}
const extendedColorEndCode = SGR_EXTENDED_COLOR_END_CODES.get(codeNumber);
let fragmentLength = 1;
if (extendedColorEndCode !== undefined && !rawCodes[index].includes(':')) {
// A colon-delimited color is already complete; only semicolon-delimited colors consume subsequent parameters.
// eslint-disable-next-line unicorn/prefer-number-coercion -- Same reason as above.
const colorType = Number.parseInt(rawCodes[index + 1], 10);
const colorLength = SGR_COLOR_LENGTHS.get(colorType);
if (colorLength !== undefined && index + colorLength <= rawCodes.length) {
fragmentLength = colorLength;
}
}
const endCode = extendedColorEndCode ?? ansiStyles.codes.get(codeNumber);
fragments.push({
type: 'start',
code: createSgrCode(sgrPrefix, rawCodes.slice(index, index + fragmentLength)),
endCode: endCode === undefined ? ansiStyles.reset.open : ansiStyles.color.ansi(endCode),
});
index += fragmentLength;
}
if (fragments.length === 0) {
fragments.push({type: 'reset'});
}
return fragments;
}
function parseCsiCode(string, index) {
const escapeCodePoint = string.codePointAt(index);
let sequenceStartIndex;
if (escapeCodePoint === ESCAPE_CODE_POINT) {
if (string[index + 1] !== ANSI_CSI) {
return;
}
sequenceStartIndex = index + 2;
} else if (escapeCodePoint === C1_CSI_CODE_POINT) {
sequenceStartIndex = index + 1;
} else {
return;
}
for (let sequenceIndex = sequenceStartIndex; sequenceIndex < string.length; sequenceIndex++) {
const codePoint = string.codePointAt(sequenceIndex);
if (isCsiFinalCharacter(codePoint)) {
const code = string.slice(index, sequenceIndex + 1);
if (string[sequenceIndex] !== ANSI_SGR_TERMINATOR || !/^[\d:;]*$/v.test(string.slice(sequenceStartIndex, sequenceIndex))) {
return createControlParseResult(code, sequenceIndex + 1);
}
return {
token: {
type: 'sgr',
code,
fragments: getSgrFragments(code),
},
endIndex: sequenceIndex + 1,
};
}
if (isCsiParameterCharacter(codePoint) || isCsiIntermediateCharacter(codePoint)) {
continue;
}
const endIndex = sequenceIndex;
return createControlParseResult(string.slice(index, endIndex), endIndex);
}
return createControlParseResult(string.slice(index), string.length);
}
function createControlStringParseResult(string, index, endIndex, terminator) {
const code = string.slice(index, endIndex);
const hyperlinkPrefix = code.startsWith(ANSI_HYPERLINK_ESC_PREFIX) ? ANSI_HYPERLINK_ESC_PREFIX : ANSI_HYPERLINK_C1_PREFIX;
if (!code.startsWith(hyperlinkPrefix)) {
return createControlParseResult(code, endIndex);
}
const uriStart = code.indexOf(';', hyperlinkPrefix.length);
const contentEnd = code.length - terminator.length;
if (uriStart === -1 || uriStart >= contentEnd) {
return createControlParseResult(code, endIndex);
}
return {
token: {
type: 'hyperlink',
code,
action: uriStart + 1 === contentEnd ? 'close' : 'open',
closePrefix: `${hyperlinkPrefix};`,
terminator,
},
endIndex,
};
}
function parseControlStringCode(string, index) {
const codePoint = string.codePointAt(index);
// ESC followed by a byte from 0x40 to 0x5F is the 7-bit representation of the corresponding C1 control.
const command = codePoint === ESCAPE_CODE_POINT ? string.codePointAt(index + 1) + 0x40 : codePoint;
const sequenceStartIndex = index + (codePoint === ESCAPE_CODE_POINT ? 2 : 1);
if (command === C1_ST_CODE_POINT) {
return createControlParseResult(string.slice(index, sequenceStartIndex), sequenceStartIndex);
}
if (!CONTROL_STRING_STARTS.has(command)) {
return;
}
for (let sequenceIndex = sequenceStartIndex; sequenceIndex < string.length; sequenceIndex++) {
// OSC accepts ST (ECMA-48) and BEL (xterm compatibility extension).
if (command === C1_OSC_CODE_POINT && string[sequenceIndex] === ANSI_BELL) {
return createControlStringParseResult(string, index, sequenceIndex + 1, ANSI_BELL);
}
if (
string[sequenceIndex] === ESCAPE
&& string[sequenceIndex + 1] === ANSI_OSC_TERMINATOR
) {
return createControlStringParseResult(string, index, sequenceIndex + 2, ANSI_STRING_TERMINATOR);
}
if (string[sequenceIndex] === C1_STRING_TERMINATOR) {
return createControlStringParseResult(string, index, sequenceIndex + 1, C1_STRING_TERMINATOR);
}
}
return createControlParseResult(string.slice(index), string.length);
}
function parseAnsiCode(string, index) {
return parseControlStringCode(string, index) || parseCsiCode(string, index);
}
function appendAnsiTokens(string, index, tokens) {
while (index < string.length) {
const nextCodePoint = string.codePointAt(index);
if (!ESCAPES.has(nextCodePoint)) {
break;
}
const escapeCode = parseAnsiCode(string, index);
if (!escapeCode) {
break;
}
tokens.push(escapeCode.token);
index = escapeCode.endIndex;
}
return index;
}
// Columns come from `string-width`, so a slice is never wider than what `string-width` measures for it.
function parseCharacterTokenWithRawSegmentation(index, graphemeSegments) {
const segment = graphemeSegments.containing(index);
if (!segment || segment.index !== index) {
return;
}
return {
token: {
type: 'character',
// Intentionally preserve UAX29 behavior (GB3): CRLF is one grapheme cluster.
value: segment.segment,
visibleWidth: stringWidth(segment.segment),
},
endIndex: index + segment.segment.length,
};
}
// Each grapheme owns its text and internal ANSI tokens, so it can be included or excluded as a whole without tracking scalar continuations.
// Slow path for when ANSI codes sit inside a grapheme, for example between a letter and its combining mark. It segments the text with the ANSI codes removed, and then groups the original tokens by grapheme.
function tokenizeAnsiWithVisibleSegmentation(string, {endCharacter = Infinity} = {}) {
const tokens = [];
let visibleString = '';
let index = 0;
while (index < string.length) {
const codePoint = string.codePointAt(index);
if (ESCAPES.has(codePoint)) {
const code = parseAnsiCode(string, index);
if (code) {
tokens.push(code.token);
index = code.endIndex;
continue;
}
}
const value = String.fromCodePoint(codePoint);
// Reuse the original tokens for segmentation and output so ANSI codes are parsed only once.
visibleString += value;
tokens.push({type: 'text', value, visibleEnd: visibleString.length});
index += value.length;
}
const graphemes = [];
let tokenIndex = 0;
let visibleCount = 0;
for (const {segment, index: segmentIndex} of GRAPHEME_SEGMENTER.segment(visibleString)) {
while (tokens[tokenIndex].type !== 'text') {
graphemes.push(tokens[tokenIndex]);
tokenIndex++;
}
if (segmentIndex > 0 && visibleCount >= endCharacter) {
return graphemes;
}
const startIndex = tokenIndex;
const visibleEnd = segmentIndex + segment.length;
do {
tokenIndex++;
} while (tokens[tokenIndex - 1].visibleEnd !== visibleEnd);
const visibleWidth = stringWidth(segment);
graphemes.push({
type: 'character',
value: segment,
visibleWidth,
parts: tokens.slice(startIndex, tokenIndex),
});
visibleCount += visibleWidth;
}
while (tokenIndex < tokens.length) {
graphemes.push(tokens[tokenIndex]);
tokenIndex++;
}
return graphemes;
}
function areValuesInSameGrapheme(leftValue, rightValue) {
const segment = GRAPHEME_SEGMENTER.segment(leftValue + rightValue).containing(leftValue.length);
return segment.index < leftValue.length;
}
// Fast path that tokenizes one grapheme at a time. It falls back to `tokenizeAnsiWithVisibleSegmentation` when an ANSI code splits a grapheme.
export default function tokenizeAnsi(string, {endCharacter = Infinity} = {}) {
const tokens = [];
const graphemeSegments = GRAPHEME_SEGMENTER.segment(string);
let index = 0;
let visibleCount = 0;
let previousCharacter;
while (index < string.length) {
index = appendAnsiTokens(string, index, tokens);
if (index === string.length) {
break;
}
const characterToken = parseCharacterTokenWithRawSegmentation(index, graphemeSegments);
if (!characterToken || (previousCharacter
&& index > previousCharacter.endIndex
&& areValuesInSameGrapheme(previousCharacter.token.value, characterToken.token.value))) {
return tokenizeAnsiWithVisibleSegmentation(string, {endCharacter});
}
if (previousCharacter && visibleCount >= endCharacter) {
break;
}
tokens.push(characterToken.token);
index = characterToken.endIndex;
visibleCount += characterToken.token.visibleWidth;
previousCharacter = characterToken;
}
return tokens;
}