UNPKG

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