-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtokenize-ansi.js
More file actions
752 lines (640 loc) · 19.3 KB
/
Copy pathtokenize-ansi.js
File metadata and controls
752 lines (640 loc) · 19.3 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import ansiStyles from 'ansi-styles';
import isFullwidthCodePoint from 'is-fullwidth-code-point';
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 ESCAPES = new Set([
ESCAPE_CODE_POINT,
C1_DCS_CODE_POINT,
C1_SOS_CODE_POINT,
C1_CSI_CODE_POINT,
C1_ST_CODE_POINT,
C1_OSC_CODE_POINT,
C1_PM_CODE_POINT,
C1_APC_CODE_POINT,
]);
const ESCAPE = '\u001B';
const ANSI_BELL = '\u0007';
const ANSI_CSI = '[';
const ANSI_OSC = ']';
const ANSI_DCS = 'P';
const ANSI_SOS = 'X';
const ANSI_PM = '^';
const ANSI_APC = '_';
const ANSI_SGR_TERMINATOR = 'm';
const ANSI_OSC_TERMINATOR = '\\';
const ANSI_STRING_TERMINATOR = `${ESCAPE}${ANSI_OSC_TERMINATOR}`;
const C1_OSC = '\u009D';
const C1_STRING_TERMINATOR = '\u009C';
const ANSI_HYPERLINK_ESC_PREFIX = `${ESCAPE}${ANSI_OSC}8;`;
const ANSI_HYPERLINK_C1_PREFIX = `${C1_OSC}8;`;
const ANSI_HYPERLINK_ESC_CLOSE = `${ANSI_HYPERLINK_ESC_PREFIX};`;
const ANSI_HYPERLINK_C1_CLOSE = `${ANSI_HYPERLINK_C1_PREFIX};`;
const CODE_POINT_0 = '0'.codePointAt(0);
const CODE_POINT_9 = '9'.codePointAt(0);
const CODE_POINT_SEMICOLON = ';'.codePointAt(0);
const CODE_POINT_COLON = ':'.codePointAt(0);
// 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 REGIONAL_INDICATOR_SYMBOL_LETTER_A = 127_462;
const REGIONAL_INDICATOR_SYMBOL_LETTER_Z = 127_487;
const SGR_RESET_CODE = 0;
const SGR_EXTENDED_FOREGROUND_CODE = 38;
const SGR_DEFAULT_FOREGROUND_CODE = 39;
const SGR_EXTENDED_BACKGROUND_CODE = 48;
const SGR_DEFAULT_BACKGROUND_CODE = 49;
const SGR_COLOR_TYPE_ANSI_256 = 5;
const SGR_COLOR_TYPE_TRUECOLOR = 2;
const SGR_ANSI_256_FRAGMENT_LENGTH = 3;
const SGR_TRUECOLOR_FRAGMENT_LENGTH = 5;
const SGR_ANSI_256_LAST_PARAMETER_OFFSET = 2;
const SGR_TRUECOLOR_LAST_PARAMETER_OFFSET = 4;
const VARIATION_SELECTOR_16_CODE_POINT = 65_039;
const COMBINING_ENCLOSING_KEYCAP_CODE_POINT = 8419;
const EMOJI_PRESENTATION_GRAPHEME_REGEX = /\p{Emoji_Presentation}/v;
const GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {granularity: 'grapheme'});
const endCodeNumbers = new Set();
for (const [, end] of ansiStyles.codes) {
endCodeNumbers.add(end);
}
function isSgrParameterCharacter(codePoint) {
return (
(codePoint >= CODE_POINT_0 && codePoint <= CODE_POINT_9)
|| codePoint === CODE_POINT_SEMICOLON
|| codePoint === CODE_POINT_COLON
);
}
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 isRegionalIndicatorCodePoint(codePoint) {
return codePoint >= REGIONAL_INDICATOR_SYMBOL_LETTER_A && codePoint <= REGIONAL_INDICATOR_SYMBOL_LETTER_Z;
}
function createControlParseResult(code, endIndex) {
return {
token: {
type: 'control',
code,
},
endIndex,
};
}
function isEmojiStyleGrapheme(grapheme) {
if (EMOJI_PRESENTATION_GRAPHEME_REGEX.test(grapheme)) {
return true;
}
for (const character of grapheme) {
const codePoint = character.codePointAt(0);
if (
codePoint === VARIATION_SELECTOR_16_CODE_POINT
|| codePoint === COMBINING_ENCLOSING_KEYCAP_CODE_POINT
) {
return true;
}
}
return false;
}
function getGraphemeWidth(grapheme) {
let regionalIndicatorCount = 0;
for (const character of grapheme) {
const codePoint = character.codePointAt(0);
if (isFullwidthCodePoint(codePoint)) {
return 2;
}
if (isRegionalIndicatorCodePoint(codePoint)) {
regionalIndicatorCount++;
}
}
if (regionalIndicatorCount >= 1) {
return 2;
}
if (isEmojiStyleGrapheme(grapheme)) {
return 2;
}
return 1;
}
function getSgrPrefix(code) {
if (code.startsWith('\u009B')) {
return '\u009B';
}
return `${ESCAPE}${ANSI_CSI}`;
}
function createSgrCode(prefix, values) {
return `${prefix}${values.join(';')}${ANSI_SGR_TERMINATOR}`;
}
function getSgrFragments(code) {
const fragments = [];
const sgrPrefix = getSgrPrefix(code);
let parameterString;
if (code.startsWith(`${ESCAPE}${ANSI_CSI}`)) {
parameterString = code.slice(2, -1);
} else if (code.startsWith('\u009B')) {
parameterString = code.slice(1, -1);
} else {
return fragments;
}
const rawCodes = parameterString.length === 0 ? [String(SGR_RESET_CODE)] : parameterString.split(';');
let index = 0;
while (index < rawCodes.length) {
const codeNumber = Number.parseInt(rawCodes[index], 10);
if (Number.isNaN(codeNumber)) {
index++;
continue;
}
if (codeNumber === SGR_RESET_CODE) {
fragments.push({type: 'reset'});
index++;
continue;
}
if (codeNumber === SGR_EXTENDED_FOREGROUND_CODE || codeNumber === SGR_EXTENDED_BACKGROUND_CODE) {
const colorType = Number.parseInt(rawCodes[index + 1], 10);
if (colorType === SGR_COLOR_TYPE_ANSI_256 && index + SGR_ANSI_256_LAST_PARAMETER_OFFSET < rawCodes.length) {
const openCode = createSgrCode(sgrPrefix, rawCodes.slice(index, index + SGR_ANSI_256_FRAGMENT_LENGTH));
fragments.push({
type: 'start',
code: openCode,
endCode: ansiStyles.color.ansi(codeNumber === SGR_EXTENDED_FOREGROUND_CODE ? SGR_DEFAULT_FOREGROUND_CODE : SGR_DEFAULT_BACKGROUND_CODE),
});
index += SGR_ANSI_256_FRAGMENT_LENGTH;
continue;
}
if (colorType === SGR_COLOR_TYPE_TRUECOLOR && index + SGR_TRUECOLOR_LAST_PARAMETER_OFFSET < rawCodes.length) {
const openCode = createSgrCode(sgrPrefix, rawCodes.slice(index, index + SGR_TRUECOLOR_FRAGMENT_LENGTH));
fragments.push({
type: 'start',
code: openCode,
endCode: ansiStyles.color.ansi(codeNumber === SGR_EXTENDED_FOREGROUND_CODE ? SGR_DEFAULT_FOREGROUND_CODE : SGR_DEFAULT_BACKGROUND_CODE),
});
index += SGR_TRUECOLOR_FRAGMENT_LENGTH;
continue;
}
const openCode = createSgrCode(sgrPrefix, [rawCodes[index]]);
fragments.push({
type: 'start',
code: openCode,
endCode: ansiStyles.color.ansi(codeNumber === SGR_EXTENDED_FOREGROUND_CODE ? SGR_DEFAULT_FOREGROUND_CODE : SGR_DEFAULT_BACKGROUND_CODE),
});
index++;
continue;
}
if (endCodeNumbers.has(codeNumber)) {
fragments.push({
type: 'end',
endCode: ansiStyles.color.ansi(codeNumber),
});
index++;
continue;
}
const mappedEndCode = ansiStyles.codes.get(codeNumber);
if (mappedEndCode !== undefined) {
const openCode = createSgrCode(sgrPrefix, [rawCodes[index]]);
fragments.push({
type: 'start',
code: openCode,
endCode: ansiStyles.color.ansi(mappedEndCode),
});
index++;
continue;
}
const openCode = createSgrCode(sgrPrefix, [rawCodes[index]]);
fragments.push({
type: 'start',
code: openCode,
endCode: ansiStyles.reset.open,
});
index++;
}
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;
}
let hasCanonicalSgrParameters = true;
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 || !hasCanonicalSgrParameters) {
return createControlParseResult(code, sequenceIndex + 1);
}
return {
token: {
type: 'sgr',
code,
fragments: getSgrFragments(code),
},
endIndex: sequenceIndex + 1,
};
}
if (isCsiParameterCharacter(codePoint)) {
if (!isSgrParameterCharacter(codePoint)) {
hasCanonicalSgrParameters = false;
}
continue;
}
if (isCsiIntermediateCharacter(codePoint)) {
hasCanonicalSgrParameters = false;
continue;
}
const endIndex = sequenceIndex;
return createControlParseResult(string.slice(index, endIndex), endIndex);
}
return createControlParseResult(string.slice(index), string.length);
}
function parseHyperlinkCode(string, index) {
let hyperlinkPrefix;
let hyperlinkClose;
const codePoint = string.codePointAt(index);
if (
codePoint === ESCAPE_CODE_POINT
&& string.startsWith(ANSI_HYPERLINK_ESC_PREFIX, index)
) {
hyperlinkPrefix = ANSI_HYPERLINK_ESC_PREFIX;
hyperlinkClose = ANSI_HYPERLINK_ESC_CLOSE;
} else if (
codePoint === C1_OSC_CODE_POINT
&& string.startsWith(ANSI_HYPERLINK_C1_PREFIX, index)
) {
hyperlinkPrefix = ANSI_HYPERLINK_C1_PREFIX;
hyperlinkClose = ANSI_HYPERLINK_C1_CLOSE;
} else {
return;
}
const uriStart = string.indexOf(';', index + hyperlinkPrefix.length);
if (uriStart === -1) {
return createControlParseResult(string.slice(index), string.length);
}
for (let sequenceIndex = uriStart + 1; sequenceIndex < string.length; sequenceIndex++) {
const character = string[sequenceIndex];
if (character === ANSI_BELL) {
const code = string.slice(index, sequenceIndex + 1);
const action = sequenceIndex === uriStart + 1 ? 'close' : 'open';
return {
token: {
type: 'hyperlink',
code,
action,
closePrefix: hyperlinkClose,
terminator: ANSI_BELL,
},
endIndex: sequenceIndex + 1,
};
}
if (
character === ESCAPE
&& string[sequenceIndex + 1] === ANSI_OSC_TERMINATOR
) {
const code = string.slice(index, sequenceIndex + 2);
const action = sequenceIndex === uriStart + 1 ? 'close' : 'open';
return {
token: {
type: 'hyperlink',
code,
action,
closePrefix: hyperlinkClose,
terminator: ANSI_STRING_TERMINATOR,
},
endIndex: sequenceIndex + 2,
};
}
if (character === C1_STRING_TERMINATOR) {
const code = string.slice(index, sequenceIndex + 1);
const action = sequenceIndex === uriStart + 1 ? 'close' : 'open';
return {
token: {
type: 'hyperlink',
code,
action,
closePrefix: hyperlinkClose,
terminator: C1_STRING_TERMINATOR,
},
endIndex: sequenceIndex + 1,
};
}
}
return createControlParseResult(string.slice(index), string.length);
}
function parseControlStringCode(string, index) {
const codePoint = string.codePointAt(index);
let sequenceStartIndex;
let supportsBellTerminator = false;
switch (codePoint) {
case ESCAPE_CODE_POINT: {
const command = string[index + 1];
switch (command) {
case ANSI_OSC: {
// OSC accepts ST (ECMA-48) and BEL (xterm compatibility extension).
sequenceStartIndex = index + 2;
supportsBellTerminator = true;
break;
}
case ANSI_DCS:
case ANSI_SOS:
case ANSI_PM:
case ANSI_APC: {
sequenceStartIndex = index + 2;
break;
}
case ANSI_OSC_TERMINATOR: {
return createControlParseResult(ANSI_STRING_TERMINATOR, index + 2);
}
default: {
return;
}
}
break;
}
case C1_OSC_CODE_POINT: {
sequenceStartIndex = index + 1;
supportsBellTerminator = true;
break;
}
case C1_DCS_CODE_POINT:
case C1_SOS_CODE_POINT:
case C1_PM_CODE_POINT:
case C1_APC_CODE_POINT: {
sequenceStartIndex = index + 1;
break;
}
case C1_ST_CODE_POINT: {
return createControlParseResult(C1_STRING_TERMINATOR, index + 1);
}
default: {
return;
}
}
for (let sequenceIndex = sequenceStartIndex; sequenceIndex < string.length; sequenceIndex++) {
if (supportsBellTerminator && string[sequenceIndex] === ANSI_BELL) {
return createControlParseResult(string.slice(index, sequenceIndex + 1), sequenceIndex + 1);
}
if (
string[sequenceIndex] === ESCAPE
&& string[sequenceIndex + 1] === ANSI_OSC_TERMINATOR
) {
return createControlParseResult(string.slice(index, sequenceIndex + 2), sequenceIndex + 2);
}
if (string[sequenceIndex] === C1_STRING_TERMINATOR) {
return createControlParseResult(string.slice(index, sequenceIndex + 1), sequenceIndex + 1);
}
}
return createControlParseResult(string.slice(index), string.length);
}
function parseAnsiCode(string, index) {
const codePoint = string.codePointAt(index);
if (codePoint === ESCAPE_CODE_POINT || codePoint === C1_OSC_CODE_POINT) {
const hyperlinkCode = parseHyperlinkCode(string, index);
if (hyperlinkCode) {
return hyperlinkCode;
}
}
const controlStringCode = parseControlStringCode(string, index);
if (controlStringCode) {
return controlStringCode;
}
return parseCsiCode(string, index);
}
function appendTrailingAnsiTokens(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;
}
function parseCharacterTokenWithRawSegmentation(string, 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: getGraphemeWidth(segment.segment),
isGraphemeContinuation: false,
},
endIndex: index + segment.segment.length,
};
}
function collectVisibleCharacters(string) {
const visibleCharacters = [];
let index = 0;
while (index < string.length) {
const codePoint = string.codePointAt(index);
if (ESCAPES.has(codePoint)) {
const code = parseAnsiCode(string, index);
if (code) {
index = code.endIndex;
continue;
}
}
const value = String.fromCodePoint(codePoint);
visibleCharacters.push({
value,
visibleWidth: 1,
isGraphemeContinuation: false,
});
index += value.length;
}
return visibleCharacters;
}
function applyGraphemeMetadata(visibleCharacters) {
if (visibleCharacters.length === 0) {
return;
}
const visibleString = visibleCharacters.map(({value}) => value).join('');
const scalarOffsets = [];
let scalarOffset = 0;
for (const visibleCharacter of visibleCharacters) {
scalarOffsets.push(scalarOffset);
scalarOffset += visibleCharacter.value.length;
}
let scalarIndex = 0;
for (const segment of GRAPHEME_SEGMENTER.segment(visibleString)) {
while (
scalarIndex < visibleCharacters.length
&& scalarOffsets[scalarIndex] < segment.index
) {
scalarIndex++;
}
let graphemeIndex = scalarIndex;
let isFirstInGrapheme = true;
while (
graphemeIndex < visibleCharacters.length
&& scalarOffsets[graphemeIndex] < segment.index + segment.segment.length
) {
visibleCharacters[graphemeIndex].visibleWidth = isFirstInGrapheme ? getGraphemeWidth(segment.segment) : 0;
visibleCharacters[graphemeIndex].isGraphemeContinuation = !isFirstInGrapheme;
isFirstInGrapheme = false;
graphemeIndex++;
}
scalarIndex = graphemeIndex;
}
}
function tokenizeAnsiWithVisibleSegmentation(string, {endCharacter = Number.POSITIVE_INFINITY} = {}) {
const tokens = [];
const visibleCharacters = collectVisibleCharacters(string);
applyGraphemeMetadata(visibleCharacters);
let index = 0;
let visibleCharacterIndex = 0;
let visibleCount = 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);
const visibleCharacter = visibleCharacters[visibleCharacterIndex];
let visibleWidth = isFullwidthCodePoint(codePoint) ? 2 : value.length;
if (visibleCharacter) {
visibleWidth = visibleCharacter.visibleWidth;
}
const token = {
type: 'character',
value,
visibleWidth,
isGraphemeContinuation: visibleCharacter ? visibleCharacter.isGraphemeContinuation : false,
};
tokens.push(token);
index += value.length;
visibleCharacterIndex++;
visibleCount += token.visibleWidth;
if (visibleCount >= endCharacter) {
const nextVisibleCharacter = visibleCharacters[visibleCharacterIndex];
if (
!nextVisibleCharacter
|| !nextVisibleCharacter.isGraphemeContinuation
) {
index = appendTrailingAnsiTokens(string, index, tokens);
break;
}
}
}
return tokens;
}
function areValuesInSameGrapheme(leftValue, rightValue) {
const pair = `${leftValue}${rightValue}`;
const splitIndex = leftValue.length;
for (const segment of GRAPHEME_SEGMENTER.segment(pair)) {
if (segment.index === splitIndex) {
return false;
}
if (segment.index > splitIndex) {
return true;
}
}
return true;
}
function hasAnsiSplitContinuationAhead(string, startIndex, previousVisibleValue, graphemeSegments) {
if (!previousVisibleValue) {
return false;
}
let index = startIndex;
let hasAnsiCode = false;
while (index < string.length) {
const codePoint = string.codePointAt(index);
if (ESCAPES.has(codePoint)) {
const code = parseAnsiCode(string, index);
if (code) {
hasAnsiCode = true;
index = code.endIndex;
continue;
}
}
if (!hasAnsiCode) {
return false;
}
const characterToken = parseCharacterTokenWithRawSegmentation(string, index, graphemeSegments);
if (!characterToken) {
return true;
}
return areValuesInSameGrapheme(previousVisibleValue, characterToken.token.value);
}
return false;
}
export default function tokenizeAnsi(string, {endCharacter = Number.POSITIVE_INFINITY} = {}) {
const tokens = [];
const graphemeSegments = GRAPHEME_SEGMENTER.segment(string);
let index = 0;
let visibleCount = 0;
let previousVisibleValue;
let hasAnsiSinceLastVisible = false;
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;
hasAnsiSinceLastVisible = true;
continue;
}
}
const characterToken = parseCharacterTokenWithRawSegmentation(string, index, graphemeSegments);
if (!characterToken) {
return tokenizeAnsiWithVisibleSegmentation(string, {endCharacter});
}
if (
hasAnsiSinceLastVisible
&& previousVisibleValue
&& areValuesInSameGrapheme(previousVisibleValue, characterToken.token.value)
) {
return tokenizeAnsiWithVisibleSegmentation(string, {endCharacter});
}
tokens.push(characterToken.token);
index = characterToken.endIndex;
visibleCount += characterToken.token.visibleWidth;
hasAnsiSinceLastVisible = false;
previousVisibleValue = characterToken.token.value;
if (visibleCount >= endCharacter) {
if (hasAnsiSplitContinuationAhead(string, index, previousVisibleValue, graphemeSegments)) {
return tokenizeAnsiWithVisibleSegmentation(string, {endCharacter});
}
index = appendTrailingAnsiTokens(string, index, tokens);
break;
}
}
return tokens;
}