UNPKG

2.41 kBJavaScriptView Raw
1const csi = some => '\x1b[' + some;
2const csiSet = (...vec) => {
3 let tx = '';
4
5 for (let x of vec) tx += csi(x);
6
7 return tx;
8};
9
10class Clear {
11 static AFTER_CURSOR = csi('0J'); // 'Clear everything after the cursor.'
12
13 static BEFORE_CURSOR = csi('1J'); // 'Clear everything before the cursor.'
14
15 static ENTIRE_SCREEN = csi('2J'); // 'Clear the entire screen. Move cursor to top-left.'
16
17 static ENTIRE_TERMINAL = csi('3J'); // 'Clear the entire screen + entire scroll-back buffer.'
18
19 static RIGHT_TO_CURSOR = csi('0K'); // 'Clear from cursor to the end of the line. Cursor unchanged.'
20
21 static LEFT_TO_CURSOR = csi('1K'); // 'Clear from cursor to the beginning of the line. Cursor unchanged.'
22
23 static ENTIRE_LINE = csi('2K'); // 'Clear the current line. Cursor unchanged.'
24
25}
26
27class Cursor {
28 static goto = (x, y) => csi(`${++x};${++y}H`);
29 static up = d => csi(d + 'A');
30 static down = d => csi(d + 'B');
31 static right = d => csi(d + 'C');
32 static left = d => csi(d + 'D');
33 static prevLine = d => csi(d + 'E');
34 static nextLine = d => csi(d + 'F');
35 static charTo = d => csi(d + 'G');
36 static QUERY_POS = csi("6n"); // Where is the cursor? Use `ESC [ 6 n`. And answers: `ESC [ Cy ; Cx R`.
37
38 static SHOW = csi("?25h"); // "Show the cursor." //
39
40 static HIDE = csi("?25l"); // "Hide the cursor." //
41
42 static SAVE = csi("s"); // "Save the cursor." //
43
44 static RESTORE = csi("u"); // "Restore the cursor." //
45
46 static BLINK_BLOCK = csi("\x31 q"); // "Change the cursor style to blinking block" //
47
48 static STEADY_BLOCK = csi("\x32 q"); // "Change the cursor style to steady block" //
49
50 static BLINK_UNDERLINE = csi("\x33 q"); // "Change the cursor style to blinking underline" //
51
52 static STEADY_UNDERLINE = csi("\x34 q"); // "Change the cursor style to steady underline" //
53
54 static BLINK_BAR = csi("\x35 q"); // "Change the cursor style to blinking bar" //
55
56 static STEADY_BAR = csi("\x36 q"); // "Change the cursor style to steady bar" //
57
58}
59
60class Scroll {
61 static up = n => csi(`${n}S`);
62 static down = n => csi(`${n}T`);
63}
64
65class DECSet {
66 static WRAP_ON = csi('?7h');
67 static WRAP_OFF = csi('?7l');
68}
69
70export { Clear, Cursor, DECSet, Scroll, Clear as clear, csi, csiSet, Cursor as cursor, DECSet as decset, Scroll as scroll };