-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathansicolor.js
581 lines (472 loc) · 14.8 KB
/
ansicolor.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
'use strict';
/* ------------------------------------------------------------------------ */
const O = Object;
/* See https://misc.flogisoft.com/bash/tip_colors_and_formatting
------------------------------------------------------------------------ */
const colorCodes = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'lightGray', '', 'default'],
colorCodesLight = [
'darkGray',
'lightRed',
'lightGreen',
'lightYellow',
'lightBlue',
'lightMagenta',
'lightCyan',
'white',
''
],
styleCodes = ['', 'bright', 'dim', 'italic', 'underline', '', '', 'inverse'],
asBright = {
red: 'lightRed',
green: 'lightGreen',
yellow: 'lightYellow',
blue: 'lightBlue',
magenta: 'lightMagenta',
cyan: 'lightCyan',
black: 'darkGray',
lightGray: 'white'
},
types = { 0: 'style', 2: 'unstyle', 3: 'color', 9: 'colorLight', 4: 'bgColor', 10: 'bgColorLight' },
subtypes = {
color: colorCodes,
colorLight: colorCodesLight,
bgColor: colorCodes,
bgColorLight: colorCodesLight,
style: styleCodes,
unstyle: styleCodes
};
/* ------------------------------------------------------------------------ */
class Color {
constructor(background, name, brightness) {
this.background = background;
this.name = name;
this.brightness = brightness;
}
get inverse() {
return new Color(!this.background, this.name || (this.background ? 'black' : 'white'), this.brightness);
}
get clean() {
const name = this.name === 'default' ? '' : this.name;
const bright = this.brightness === Code.bright;
const dim = this.brightness === Code.dim;
if (!name && !bright && !dim) {
return undefined;
}
return {
name,
bright,
dim
};
}
defaultBrightness(value) {
return new Color(this.background, this.name, this.brightness || value);
}
css(inverted) {
const color = inverted ? this.inverse : this;
const rgbName = (color.brightness === Code.bright && asBright[color.name]) || color.name;
const prop = color.background ? 'background:' : 'color:',
rgb = Colors.rgb[rgbName],
alpha = this.brightness === Code.dim ? 0.5 : 1;
return rgb
? prop + 'rgba(' + [...rgb, alpha].join(',') + ');'
: !color.background && alpha < 1
? 'color:rgba(0,0,0,0.5);'
: ''; // Chrome does not support 'opacity' property...
}
}
/* ------------------------------------------------------------------------ */
class Code {
constructor(n) {
let value = undefined;
let type = undefined;
let subtype = undefined;
let str = '';
let isBrightness = false;
if (n !== undefined) {
value = Number(n);
type = types[Math.floor(value / 10)];
if (type === undefined || subtypes[type] === undefined) {
return;
}
subtype = subtypes[type][value % 10];
str = '\u001b[' + value + 'm';
isBrightness = value === Code.noBrightness || value === Code.bright || value === Code.dim;
}
this.value = value;
this.type = type;
this.subtype = subtype;
this.str = str;
this.isBrightness = isBrightness;
}
static str(x) {
if (x === undefined) return '';
return '\u001b[' + Number(x) + 'm';
}
clone() {
const newCode = new Code(undefined);
newCode.value = this.value;
newCode.type = this.type;
newCode.subtype = this.subtype;
newCode.str = this.str;
newCode.isBrightness = this.isBrightness;
return newCode;
}
}
/* ------------------------------------------------------------------------ */
O.assign(Code, {
reset: 0,
bright: 1,
dim: 2,
inverse: 7,
noBrightness: 22,
noItalic: 23,
noUnderline: 24,
noInverse: 27,
noColor: 39,
noBgColor: 49
});
/* ------------------------------------------------------------------------ */
const replaceAll = (str, a, b) => str.split(a).join(b);
/* ANSI brightness codes do not overlap, e.g. "{bright}{dim}foo" will be rendered bright (not dim).
So we fix it by adding brightness canceling before each brightness code, so the former example gets
converted to "{noBrightness}{bright}{noBrightness}{dim}foo" – this way it gets rendered as expected.
*/
const denormalizeBrightness = (s) => s.replace(/(\u001b\[(1|2)m)/g, '\u001b[22m$1');
const normalizeBrightness = (s) => s.replace(/\u001b\[22m(\u001b\[(1|2)m)/g, '$1');
const wrap = (x, openCode, closeCode) => {
const open = Code.str(openCode),
close = Code.str(closeCode);
return String(x)
.split('\n')
.map((line) => denormalizeBrightness(open + replaceAll(normalizeBrightness(line), close, open) + close))
.join('\n');
};
/* ------------------------------------------------------------------------ */
const camel = (a, b) => a + b.charAt(0).toUpperCase() + b.slice(1);
const stringWrappingMethods = (() =>
[
...colorCodes.map((k, i) =>
!k
? []
: [
// color methods
[k, 30 + i, Code.noColor],
[camel('bg', k), 40 + i, Code.noBgColor]
]
),
...colorCodesLight.map((k, i) =>
!k
? []
: [
// light color methods
[k, 90 + i, Code.noColor],
[camel('bg', k), 100 + i, Code.noBgColor]
]
),
/* THIS ONE IS FOR BACKWARDS COMPATIBILITY WITH PREVIOUS VERSIONS (had 'bright' instead of 'light' for backgrounds)
*/
...['', 'BrightRed', 'BrightGreen', 'BrightYellow', 'BrightBlue', 'BrightMagenta', 'BrightCyan'].map((k, i) =>
!k ? [] : [['bg' + k, 100 + i, Code.noBgColor]]
),
...styleCodes.map((k, i) =>
!k
? []
: [
// style methods
[k, i, k === 'bright' || k === 'dim' ? Code.noBrightness : 20 + i]
]
)
].reduce((a, b) => a.concat(b)))();
/* ------------------------------------------------------------------------ */
const assignStringWrappingAPI = (target, wrapBefore = target) =>
stringWrappingMethods.reduce(
(memo, [k, open, close]) =>
O.defineProperty(memo, k, {
get: () => assignStringWrappingAPI((str) => wrapBefore(wrap(str, open, close)))
}),
target
);
/* ------------------------------------------------------------------------ */
const TEXT = 0,
BRACKET = 1,
CODE = 2;
class Span {
constructor(code, text) {
this.code = code;
this.text = text;
// Those are added in the actual parse, this is done for performance reasons to have the same hidden class
this.css = '';
this.color = undefined;
this.bgColor = undefined;
this.bold = undefined;
this.inverse = undefined;
this.italic = undefined;
this.underline = undefined;
this.bright = undefined;
this.dim = undefined;
}
}
// getString as function instead of string to allow garbage collection
function* rawParse(getString) {
const stateObject = {
state: TEXT,
buffer: '',
text: '',
code: '',
codes: []
};
const ONE_MB = 1048576;
// Instead of holding the reference to the string we split into chunks of 1MB
// and after processing is finished we can remove the reference so it can be GCed
const chunks = splitStringToChunksOfSize(getString(), ONE_MB);
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
// Free memory for the previous chunk
chunks[i] = undefined;
yield* processChunk(chunk, stateObject);
}
if (stateObject.state !== TEXT) stateObject.text += stateObject.buffer;
if (stateObject.text) {
yield new Span(new Code(), stateObject.text);
}
}
function splitStringToChunksOfSize(str, chunkSize) {
const chunks = [];
const chunksLength = Math.ceil(str.length / chunkSize);
for (let i = 0, o = 0; i < chunksLength; ++i, o += chunkSize) {
chunks.push(str.substring(o, o + chunkSize));
}
return chunks;
}
function* processChunk(chunk, stateObject) {
const chars = chunk;
const charsLength = chunk.length;
for (let i = 0; i < charsLength; i++) {
const c = chars[i];
stateObject.buffer += c;
switch (stateObject.state) {
case TEXT:
if (c === '\u001b') {
stateObject.state = BRACKET;
stateObject.buffer = c;
} else {
stateObject.text += c;
}
break;
case BRACKET:
if (c === '[') {
stateObject.state = CODE;
stateObject.code = '';
stateObject.codes = [];
} else {
stateObject.state = TEXT;
stateObject.text += stateObject.buffer;
}
break;
case CODE:
if (c >= '0' && c <= '9') {
stateObject.code += c;
} else if (c === ';') {
stateObject.codes.push(new Code(stateObject.code));
stateObject.code = '';
} else if (c === 'm') {
stateObject.code = stateObject.code || '0';
for (const code of stateObject.codes) {
yield new Span(code, stateObject.text);
stateObject.text = '';
}
yield new Span(new Code(stateObject.code), stateObject.text);
stateObject.text = '';
stateObject.state = TEXT;
} else {
stateObject.state = TEXT;
stateObject.text += stateObject.buffer;
}
}
}
}
/**
* Parse ansi text
* @param {Generator<Span, void, *>} rawSpansIterator raw spans iterator
* @return {Generator<Span, void, *>}
*/
function* parseAnsi(rawSpansIterator) {
let color = new Color();
let bgColor = new Color(true /* background */);
let brightness = undefined;
let styles = new Set();
function reset() {
color = new Color();
bgColor = new Color(true /* background */);
brightness = undefined;
styles.clear();
}
reset();
for (const span of rawSpansIterator) {
const c = span.code;
if (span.text !== '') {
const inverted = styles.has('inverse');
const underline = styles.has('underline') ? 'text-decoration: underline;' : '';
const italic = styles.has('italic') ? 'font-style: italic;' : '';
const bold = brightness === Code.bright ? 'font-weight: bold;' : '';
const foreColor = color.defaultBrightness(brightness);
const newSpan = new Span(span.code ? span.code.clone() : undefined, span.text);
newSpan.css = span.css ? span.css : bold + italic + underline + foreColor.css(inverted) + bgColor.css(inverted);
newSpan.bold = span.bold ? span.bold : !!bold;
newSpan.color = span.color ? span.color : foreColor.clean;
newSpan.bgColor = span.bgColor ? span.bgColor : bgColor.clean;
newSpan.inverse = inverted;
newSpan.italic = !!italic;
newSpan.underline = !!underline;
newSpan.bright = styles.has('bright');
newSpan.dim = styles.has('dim');
yield newSpan;
}
if (c.isBrightness) {
brightness = c.value;
continue;
}
if (span.code.value === undefined) {
continue;
}
if (span.code.value === Code.reset) {
reset();
continue;
}
switch (span.code.type) {
case 'color':
case 'colorLight':
color = new Color(false, c.subtype);
break;
case 'bgColor':
case 'bgColorLight':
bgColor = new Color(true, c.subtype);
break;
case 'style':
styles.add(c.subtype);
break;
case 'unstyle':
styles.delete(c.subtype);
break;
}
}
}
/* ------------------------------------------------------------------------ */
/**
* Represents an ANSI-escaped string.
*/
class Colors {
/**
* @param {string} s a string containing ANSI escape codes.
*/
constructor(s) {
this.spans = s ? Array.from(rawParse(typeof s === 'string' ? () => s : s)) : [];
}
get str() {
return this.spans.reduce((str, p) => str + p.text + p.code.str, '');
}
get parsed() {
const newColors = new Colors();
newColors.spans = Array.from(parseAnsi(this.spans));
return newColors;
}
/* Outputs with Chrome DevTools-compatible format */
get asChromeConsoleLogArguments() {
const spans = this.parsed.spans;
return [spans.map((s) => '%c' + s.text).join(''), ...spans.map((s) => s.css)];
}
get browserConsoleArguments() /* LEGACY, DEPRECATED */ {
return this.asChromeConsoleLogArguments;
}
/**
* @desc installs String prototype extensions
* @example
* require ('ansicolor').nice
* console.log ('foo'.bright.red)
*/
static get nice() {
Colors.names.forEach((k) => {
if (!(k in String.prototype)) {
O.defineProperty(String.prototype, k, {
get: function () {
return Colors[k](this);
}
});
}
});
return Colors;
}
/**
* @desc parses a string containing ANSI escape codes
* @return {Colors} parsed representation.
*/
static parse(s) {
return new Colors(s).parsed;
}
/**
*
* @param {string | () => string} s string or a function returning a string (for large strings you may want to use a function to avoid memory issues)
* @returns {Generator<Span, void, *>} Spans iterator
*/
static parseIterator(s) {
return parseAnsi(rawParse(typeof s === 'string' ? () => s : s));
}
/**
* @desc strips ANSI codes from a string
* @param {string} s a string containing ANSI escape codes.
* @return {string} clean string.
*/
static strip(s) {
return s.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]/g, ''); // hope V8 caches the regexp
}
/**
* @desc checks if a value contains ANSI escape codes
* @param {any} s value to check
* @return {boolean} has codes
*/
static isEscaped(s) {
s = String(s);
return Colors.strip(s) !== s;
}
/**
* @example
* const spans = [...ansi.parse ('\u001b[7m\u001b[7mfoo\u001b[7mbar\u001b[27m')]
*/
[Symbol.iterator]() {
return this.spans[Symbol.iterator]();
}
/**
* @desc This allows an alternative import style, see https://github.com/xpl/ansicolor/issues/7#issuecomment-578923578
* @example
* import { ansicolor, ParsedSpan } from 'ansicolor'
*/
static get ansicolor() {
return Colors;
}
}
/* ------------------------------------------------------------------------ */
assignStringWrappingAPI(Colors, (str) => str);
/* ------------------------------------------------------------------------ */
Colors.names = stringWrappingMethods.map(([k]) => k);
/* ------------------------------------------------------------------------ */
Colors.rgb = {
black: [0, 0, 0],
darkGray: [100, 100, 100],
lightGray: [200, 200, 200],
white: [255, 255, 255],
red: [204, 0, 0],
lightRed: [255, 51, 0],
green: [0, 204, 0],
lightGreen: [51, 204, 51],
yellow: [204, 102, 0],
lightYellow: [255, 153, 51],
blue: [0, 0, 255],
lightBlue: [26, 140, 255],
magenta: [204, 0, 204],
lightMagenta: [255, 0, 255],
cyan: [0, 153, 255],
lightCyan: [0, 204, 255]
};
/* ------------------------------------------------------------------------ */
module.exports = Colors;
/* ------------------------------------------------------------------------ */