1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const ESC = '\x1b[';
|
4 | const CURSOR_UP = 'A';
|
5 | const CURSOR_DOWN = 'B';
|
6 | const CURSOR_RIGHT = 'C';
|
7 | const CURSOR_LEFT = 'D';
|
8 | const NEXT_LINE = 'E';
|
9 | const PREV_LINE = 'F';
|
10 | const CURSOR_MOVE_TO_X = 'G';
|
11 | const CURSOR_MOVE_TO = 'H';
|
12 | const CURSOR_REPORT_POS = 'R';
|
13 | const SCROLL_UP = 'S';
|
14 | const SCROLL_DOWN = 'T';
|
15 | const CURSOR_SAVE_POS = 's';
|
16 | const CURSOR_RESTORE_POS = 'u';
|
17 | const CURSOR_QUERY_POS = '6n';
|
18 | const CURSOR_HIDE = '?25l';
|
19 | const CURSOR_SHOW = '?25h';
|
20 | const ERASE_DOWN = 'J';
|
21 | const ERASE_UP = '1J';
|
22 | const ERASE_SCREEN = '2J';
|
23 | const ERASE_END_LINE = 'K';
|
24 | const ERASE_START_LINE = '1K';
|
25 | const ERASE_LINE = '2K';
|
26 | const FG_BLACK = '30m';
|
27 | const FG_RED = '31m';
|
28 | const FG_GREEN = '32m';
|
29 | const FG_YELLOW = '33m';
|
30 | const FG_BLUE = '34m';
|
31 | const FG_MAGENTA = '35m';
|
32 | const FG_CYAN = '36m';
|
33 | const FG_WHITE = '37m';
|
34 | const FG_RGB = '38;2;';
|
35 | const FG_CUSTOM = '38;5;';
|
36 | const FG_DEFAULT = '39m';
|
37 | const BG_BLACK = '40m';
|
38 | const BG_RED = '41m';
|
39 | const BG_GREEN = '42m';
|
40 | const BG_YELLOW = '43m';
|
41 | const BG_BLUE = '44m';
|
42 | const BG_MAGENTA = '45m';
|
43 | const BG_CYAN = '46m';
|
44 | const BG_WHITE = '47m';
|
45 | const BG_RGB = '48;2;';
|
46 | const BG_CUSTOM = '48;5;';
|
47 | const BG_DEFAULT = '49m';
|
48 | const FONT_BOLD = '1m';
|
49 | const FONT_LIGHT = '2m';
|
50 | const FONT_ITALIC = '3m';
|
51 | const FONT_UNDERLINED = '4m';
|
52 | const FONT_BLINK = '5m';
|
53 | const FONT_FAST_BLINK = '6m';
|
54 | const FONT_INVERSE = '7m';
|
55 | const FONT_HIDDEN = '8m';
|
56 | const FONT_STRIKETHROUGH = '9m';
|
57 | const FONT_BORDER = '51m';
|
58 | const FONT_ROUNDED_BORDER = '52m';
|
59 | const FONT_OVERLINE = '53m';
|
60 | const FONT_RESET = '0m';
|
61 | const FONT_END_BOLD = '21m';
|
62 | const FONT_END_LIGHT = '22m';
|
63 | const FONT_END_ITALIC = '23m';
|
64 | const FONT_END_UNDERLINED = '24m';
|
65 | const FONT_END_BLINK = '25m';
|
66 | const FONT_END_FAST_BLINK = '26m';
|
67 | const FONT_END_INVERSE = '27m';
|
68 | const FONT_END_HIDDEN = '28m';
|
69 | const FONT_END_STRIKETHROUGH = '29m';
|
70 | const FONT_END_BORDER = '54m';
|
71 | const FONT_END_OVERLINE = '54m';
|
72 | class CliAnsi {
|
73 | write(...args) {
|
74 | args.forEach((arg) => {
|
75 | process.stdout.write(arg);
|
76 | });
|
77 | }
|
78 | writeLine(...args) {
|
79 | this.write.apply(this, args.concat(["\n"]));
|
80 | }
|
81 | writeLines(...args) {
|
82 | args.forEach((arg) => {
|
83 | this.writeLine(arg);
|
84 | });
|
85 | }
|
86 | cursorTo(x, y) {
|
87 | if (typeof x !== 'number') {
|
88 | throw new TypeError('The `x` argument is required');
|
89 | }
|
90 | if (typeof y !== 'number') {
|
91 | return `${ESC}${x + 1}${CURSOR_MOVE_TO_X}`;
|
92 | }
|
93 | return `${ESC}${y + 1};${x + 1}${CURSOR_MOVE_TO}`;
|
94 | }
|
95 | ;
|
96 | cursorMove(x, y) {
|
97 | return this.cursorMoveX(x) + this.cursorMoveY(y);
|
98 | }
|
99 | cursorMoveX(x) {
|
100 | if (x < 0) {
|
101 | return `${ESC}${x * -1}${CURSOR_LEFT}`;
|
102 | }
|
103 | else if (x > 0) {
|
104 | return `${ESC}${x}${CURSOR_RIGHT}`;
|
105 | }
|
106 | return '';
|
107 | }
|
108 | cursorMoveY(y) {
|
109 | if (y < 0) {
|
110 | return `${ESC}${y * -1}${CURSOR_UP}`;
|
111 | }
|
112 | else if (y > 0) {
|
113 | return `${ESC}${y}${CURSOR_DOWN}`;
|
114 | }
|
115 | return '';
|
116 | }
|
117 | cursorUp(n = 1) {
|
118 | return `${ESC}${n}${CURSOR_UP}`;
|
119 | }
|
120 | cursorDown(n = 1) {
|
121 | return `${ESC}${n}${CURSOR_DOWN}`;
|
122 | }
|
123 | cursorLeft(n = 1) {
|
124 | return `${ESC}${n}${CURSOR_LEFT}`;
|
125 | }
|
126 | cursorRight(n = 1) {
|
127 | return `${ESC}${n}${CURSOR_RIGHT}`;
|
128 | }
|
129 | cursorHome() {
|
130 | return `${ESC}${CURSOR_LEFT}`;
|
131 | }
|
132 | cursorPreviousLine() {
|
133 | return `${ESC}${PREV_LINE}`;
|
134 | }
|
135 | cursorNextLine() {
|
136 | return `${ESC}${NEXT_LINE}`;
|
137 | }
|
138 | cursorHide() {
|
139 | return `${ESC}${CURSOR_HIDE}`;
|
140 | }
|
141 | cursorShow() {
|
142 | return `${ESC}${CURSOR_SHOW}`;
|
143 | }
|
144 | cursorSavePosition() {
|
145 | return `${ESC}${CURSOR_SAVE_POS}`;
|
146 | }
|
147 | cursorRestorePosition() {
|
148 | return `${ESC}${CURSOR_RESTORE_POS}`;
|
149 | }
|
150 | cursorQueryPosition() {
|
151 | return `${ESC}${CURSOR_QUERY_POS}`;
|
152 | }
|
153 | eraseLine() {
|
154 | return `${ESC}${ERASE_LINE}`;
|
155 | }
|
156 | eraseLines(numLines) {
|
157 | let clear = '';
|
158 | for (let i = 0; i < numLines; i++) {
|
159 | clear += this.eraseLine();
|
160 | if (i < numLines - 1) {
|
161 | clear += this.cursorUp();
|
162 | }
|
163 | }
|
164 | return clear;
|
165 | }
|
166 | ;
|
167 | bold(str) {
|
168 | return `${ESC}${FONT_BOLD}${str}${ESC}${FONT_END_BOLD}`;
|
169 | }
|
170 | underlined(str) {
|
171 | return `${ESC}${FONT_UNDERLINED}${str}${ESC}${FONT_END_UNDERLINED}`;
|
172 | }
|
173 | italic(str) {
|
174 | return `${ESC}${FONT_ITALIC}${str}${ESC}${FONT_END_ITALIC}`;
|
175 | }
|
176 | light(str) {
|
177 | return `${ESC}${FONT_LIGHT}${str}${ESC}${FONT_END_LIGHT}`;
|
178 | }
|
179 | blink(str) {
|
180 | return `${ESC}${FONT_BLINK}${str}${ESC}${FONT_END_BLINK}`;
|
181 | }
|
182 | inverse(str) {
|
183 | return `${ESC}${FONT_INVERSE}${str}${ESC}${FONT_END_INVERSE}`;
|
184 | }
|
185 | border(str) {
|
186 | return `${ESC}${FONT_BORDER}${str}${ESC}${FONT_END_BORDER}`;
|
187 | }
|
188 | roundedBorder(str) {
|
189 | return `${ESC}${FONT_ROUNDED_BORDER}${str}${ESC}${FONT_END_BORDER}`;
|
190 | }
|
191 | strikethrough(str) {
|
192 | return `${ESC}${FONT_STRIKETHROUGH}${str}${ESC}${FONT_END_STRIKETHROUGH}`;
|
193 | }
|
194 | hidden(str) {
|
195 | return `${ESC}${FONT_HIDDEN}${str}${ESC}${FONT_END_HIDDEN}`;
|
196 | }
|
197 | center(str, targetLength, padChar = ' ') {
|
198 | const regex = new RegExp(`${ESC}[^m]+m`, 'g');
|
199 | const len = str.replace(regex, '').length;
|
200 | const padLeft = Math.max(0, Math.floor((targetLength - len) / 2));
|
201 | const padRight = Math.max(0, Math.ceil((targetLength - len) / 2));
|
202 | return `${padChar.repeat(padLeft)}${str}${padChar.repeat(padRight)}`;
|
203 | }
|
204 | left(str, targetLength, padChar = ' ') {
|
205 | const regex = new RegExp(`${ESC}[^m]+m`, 'g');
|
206 | const len = str.replace(regex, '').length;
|
207 | const padding = Math.max(targetLength - len, 1);
|
208 | return `${str}${padChar.repeat(padding)}`;
|
209 | }
|
210 | right(str, targetLength, padChar = ' ') {
|
211 | const regex = new RegExp(`${ESC}[^m]+m`, 'g');
|
212 | const len = str.replace(regex, '').length;
|
213 | const padding = targetLength - len;
|
214 | return `${padChar.repeat(padding)}${str}`;
|
215 | }
|
216 | bgBlue(str) {
|
217 | return this._bg(BG_BLUE, str);
|
218 | }
|
219 | bgBlack(str) {
|
220 | return this._bg(BG_BLACK, str);
|
221 | }
|
222 | bgCyan(str) {
|
223 | return this._bg(BG_CYAN, str);
|
224 | }
|
225 | bgGreen(str) {
|
226 | return this._bg(BG_GREEN, str);
|
227 | }
|
228 | bgMagenta(str) {
|
229 | return this._bg(BG_MAGENTA, str);
|
230 | }
|
231 | bgRed(str) {
|
232 | return this._bg(BG_RED, str);
|
233 | }
|
234 | bgWhite(str) {
|
235 | return this._bg(BG_WHITE, str);
|
236 | }
|
237 | bgYellow(str) {
|
238 | return this._bg(BG_YELLOW, str);
|
239 | }
|
240 | bgRgb(str, r, g, b) {
|
241 | return `${ESC}${BG_RGB}${r};${g};${b}m${str}${ESC}${BG_DEFAULT}`;
|
242 | }
|
243 | bgCustom(str, colorNumber) {
|
244 | return `${ESC}${BG_CUSTOM}${colorNumber}m${str}${ESC}${BG_DEFAULT}`;
|
245 | }
|
246 | fgBlue(str) {
|
247 | return this._fg(FG_BLUE, str);
|
248 | }
|
249 | fgBlack(str) {
|
250 | return this._fg(FG_BLACK, str);
|
251 | }
|
252 | fgCyan(str) {
|
253 | return this._fg(FG_CYAN, str);
|
254 | }
|
255 | fgGreen(str) {
|
256 | return this._fg(FG_GREEN, str);
|
257 | }
|
258 | fgMagenta(str) {
|
259 | return this._fg(FG_MAGENTA, str);
|
260 | }
|
261 | fgRed(str) {
|
262 | return this._fg(FG_RED, str);
|
263 | }
|
264 | fgWhite(str) {
|
265 | return this._fg(FG_WHITE, str);
|
266 | }
|
267 | fgYellow(str) {
|
268 | return this._fg(FG_YELLOW, str);
|
269 | }
|
270 | fgRgb(str, r, g, b) {
|
271 | return `${ESC}${FG_RGB}${r};${g};${b}m${str}${ESC}${FG_DEFAULT}`;
|
272 | }
|
273 | fgCustom(str, colorNumber) {
|
274 | return `${ESC}${FG_CUSTOM}${colorNumber}m${str}${ESC}${FG_DEFAULT}`;
|
275 | }
|
276 | _bg(color, str) {
|
277 | return `${ESC}${color}${str}${ESC}${BG_DEFAULT}`;
|
278 | }
|
279 | _fg(color, str) {
|
280 | return `${ESC}${color}${str}${ESC}${FG_DEFAULT}`;
|
281 | }
|
282 | }
|
283 | exports.CliAnsi = CliAnsi;
|
284 |
|
\ | No newline at end of file |