UNPKG

7.85 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const ESC = '\x1b[';
4const CURSOR_UP = 'A';
5const CURSOR_DOWN = 'B';
6const CURSOR_RIGHT = 'C';
7const CURSOR_LEFT = 'D';
8const NEXT_LINE = 'E';
9const PREV_LINE = 'F';
10const CURSOR_MOVE_TO_X = 'G';
11const CURSOR_MOVE_TO = 'H';
12const CURSOR_REPORT_POS = 'R';
13const SCROLL_UP = 'S';
14const SCROLL_DOWN = 'T';
15const CURSOR_SAVE_POS = 's';
16const CURSOR_RESTORE_POS = 'u';
17const CURSOR_QUERY_POS = '6n';
18const CURSOR_HIDE = '?25l';
19const CURSOR_SHOW = '?25h';
20const ERASE_DOWN = 'J';
21const ERASE_UP = '1J';
22const ERASE_SCREEN = '2J';
23const ERASE_END_LINE = 'K';
24const ERASE_START_LINE = '1K';
25const ERASE_LINE = '2K';
26const FG_BLACK = '30m';
27const FG_RED = '31m';
28const FG_GREEN = '32m';
29const FG_YELLOW = '33m';
30const FG_BLUE = '34m';
31const FG_MAGENTA = '35m';
32const FG_CYAN = '36m';
33const FG_WHITE = '37m';
34const FG_RGB = '38;2;';
35const FG_CUSTOM = '38;5;';
36const FG_DEFAULT = '39m';
37const BG_BLACK = '40m';
38const BG_RED = '41m';
39const BG_GREEN = '42m';
40const BG_YELLOW = '43m';
41const BG_BLUE = '44m';
42const BG_MAGENTA = '45m';
43const BG_CYAN = '46m';
44const BG_WHITE = '47m';
45const BG_RGB = '48;2;';
46const BG_CUSTOM = '48;5;';
47const BG_DEFAULT = '49m';
48const FONT_BOLD = '1m';
49const FONT_LIGHT = '2m';
50const FONT_ITALIC = '3m';
51const FONT_UNDERLINED = '4m';
52const FONT_BLINK = '5m';
53const FONT_FAST_BLINK = '6m';
54const FONT_INVERSE = '7m';
55const FONT_HIDDEN = '8m';
56const FONT_STRIKETHROUGH = '9m';
57const FONT_BORDER = '51m';
58const FONT_ROUNDED_BORDER = '52m';
59const FONT_OVERLINE = '53m';
60const FONT_RESET = '0m';
61const FONT_END_BOLD = '21m';
62const FONT_END_LIGHT = '22m';
63const FONT_END_ITALIC = '23m';
64const FONT_END_UNDERLINED = '24m';
65const FONT_END_BLINK = '25m';
66const FONT_END_FAST_BLINK = '26m';
67const FONT_END_INVERSE = '27m';
68const FONT_END_HIDDEN = '28m';
69const FONT_END_STRIKETHROUGH = '29m';
70const FONT_END_BORDER = '54m';
71const FONT_END_OVERLINE = '54m';
72class 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}
283exports.CliAnsi = CliAnsi;
284//# sourceMappingURL=cli-ansi.js.map
\No newline at end of file