UNPKG

1.65 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
7export const $ = {
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
24export const reset = init(0, 0);
25export const bold = init(1, 22);
26export const dim = init(2, 22);
27export const italic = init(3, 23);
28export const underline = init(4, 24);
29export const inverse = init(7, 27);
30export const hidden = init(8, 28);
31export const strikethrough = init(9, 29);
32
33// colors
34export const black = init(30, 39);
35export const red = init(31, 39);
36export const green = init(32, 39);
37export const yellow = init(33, 39);
38export const blue = init(34, 39);
39export const magenta = init(35, 39);
40export const cyan = init(36, 39);
41export const white = init(37, 39);
42export const gray = init(90, 39);
43export const grey = init(90, 39);
44
45// background colors
46export const bgBlack = init(40, 49);
47export const bgRed = init(41, 49);
48export const bgGreen = init(42, 49);
49export const bgYellow = init(43, 49);
50export const bgBlue = init(44, 49);
51export const bgMagenta = init(45, 49);
52export const bgCyan = init(46, 49);
53export const bgWhite = init(47, 49);