-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpty-protocol.ts
More file actions
33 lines (30 loc) · 1.06 KB
/
pty-protocol.ts
File metadata and controls
33 lines (30 loc) · 1.06 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
// DSR (Device Status Report) - cursor position query: ESC[6n or ESC[?6n
const DSR_PATTERN = /\x1b\[\??6n/g;
/** Result of splitting PTY output around device-status-report cursor queries. */
export interface DsrSplit {
segments: Array<{ text: string; dsrAfter: boolean }>;
hasDsr: boolean;
}
export function splitAroundDsr(input: string): DsrSplit {
const segments: Array<{ text: string; dsrAfter: boolean }> = [];
let lastIndex = 0;
let hasDsr = false;
const regex = new RegExp(DSR_PATTERN.source, "g");
let match: RegExpExecArray | null;
while ((match = regex.exec(input)) !== null) {
hasDsr = true;
if (match.index > lastIndex) {
segments.push({ text: input.slice(lastIndex, match.index), dsrAfter: true });
} else {
segments.push({ text: "", dsrAfter: true });
}
lastIndex = match.index + match[0].length;
}
if (lastIndex < input.length) {
segments.push({ text: input.slice(lastIndex), dsrAfter: false });
}
return { segments, hasDsr };
}
export function buildCursorPositionResponse(row = 1, col = 1): string {
return `\x1b[${row};${col}R`;
}