UNPKG

1.52 kBJavaScriptView Raw
1let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
2if (typeof process !== 'undefined') {
3 ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env || {});
4 isTTY = process.stdout && process.stdout.isTTY;
5}
6
7const $ = exports.$ = {
8 enabled: !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== 'dumb' && (
9 FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
10 )
11}
12
13function init(x, y) {
14 let rgx = new RegExp(`\\x1b\\[${y}m`, 'g');
15 let open = `\x1b[${x}m`, close = `\x1b[${y}m`;
16
17 return function (txt) {
18 if (!$.enabled || txt == null) return txt;
19 return open + (!!~(''+txt).indexOf(close) ? txt.replace(rgx, close + open) : txt) + close;
20 };
21}
22
23// modifiers
24exports.reset = init(0, 0);
25exports.bold = init(1, 22);
26exports.dim = init(2, 22);
27exports.italic = init(3, 23);
28exports.underline = init(4, 24);
29exports.inverse = init(7, 27);
30exports.hidden = init(8, 28);
31exports.strikethrough = init(9, 29);
32
33// colors
34exports.black = init(30, 39);
35exports.red = init(31, 39);
36exports.green = init(32, 39);
37exports.yellow = init(33, 39);
38exports.blue = init(34, 39);
39exports.magenta = init(35, 39);
40exports.cyan = init(36, 39);
41exports.white = init(37, 39);
42exports.gray = init(90, 39);
43exports.grey = init(90, 39);
44
45// background colors
46exports.bgBlack = init(40, 49);
47exports.bgRed = init(41, 49);
48exports.bgGreen = init(42, 49);
49exports.bgYellow = init(43, 49);
50exports.bgBlue = init(44, 49);
51exports.bgMagenta = init(45, 49);
52exports.bgCyan = init(46, 49);
53exports.bgWhite = init(47, 49);