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 | export const isColorSupported =
|
17 | !isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI)
|
18 |
|
19 | const raw = (open, close, searchRegex, replaceValue) => (s) =>
|
20 | open +
|
21 | (~(s += "").indexOf(close, 4)
|
22 | ? s.replace(searchRegex, replaceValue)
|
23 | : s) +
|
24 | close
|
25 |
|
26 | const init = (open, close) =>
|
27 | raw(
|
28 | `\x1b[${open}m`,
|
29 | `\x1b[${close}m`,
|
30 | new RegExp(`\\x1b\\[${close}m`, "g"),
|
31 | `\x1b[${open}m`
|
32 | )
|
33 |
|
34 | export const reset = init(0, 0)
|
35 | export const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m")
|
36 | export const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m")
|
37 | export const italic = init(3, 23)
|
38 | export const underline = init(4, 24)
|
39 | export const inverse = init(7, 27)
|
40 | export const hidden = init(8, 28)
|
41 | export const strikethrough = init(9, 29)
|
42 | export const black = init(30, 39)
|
43 | export const red = init(31, 39)
|
44 | export const green = init(32, 39)
|
45 | export const yellow = init(33, 39)
|
46 | export const blue = init(34, 39)
|
47 | export const magenta = init(35, 39)
|
48 | export const cyan = init(36, 39)
|
49 | export const white = init(37, 39)
|
50 | export const gray = init(90, 39)
|
51 | export const bgBlack = init(40, 49)
|
52 | export const bgRed = init(41, 49)
|
53 | export const bgGreen = init(42, 49)
|
54 | export const bgYellow = init(43, 49)
|
55 | export const bgBlue = init(44, 49)
|
56 | export const bgMagenta = init(45, 49)
|
57 | export const bgCyan = init(46, 49)
|
58 | export const bgWhite = init(47, 49)
|
59 | export const blackBright = init(90, 39)
|
60 | export const redBright = init(91, 39)
|
61 | export const greenBright = init(92, 39)
|
62 | export const yellowBright = init(93, 39)
|
63 | export const blueBright = init(94, 39)
|
64 | export const magentaBright = init(95, 39)
|
65 | export const cyanBright = init(96, 39)
|
66 | export const whiteBright = init(97, 39)
|
67 | export const bgBlackBright = init(100, 49)
|
68 | export const bgRedBright = init(101, 49)
|
69 | export const bgGreenBright = init(102, 49)
|
70 | export const bgYellowBright = init(103, 49)
|
71 | export const bgBlueBright = init(104, 49)
|
72 | export const bgMagentaBright = init(105, 49)
|
73 | export const bgCyanBright = init(106, 49)
|
74 | export const bgWhiteBright = init(107, 49)
|
75 |
|
76 | const none = (any) => any
|
77 |
|
78 | export const createColors = ({ useColor = isColorSupported } = {}) => ({
|
79 | ...Object.entries({
|
80 | reset,
|
81 | bold,
|
82 | dim,
|
83 | italic,
|
84 | underline,
|
85 | inverse,
|
86 | hidden,
|
87 | strikethrough,
|
88 | black,
|
89 | red,
|
90 | green,
|
91 | yellow,
|
92 | blue,
|
93 | magenta,
|
94 | cyan,
|
95 | white,
|
96 | gray,
|
97 | bgBlack,
|
98 | bgRed,
|
99 | bgGreen,
|
100 | bgYellow,
|
101 | bgBlue,
|
102 | bgMagenta,
|
103 | bgCyan,
|
104 | bgWhite,
|
105 | blackBright,
|
106 | redBright,
|
107 | greenBright,
|
108 | yellowBright,
|
109 | blueBright,
|
110 | magentaBright,
|
111 | cyanBright,
|
112 | whiteBright,
|
113 | bgBlackBright,
|
114 | bgRedBright,
|
115 | bgGreenBright,
|
116 | bgYellowBright,
|
117 | bgBlueBright,
|
118 | bgMagentaBright,
|
119 | bgCyanBright,
|
120 | bgWhiteBright,
|
121 | }).reduce((colorMap, [key, color]) => ({
|
122 | ...colorMap,
|
123 | [key]: useColor ? color : none,
|
124 | })),
|
125 | })
|