-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Expand file tree
/
Copy pathterminal-control.ts
More file actions
60 lines (47 loc) · 1.7 KB
/
Copy pathterminal-control.ts
File metadata and controls
60 lines (47 loc) · 1.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
const ESC = '\u001b';
const BEL = '\u0007';
const COMPLETE_TERMINAL_CONTROL_RE =
/(?:\u001b\][\s\S]*?(?:\u0007|\u001b\\)|\u001b\[[0-?]*[ -/]*[@-~]|\u001b[@-Z\\-_]|\u009b[0-?]*[ -/]*[@-~])/g;
function hasOscTerminator(value: string): boolean {
return value.includes(BEL) || value.includes(`${ESC}\\`);
}
function hasCsiFinalByte(value: string): boolean {
return /[\x40-\x7e]/.test(value.slice(2));
}
function splitIncompleteControlTail(value: string): [string, string] {
const escIndex = value.lastIndexOf(ESC);
const csiIndex = value.lastIndexOf('\u009b');
const index = Math.max(escIndex, csiIndex);
if (index < 0) return [value, ''];
const tail = value.slice(index);
if (tail === ESC || tail === '\u009b') return [value.slice(0, index), tail];
if (tail.startsWith(`${ESC}]`)) {
return hasOscTerminator(tail) ? [value, ''] : [value.slice(0, index), tail];
}
if (tail.startsWith(`${ESC}[`)) {
return hasCsiFinalByte(tail) ? [value, ''] : [value.slice(0, index), tail];
}
if (tail.startsWith('\u009b')) {
return /[\x40-\x7e]/.test(tail.slice(1))
? [value, '']
: [value.slice(0, index), tail];
}
return [value, ''];
}
export function stripTerminalControlSequences(value: string): string {
return value.replace(COMPLETE_TERMINAL_CONTROL_RE, '');
}
export class TerminalControlSequenceStripper {
#pending = '';
write(chunk: unknown): string {
const value = `${this.#pending}${typeof chunk === 'string' ? chunk : String(chunk)}`;
this.#pending = '';
const [ready, pending] = splitIncompleteControlTail(value);
this.#pending = pending;
return stripTerminalControlSequences(ready);
}
flush(): string {
this.#pending = '';
return '';
}
}