UNPKG

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