1 | 'use strict';
|
2 |
|
3 | let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
|
4 | if (typeof process !== 'undefined') {
|
5 | ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env || {});
|
6 | isTTY = process.stdout && process.stdout.isTTY;
|
7 | }
|
8 |
|
9 | const $ = {
|
10 | enabled: !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== 'dumb' && (
|
11 | FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
|
12 | ),
|
13 |
|
14 |
|
15 | reset: init(0, 0),
|
16 | bold: init(1, 22),
|
17 | dim: init(2, 22),
|
18 | italic: init(3, 23),
|
19 | underline: init(4, 24),
|
20 | inverse: init(7, 27),
|
21 | hidden: init(8, 28),
|
22 | strikethrough: init(9, 29),
|
23 |
|
24 |
|
25 | black: init(30, 39),
|
26 | red: init(31, 39),
|
27 | green: init(32, 39),
|
28 | yellow: init(33, 39),
|
29 | blue: init(34, 39),
|
30 | magenta: init(35, 39),
|
31 | cyan: init(36, 39),
|
32 | white: init(37, 39),
|
33 | gray: init(90, 39),
|
34 | grey: init(90, 39),
|
35 |
|
36 |
|
37 | bgBlack: init(40, 49),
|
38 | bgRed: init(41, 49),
|
39 | bgGreen: init(42, 49),
|
40 | bgYellow: init(43, 49),
|
41 | bgBlue: init(44, 49),
|
42 | bgMagenta: init(45, 49),
|
43 | bgCyan: init(46, 49),
|
44 | bgWhite: init(47, 49)
|
45 | };
|
46 |
|
47 | function run(arr, str) {
|
48 | let i=0, tmp, beg='', end='';
|
49 | for (; i < arr.length; i++) {
|
50 | tmp = arr[i];
|
51 | beg += tmp.open;
|
52 | end += tmp.close;
|
53 | if (!!~str.indexOf(tmp.close)) {
|
54 | str = str.replace(tmp.rgx, tmp.close + tmp.open);
|
55 | }
|
56 | }
|
57 | return beg + str + end;
|
58 | }
|
59 |
|
60 | function chain(has, keys) {
|
61 | let ctx = { has, keys };
|
62 |
|
63 | ctx.reset = $.reset.bind(ctx);
|
64 | ctx.bold = $.bold.bind(ctx);
|
65 | ctx.dim = $.dim.bind(ctx);
|
66 | ctx.italic = $.italic.bind(ctx);
|
67 | ctx.underline = $.underline.bind(ctx);
|
68 | ctx.inverse = $.inverse.bind(ctx);
|
69 | ctx.hidden = $.hidden.bind(ctx);
|
70 | ctx.strikethrough = $.strikethrough.bind(ctx);
|
71 |
|
72 | ctx.black = $.black.bind(ctx);
|
73 | ctx.red = $.red.bind(ctx);
|
74 | ctx.green = $.green.bind(ctx);
|
75 | ctx.yellow = $.yellow.bind(ctx);
|
76 | ctx.blue = $.blue.bind(ctx);
|
77 | ctx.magenta = $.magenta.bind(ctx);
|
78 | ctx.cyan = $.cyan.bind(ctx);
|
79 | ctx.white = $.white.bind(ctx);
|
80 | ctx.gray = $.gray.bind(ctx);
|
81 | ctx.grey = $.grey.bind(ctx);
|
82 |
|
83 | ctx.bgBlack = $.bgBlack.bind(ctx);
|
84 | ctx.bgRed = $.bgRed.bind(ctx);
|
85 | ctx.bgGreen = $.bgGreen.bind(ctx);
|
86 | ctx.bgYellow = $.bgYellow.bind(ctx);
|
87 | ctx.bgBlue = $.bgBlue.bind(ctx);
|
88 | ctx.bgMagenta = $.bgMagenta.bind(ctx);
|
89 | ctx.bgCyan = $.bgCyan.bind(ctx);
|
90 | ctx.bgWhite = $.bgWhite.bind(ctx);
|
91 |
|
92 | return ctx;
|
93 | }
|
94 |
|
95 | function init(open, close) {
|
96 | let blk = {
|
97 | open: `\x1b[${open}m`,
|
98 | close: `\x1b[${close}m`,
|
99 | rgx: new RegExp(`\\x1b\\[${close}m`, 'g')
|
100 | };
|
101 | return function (txt) {
|
102 | if (this !== void 0 && this.has !== void 0) {
|
103 | !!~this.has.indexOf(open) || (this.has.push(open),this.keys.push(blk));
|
104 | return txt === void 0 ? this : $.enabled ? run(this.keys, txt+'') : txt+'';
|
105 | }
|
106 | return txt === void 0 ? chain([open], [blk]) : $.enabled ? run([blk], txt+'') : txt+'';
|
107 | };
|
108 | }
|
109 |
|
110 | module.exports = $;
|