1 | import * as tty from "tty"
|
---|
2 |
|
---|
3 | const {
|
---|
4 | env = {},
|
---|
5 | argv = [],
|
---|
6 | platform = "",
|
---|
7 | } = typeof process === "undefined" ? {} : process
|
---|
8 |
|
---|
9 | const isDisabled = "NO_COLOR" in env || argv.includes("--no-color")
|
---|
10 | const isForced = "FORCE_COLOR" in env || argv.includes("--color")
|
---|
11 | const isWindows = platform === "win32"
|
---|
12 | const isDumbTerminal = env.TERM === "dumb"
|
---|
13 |
|
---|
14 | const isCompatibleTerminal =
|
---|
15 | tty && tty.isatty && tty.isatty(1) && env.TERM && !isDumbTerminal
|
---|
16 |
|
---|
17 | const isCI =
|
---|
18 | "CI" in env &&
|
---|
19 | ("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env)
|
---|
20 |
|
---|
21 | export const isColorSupported =
|
---|
22 | !isDisabled &&
|
---|
23 | (isForced || (isWindows && !isDumbTerminal) || isCompatibleTerminal || isCI)
|
---|
24 |
|
---|
25 | const replaceClose = (
|
---|
26 | index,
|
---|
27 | string,
|
---|
28 | close,
|
---|
29 | replace,
|
---|
30 | head = string.substring(0, index) + replace,
|
---|
31 | tail = string.substring(index + close.length),
|
---|
32 | next = tail.indexOf(close)
|
---|
33 | ) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace))
|
---|
34 |
|
---|
35 | const clearBleed = (index, string, open, close, replace) =>
|
---|
36 | index < 0
|
---|
37 | ? open + string + close
|
---|
38 | : open + replaceClose(index, string, close, replace) + close
|
---|
39 |
|
---|
40 | const filterEmpty =
|
---|
41 | (open, close, replace = open, at = open.length + 1) =>
|
---|
42 | (string) =>
|
---|
43 | string || !(string === "" || string === undefined)
|
---|
44 | ? clearBleed(
|
---|
45 | ("" + string).indexOf(close, at),
|
---|
46 | string,
|
---|
47 | open,
|
---|
48 | close,
|
---|
49 | replace
|
---|
50 | )
|
---|
51 | : ""
|
---|
52 |
|
---|
53 | const init = (open, close, replace) =>
|
---|
54 | filterEmpty(`\x1b[${open}m`, `\x1b[${close}m`, replace)
|
---|
55 |
|
---|
56 | const colors = {
|
---|
57 | reset: init(0, 0),
|
---|
58 | bold: init(1, 22, "\x1b[22m\x1b[1m"),
|
---|
59 | dim: init(2, 22, "\x1b[22m\x1b[2m"),
|
---|
60 | italic: init(3, 23),
|
---|
61 | underline: init(4, 24),
|
---|
62 | inverse: init(7, 27),
|
---|
63 | hidden: init(8, 28),
|
---|
64 | strikethrough: init(9, 29),
|
---|
65 | black: init(30, 39),
|
---|
66 | red: init(31, 39),
|
---|
67 | green: init(32, 39),
|
---|
68 | yellow: init(33, 39),
|
---|
69 | blue: init(34, 39),
|
---|
70 | magenta: init(35, 39),
|
---|
71 | cyan: init(36, 39),
|
---|
72 | white: init(37, 39),
|
---|
73 | gray: init(90, 39),
|
---|
74 | bgBlack: init(40, 49),
|
---|
75 | bgRed: init(41, 49),
|
---|
76 | bgGreen: init(42, 49),
|
---|
77 | bgYellow: init(43, 49),
|
---|
78 | bgBlue: init(44, 49),
|
---|
79 | bgMagenta: init(45, 49),
|
---|
80 | bgCyan: init(46, 49),
|
---|
81 | bgWhite: init(47, 49),
|
---|
82 | blackBright: init(90, 39),
|
---|
83 | redBright: init(91, 39),
|
---|
84 | greenBright: init(92, 39),
|
---|
85 | yellowBright: init(93, 39),
|
---|
86 | blueBright: init(94, 39),
|
---|
87 | magentaBright: init(95, 39),
|
---|
88 | cyanBright: init(96, 39),
|
---|
89 | whiteBright: init(97, 39),
|
---|
90 | bgBlackBright: init(100, 49),
|
---|
91 | bgRedBright: init(101, 49),
|
---|
92 | bgGreenBright: init(102, 49),
|
---|
93 | bgYellowBright: init(103, 49),
|
---|
94 | bgBlueBright: init(104, 49),
|
---|
95 | bgMagentaBright: init(105, 49),
|
---|
96 | bgCyanBright: init(106, 49),
|
---|
97 | bgWhiteBright: init(107, 49),
|
---|
98 | }
|
---|
99 |
|
---|
100 | export const createColors = ({ useColor = isColorSupported } = {}) =>
|
---|
101 | useColor
|
---|
102 | ? colors
|
---|
103 | : Object.keys(colors).reduce(
|
---|
104 | (colors, key) => ({ ...colors, [key]: String }),
|
---|
105 | {}
|
---|
106 | )
|
---|
107 |
|
---|
108 | export const {
|
---|
109 | reset,
|
---|
110 | bold,
|
---|
111 | dim,
|
---|
112 | italic,
|
---|
113 | underline,
|
---|
114 | inverse,
|
---|
115 | hidden,
|
---|
116 | strikethrough,
|
---|
117 | black,
|
---|
118 | red,
|
---|
119 | green,
|
---|
120 | yellow,
|
---|
121 | blue,
|
---|
122 | magenta,
|
---|
123 | cyan,
|
---|
124 | white,
|
---|
125 | gray,
|
---|
126 | bgBlack,
|
---|
127 | bgRed,
|
---|
128 | bgGreen,
|
---|
129 | bgYellow,
|
---|
130 | bgBlue,
|
---|
131 | bgMagenta,
|
---|
132 | bgCyan,
|
---|
133 | bgWhite,
|
---|
134 | blackBright,
|
---|
135 | redBright,
|
---|
136 | greenBright,
|
---|
137 | yellowBright,
|
---|
138 | blueBright,
|
---|
139 | magentaBright,
|
---|
140 | cyanBright,
|
---|
141 | whiteBright,
|
---|
142 | bgBlackBright,
|
---|
143 | bgRedBright,
|
---|
144 | bgGreenBright,
|
---|
145 | bgYellowBright,
|
---|
146 | bgBlueBright,
|
---|
147 | bgMagentaBright,
|
---|
148 | bgCyanBright,
|
---|
149 | bgWhiteBright,
|
---|
150 | } = createColors()
|
---|