-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.ts
More file actions
81 lines (73 loc) · 1.63 KB
/
settings.ts
File metadata and controls
81 lines (73 loc) · 1.63 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
import {
ALTSCREEN,
CSI,
ESC,
HIDECURSOR,
MAINSCREEN,
SHOWCURSOR,
} from "./termcodes.ts";
export interface Setting {
apply: Uint8Array;
revert: Uint8Array;
}
export function settings(...sequence: Setting[]): Setting {
return {
apply: concat(sequence.map((s) => s.apply)),
revert: concat(sequence.map((s) => s.revert).reverse()),
};
}
export function alternateBuffer(
options?: { clear?: boolean },
): Setting {
return {
apply: ALTSCREEN(options),
revert: MAINSCREEN(),
};
}
export function cursor(visible: boolean): Setting {
if (visible) {
return {
apply: SHOWCURSOR(),
revert: HIDECURSOR(),
};
} else {
return {
apply: HIDECURSOR(),
revert: SHOWCURSOR(),
};
}
}
/**
* Save and restore cursor position using DECSC (`ESC 7`) / DECRC (`ESC 8`).
*
* @see {@link https://vt100.net/docs/vt510-rm/DECSC.html | VT510 DECSC}
* @see {@link https://vt100.net/docs/vt510-rm/DECRC.html | VT510 DECRC}
*/
export function saveCursorPosition(): Setting {
return {
apply: ESC("7"),
revert: ESC("8"),
};
}
export function progressiveInput(level: number): Setting {
return {
apply: CSI(`>${level}u`),
revert: CSI("<u"),
};
}
export function mouseTracking(): Setting {
return {
apply: concat([CSI("?1003h"), CSI("?1006h")]),
revert: concat([CSI("?1006l"), CSI("?1003l")]),
};
}
function concat(arrays: Uint8Array[]): Uint8Array {
let length = arrays.reduce((sum, a) => sum + a.length, 0);
let result = new Uint8Array(length);
let offset = 0;
for (let a of arrays) {
result.set(a, offset);
offset += a.length;
}
return result;
}