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