1 | 'use strict';
|
---|
2 | const ansiEscapes = module.exports;
|
---|
3 | // TODO: remove this in the next major version
|
---|
4 | module.exports.default = ansiEscapes;
|
---|
5 |
|
---|
6 | const ESC = '\u001B[';
|
---|
7 | const OSC = '\u001B]';
|
---|
8 | const BEL = '\u0007';
|
---|
9 | const SEP = ';';
|
---|
10 | const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
|
---|
11 |
|
---|
12 | ansiEscapes.cursorTo = (x, y) => {
|
---|
13 | if (typeof x !== 'number') {
|
---|
14 | throw new TypeError('The `x` argument is required');
|
---|
15 | }
|
---|
16 |
|
---|
17 | if (typeof y !== 'number') {
|
---|
18 | return ESC + (x + 1) + 'G';
|
---|
19 | }
|
---|
20 |
|
---|
21 | return ESC + (y + 1) + ';' + (x + 1) + 'H';
|
---|
22 | };
|
---|
23 |
|
---|
24 | ansiEscapes.cursorMove = (x, y) => {
|
---|
25 | if (typeof x !== 'number') {
|
---|
26 | throw new TypeError('The `x` argument is required');
|
---|
27 | }
|
---|
28 |
|
---|
29 | let ret = '';
|
---|
30 |
|
---|
31 | if (x < 0) {
|
---|
32 | ret += ESC + (-x) + 'D';
|
---|
33 | } else if (x > 0) {
|
---|
34 | ret += ESC + x + 'C';
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (y < 0) {
|
---|
38 | ret += ESC + (-y) + 'A';
|
---|
39 | } else if (y > 0) {
|
---|
40 | ret += ESC + y + 'B';
|
---|
41 | }
|
---|
42 |
|
---|
43 | return ret;
|
---|
44 | };
|
---|
45 |
|
---|
46 | ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
|
---|
47 | ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
|
---|
48 | ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
|
---|
49 | ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
|
---|
50 |
|
---|
51 | ansiEscapes.cursorLeft = ESC + 'G';
|
---|
52 | ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
|
---|
53 | ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
|
---|
54 | ansiEscapes.cursorGetPosition = ESC + '6n';
|
---|
55 | ansiEscapes.cursorNextLine = ESC + 'E';
|
---|
56 | ansiEscapes.cursorPrevLine = ESC + 'F';
|
---|
57 | ansiEscapes.cursorHide = ESC + '?25l';
|
---|
58 | ansiEscapes.cursorShow = ESC + '?25h';
|
---|
59 |
|
---|
60 | ansiEscapes.eraseLines = count => {
|
---|
61 | let clear = '';
|
---|
62 |
|
---|
63 | for (let i = 0; i < count; i++) {
|
---|
64 | clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (count) {
|
---|
68 | clear += ansiEscapes.cursorLeft;
|
---|
69 | }
|
---|
70 |
|
---|
71 | return clear;
|
---|
72 | };
|
---|
73 |
|
---|
74 | ansiEscapes.eraseEndLine = ESC + 'K';
|
---|
75 | ansiEscapes.eraseStartLine = ESC + '1K';
|
---|
76 | ansiEscapes.eraseLine = ESC + '2K';
|
---|
77 | ansiEscapes.eraseDown = ESC + 'J';
|
---|
78 | ansiEscapes.eraseUp = ESC + '1J';
|
---|
79 | ansiEscapes.eraseScreen = ESC + '2J';
|
---|
80 | ansiEscapes.scrollUp = ESC + 'S';
|
---|
81 | ansiEscapes.scrollDown = ESC + 'T';
|
---|
82 |
|
---|
83 | ansiEscapes.clearScreen = '\u001Bc';
|
---|
84 |
|
---|
85 | ansiEscapes.clearTerminal = process.platform === 'win32' ?
|
---|
86 | `${ansiEscapes.eraseScreen}${ESC}0f` :
|
---|
87 | // 1. Erases the screen (Only done in case `2` is not supported)
|
---|
88 | // 2. Erases the whole screen including scrollback buffer
|
---|
89 | // 3. Moves cursor to the top-left position
|
---|
90 | // More info: https://www.real-world-systems.com/docs/ANSIcode.html
|
---|
91 | `${ansiEscapes.eraseScreen}${ESC}3J${ESC}H`;
|
---|
92 |
|
---|
93 | ansiEscapes.beep = BEL;
|
---|
94 |
|
---|
95 | ansiEscapes.link = (text, url) => {
|
---|
96 | return [
|
---|
97 | OSC,
|
---|
98 | '8',
|
---|
99 | SEP,
|
---|
100 | SEP,
|
---|
101 | url,
|
---|
102 | BEL,
|
---|
103 | text,
|
---|
104 | OSC,
|
---|
105 | '8',
|
---|
106 | SEP,
|
---|
107 | SEP,
|
---|
108 | BEL
|
---|
109 | ].join('');
|
---|
110 | };
|
---|
111 |
|
---|
112 | ansiEscapes.image = (buffer, options = {}) => {
|
---|
113 | let ret = `${OSC}1337;File=inline=1`;
|
---|
114 |
|
---|
115 | if (options.width) {
|
---|
116 | ret += `;width=${options.width}`;
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (options.height) {
|
---|
120 | ret += `;height=${options.height}`;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (options.preserveAspectRatio === false) {
|
---|
124 | ret += ';preserveAspectRatio=0';
|
---|
125 | }
|
---|
126 |
|
---|
127 | return ret + ':' + buffer.toString('base64') + BEL;
|
---|
128 | };
|
---|
129 |
|
---|
130 | ansiEscapes.iTerm = {
|
---|
131 | setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
|
---|
132 |
|
---|
133 | annotation: (message, options = {}) => {
|
---|
134 | let ret = `${OSC}1337;`;
|
---|
135 |
|
---|
136 | const hasX = typeof options.x !== 'undefined';
|
---|
137 | const hasY = typeof options.y !== 'undefined';
|
---|
138 | if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
|
---|
139 | throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
|
---|
140 | }
|
---|
141 |
|
---|
142 | message = message.replace(/\|/g, '');
|
---|
143 |
|
---|
144 | ret += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
|
---|
145 |
|
---|
146 | if (options.length > 0) {
|
---|
147 | ret +=
|
---|
148 | (hasX ?
|
---|
149 | [message, options.length, options.x, options.y] :
|
---|
150 | [options.length, message]).join('|');
|
---|
151 | } else {
|
---|
152 | ret += message;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return ret + BEL;
|
---|
156 | }
|
---|
157 | };
|
---|