1 | "use strict"
|
2 |
|
3 | let enabled =
|
4 | !("NO_COLOR" in process.env) &&
|
5 | process.env.FORCE_COLOR !== "0" &&
|
6 | (process.platform === "win32" ||
|
7 | (process.stdout != null &&
|
8 | process.stdout.isTTY &&
|
9 | process.env.TERM &&
|
10 | process.env.TERM !== "dumb"))
|
11 |
|
12 | const rawInit = (open, close, searchRegex, replaceValue) => s =>
|
13 | enabled
|
14 | ? open +
|
15 | (~(s += "").indexOf(close, 4)
|
16 | ? s.replace(searchRegex, replaceValue)
|
17 | : s) +
|
18 | close
|
19 | : s
|
20 |
|
21 | const init = (open, close) => {
|
22 | return rawInit(
|
23 | `\x1b[${open}m`,
|
24 | `\x1b[${close}m`,
|
25 | new RegExp(`\\x1b\\[${close}m`, "g"),
|
26 | `\x1b[${open}m`
|
27 | )
|
28 | }
|
29 |
|
30 | module.exports = {
|
31 | options: Object.defineProperty({}, "enabled", {
|
32 | get: () => enabled,
|
33 | set: value => (enabled = value)
|
34 | }),
|
35 | reset: init(0, 0),
|
36 | bold: rawInit("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m"),
|
37 | dim: rawInit("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m"),
|
38 | italic: init(3, 23),
|
39 | underline: init(4, 24),
|
40 | inverse: init(7, 27),
|
41 | hidden: init(8, 28),
|
42 | strikethrough: init(9, 29),
|
43 | black: init(30, 39),
|
44 | red: init(31, 39),
|
45 | green: init(32, 39),
|
46 | yellow: init(33, 39),
|
47 | blue: init(34, 39),
|
48 | magenta: init(35, 39),
|
49 | cyan: init(36, 39),
|
50 | white: init(37, 39),
|
51 | gray: init(90, 39),
|
52 | bgBlack: init(40, 49),
|
53 | bgRed: init(41, 49),
|
54 | bgGreen: init(42, 49),
|
55 | bgYellow: init(43, 49),
|
56 | bgBlue: init(44, 49),
|
57 | bgMagenta: init(45, 49),
|
58 | bgCyan: init(46, 49),
|
59 | bgWhite: init(47, 49),
|
60 | blackBright: init(90, 39),
|
61 | redBright: init(91, 39),
|
62 | greenBright: init(92, 39),
|
63 | yellowBright: init(93, 39),
|
64 | blueBright: init(94, 39),
|
65 | magentaBright: init(95, 39),
|
66 | cyanBright: init(96, 39),
|
67 | whiteBright: init(97, 39),
|
68 | bgBlackBright: init(100, 49),
|
69 | bgRedBright: init(101, 49),
|
70 | bgGreenBright: init(102, 49),
|
71 | bgYellowBright: init(103, 49),
|
72 | bgBlueBright: init(104, 49),
|
73 | bgMagentaBright: init(105, 49),
|
74 | bgCyanBright: init(106, 49),
|
75 | bgWhiteBright: init(107, 49)
|
76 | }
|