1 | import * as tty from "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 | export const options = Object.defineProperty({}, "enabled", {
|
---|
38 | get: () => enabled,
|
---|
39 | set: (value) => (enabled = value),
|
---|
40 | })
|
---|
41 |
|
---|
42 | export const reset = init(0, 0)
|
---|
43 | export const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m")
|
---|
44 | export const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m")
|
---|
45 | export const italic = init(3, 23)
|
---|
46 | export const underline = init(4, 24)
|
---|
47 | export const inverse = init(7, 27)
|
---|
48 | export const hidden = init(8, 28)
|
---|
49 | export const strikethrough = init(9, 29)
|
---|
50 | export const black = init(30, 39)
|
---|
51 | export const red = init(31, 39)
|
---|
52 | export const green = init(32, 39)
|
---|
53 | export const yellow = init(33, 39)
|
---|
54 | export const blue = init(34, 39)
|
---|
55 | export const magenta = init(35, 39)
|
---|
56 | export const cyan = init(36, 39)
|
---|
57 | export const white = init(37, 39)
|
---|
58 | export const gray = init(90, 39)
|
---|
59 | export const bgBlack = init(40, 49)
|
---|
60 | export const bgRed = init(41, 49)
|
---|
61 | export const bgGreen = init(42, 49)
|
---|
62 | export const bgYellow = init(43, 49)
|
---|
63 | export const bgBlue = init(44, 49)
|
---|
64 | export const bgMagenta = init(45, 49)
|
---|
65 | export const bgCyan = init(46, 49)
|
---|
66 | export const bgWhite = init(47, 49)
|
---|
67 | export const blackBright = init(90, 39)
|
---|
68 | export const redBright = init(91, 39)
|
---|
69 | export const greenBright = init(92, 39)
|
---|
70 | export const yellowBright = init(93, 39)
|
---|
71 | export const blueBright = init(94, 39)
|
---|
72 | export const magentaBright = init(95, 39)
|
---|
73 | export const cyanBright = init(96, 39)
|
---|
74 | export const whiteBright = init(97, 39)
|
---|
75 | export const bgBlackBright = init(100, 49)
|
---|
76 | export const bgRedBright = init(101, 49)
|
---|
77 | export const bgGreenBright = init(102, 49)
|
---|
78 | export const bgYellowBright = init(103, 49)
|
---|
79 | export const bgBlueBright = init(104, 49)
|
---|
80 | export const bgMagentaBright = init(105, 49)
|
---|
81 | export const bgCyanBright = init(106, 49)
|
---|
82 | export const bgWhiteBright = init(107, 49)
|
---|