UNPKG

5.91 kBJavaScriptView Raw
1'use strict';
2
3const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
4
5/* eslint-disable no-control-regex */
6// this is a modified version of https://github.com/chalk/ansi-regex (MIT License)
7const ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
8
9const hasColor = () => {
10 if (typeof process !== 'undefined') {
11 return process.env.FORCE_COLOR !== '0';
12 }
13 return false;
14};
15
16const create = () => {
17 const colors = {
18 enabled: hasColor(),
19 visible: true,
20 styles: {},
21 keys: {}
22 };
23
24 const ansi = style => {
25 let open = style.open = `\u001b[${style.codes[0]}m`;
26 let close = style.close = `\u001b[${style.codes[1]}m`;
27 let regex = style.regex = new RegExp(`\\u001b\\[${style.codes[1]}m`, 'g');
28 style.wrap = (input, newline) => {
29 if (input.includes(close)) input = input.replace(regex, close + open);
30 let output = open + input + close;
31 // see https://github.com/chalk/chalk/pull/92, thanks to the
32 // chalk contributors for this fix. However, we've confirmed that
33 // this issue is also present in Windows terminals
34 return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output;
35 };
36 return style;
37 };
38
39 const wrap = (style, input, newline) => {
40 return typeof style === 'function' ? style(input) : style.wrap(input, newline);
41 };
42
43 const style = (input, stack) => {
44 if (input === '' || input == null) return '';
45 if (colors.enabled === false) return input;
46 if (colors.visible === false) return '';
47 let str = '' + input;
48 let nl = str.includes('\n');
49 let n = stack.length;
50 if (n > 0 && stack.includes('unstyle')) {
51 stack = [...new Set(['unstyle', ...stack])].reverse();
52 }
53 while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl);
54 return str;
55 };
56
57 const define = (name, codes, type) => {
58 colors.styles[name] = ansi({ name, codes });
59 let keys = colors.keys[type] || (colors.keys[type] = []);
60 keys.push(name);
61
62 Reflect.defineProperty(colors, name, {
63 configurable: true,
64 enumerable: true,
65 set(value) {
66 colors.alias(name, value);
67 },
68 get() {
69 let color = input => style(input, color.stack);
70 Reflect.setPrototypeOf(color, colors);
71 color.stack = this.stack ? this.stack.concat(name) : [name];
72 return color;
73 }
74 });
75 };
76
77 define('reset', [0, 0], 'modifier');
78 define('bold', [1, 22], 'modifier');
79 define('dim', [2, 22], 'modifier');
80 define('italic', [3, 23], 'modifier');
81 define('underline', [4, 24], 'modifier');
82 define('inverse', [7, 27], 'modifier');
83 define('hidden', [8, 28], 'modifier');
84 define('strikethrough', [9, 29], 'modifier');
85
86 define('black', [30, 39], 'color');
87 define('red', [31, 39], 'color');
88 define('green', [32, 39], 'color');
89 define('yellow', [33, 39], 'color');
90 define('blue', [34, 39], 'color');
91 define('magenta', [35, 39], 'color');
92 define('cyan', [36, 39], 'color');
93 define('white', [37, 39], 'color');
94 define('gray', [90, 39], 'color');
95 define('grey', [90, 39], 'color');
96
97 define('bgBlack', [40, 49], 'bg');
98 define('bgRed', [41, 49], 'bg');
99 define('bgGreen', [42, 49], 'bg');
100 define('bgYellow', [43, 49], 'bg');
101 define('bgBlue', [44, 49], 'bg');
102 define('bgMagenta', [45, 49], 'bg');
103 define('bgCyan', [46, 49], 'bg');
104 define('bgWhite', [47, 49], 'bg');
105
106 define('blackBright', [90, 39], 'bright');
107 define('redBright', [91, 39], 'bright');
108 define('greenBright', [92, 39], 'bright');
109 define('yellowBright', [93, 39], 'bright');
110 define('blueBright', [94, 39], 'bright');
111 define('magentaBright', [95, 39], 'bright');
112 define('cyanBright', [96, 39], 'bright');
113 define('whiteBright', [97, 39], 'bright');
114
115 define('bgBlackBright', [100, 49], 'bgBright');
116 define('bgRedBright', [101, 49], 'bgBright');
117 define('bgGreenBright', [102, 49], 'bgBright');
118 define('bgYellowBright', [103, 49], 'bgBright');
119 define('bgBlueBright', [104, 49], 'bgBright');
120 define('bgMagentaBright', [105, 49], 'bgBright');
121 define('bgCyanBright', [106, 49], 'bgBright');
122 define('bgWhiteBright', [107, 49], 'bgBright');
123
124 colors.ansiRegex = ANSI_REGEX;
125 colors.hasColor = colors.hasAnsi = str => {
126 colors.ansiRegex.lastIndex = 0;
127 return typeof str === 'string' && str !== '' && colors.ansiRegex.test(str);
128 };
129
130 colors.alias = (name, color) => {
131 let fn = typeof color === 'string' ? colors[color] : color;
132
133 if (typeof fn !== 'function') {
134 throw new TypeError('Expected alias to be the name of an existing color (string) or a function');
135 }
136
137 if (!fn.stack) {
138 Reflect.defineProperty(fn, 'name', { value: name });
139 colors.styles[name] = fn;
140 fn.stack = [name];
141 }
142
143 Reflect.defineProperty(colors, name, {
144 configurable: true,
145 enumerable: true,
146 set(value) {
147 colors.alias(name, value);
148 },
149 get() {
150 let color = input => style(input, color.stack);
151 Reflect.setPrototypeOf(color, colors);
152 color.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack;
153 return color;
154 }
155 });
156 };
157
158 colors.theme = custom => {
159 if (!isObject(custom)) throw new TypeError('Expected theme to be an object');
160 for (let name of Object.keys(custom)) {
161 colors.alias(name, custom[name]);
162 }
163 return colors;
164 };
165
166 colors.alias('unstyle', str => {
167 if (typeof str === 'string' && str !== '') {
168 colors.ansiRegex.lastIndex = 0;
169 return str.replace(colors.ansiRegex, '');
170 }
171 return '';
172 });
173
174 colors.alias('noop', str => str);
175 colors.none = colors.clear = colors.noop;
176
177 colors.stripColor = colors.unstyle;
178 colors.symbols = require('./symbols');
179 colors.define = define;
180 return colors;
181};
182
183module.exports = create();
184module.exports.create = create;