1 | let argv = process.argv || [],
|
---|
2 | env = process.env
|
---|
3 | let isColorSupported =
|
---|
4 | !("NO_COLOR" in env || argv.includes("--no-color")) &&
|
---|
5 | ("FORCE_COLOR" in env ||
|
---|
6 | argv.includes("--color") ||
|
---|
7 | process.platform === "win32" ||
|
---|
8 | (require != null && require("tty").isatty(1) && env.TERM !== "dumb") ||
|
---|
9 | "CI" in env)
|
---|
10 |
|
---|
11 | let formatter =
|
---|
12 | (open, close, replace = open) =>
|
---|
13 | input => {
|
---|
14 | let string = "" + input
|
---|
15 | let index = string.indexOf(close, open.length)
|
---|
16 | return ~index
|
---|
17 | ? open + replaceClose(string, close, replace, index) + close
|
---|
18 | : open + string + close
|
---|
19 | }
|
---|
20 |
|
---|
21 | let replaceClose = (string, close, replace, index) => {
|
---|
22 | let result = ""
|
---|
23 | let cursor = 0
|
---|
24 | do {
|
---|
25 | result += string.substring(cursor, index) + replace
|
---|
26 | cursor = index + close.length
|
---|
27 | index = string.indexOf(close, cursor)
|
---|
28 | } while (~index)
|
---|
29 | return result + string.substring(cursor)
|
---|
30 | }
|
---|
31 |
|
---|
32 | let createColors = (enabled = isColorSupported) => {
|
---|
33 | let init = enabled ? formatter : () => String
|
---|
34 | return {
|
---|
35 | isColorSupported: enabled,
|
---|
36 | reset: init("\x1b[0m", "\x1b[0m"),
|
---|
37 | bold: init("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
|
---|
38 | dim: init("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
|
---|
39 | italic: init("\x1b[3m", "\x1b[23m"),
|
---|
40 | underline: init("\x1b[4m", "\x1b[24m"),
|
---|
41 | inverse: init("\x1b[7m", "\x1b[27m"),
|
---|
42 | hidden: init("\x1b[8m", "\x1b[28m"),
|
---|
43 | strikethrough: init("\x1b[9m", "\x1b[29m"),
|
---|
44 | black: init("\x1b[30m", "\x1b[39m"),
|
---|
45 | red: init("\x1b[31m", "\x1b[39m"),
|
---|
46 | green: init("\x1b[32m", "\x1b[39m"),
|
---|
47 | yellow: init("\x1b[33m", "\x1b[39m"),
|
---|
48 | blue: init("\x1b[34m", "\x1b[39m"),
|
---|
49 | magenta: init("\x1b[35m", "\x1b[39m"),
|
---|
50 | cyan: init("\x1b[36m", "\x1b[39m"),
|
---|
51 | white: init("\x1b[37m", "\x1b[39m"),
|
---|
52 | gray: init("\x1b[90m", "\x1b[39m"),
|
---|
53 | bgBlack: init("\x1b[40m", "\x1b[49m"),
|
---|
54 | bgRed: init("\x1b[41m", "\x1b[49m"),
|
---|
55 | bgGreen: init("\x1b[42m", "\x1b[49m"),
|
---|
56 | bgYellow: init("\x1b[43m", "\x1b[49m"),
|
---|
57 | bgBlue: init("\x1b[44m", "\x1b[49m"),
|
---|
58 | bgMagenta: init("\x1b[45m", "\x1b[49m"),
|
---|
59 | bgCyan: init("\x1b[46m", "\x1b[49m"),
|
---|
60 | bgWhite: init("\x1b[47m", "\x1b[49m"),
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | module.exports = createColors()
|
---|
65 | module.exports.createColors = createColors
|
---|