forked from ai/nanocolors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (120 loc) · 3.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import tty from 'tty'
let isDisabled =
'NO_COLOR' in process.env || process.argv.includes('--no-color')
let isForced = 'FORCE_COLOR' in process.env || process.argv.includes('--color')
let isWindows = process.platform === 'win32'
let isCompatibleTerminal =
tty && tty.isatty(1) && process.env.TERM && process.env.TERM !== 'dumb'
let isCI =
'CI' in process.env &&
('GITHUB_ACTIONS' in process.env ||
'GITLAB_CI' in process.env ||
'CIRCLECI' in process.env)
export let isColorSupported =
!isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI)
let nope = s => String(s)
function color(open, close, closeRegexp) {
return s => {
if (s === '') {
return s
} else {
return (
open +
(!!~('' + s).indexOf(close, 4) ? s.replace(closeRegexp, open) : s) +
close
)
}
}
}
let close39 = '\x1b[39m'
let close49 = '\x1b[49m'
let regexp39 = /\x1b\[39m/g
let regexp49 = /\x1b\[49m/g
export function createColors(enabled = isColorSupported) {
if (enabled) {
return {
isColorSupported: true,
reset: s => `\x1b[0m${s}\x1b[0m`,
bold: color('\x1b[1m', '\x1b[22m', /\x1b\[22m/g),
dim: color('\x1b[2m', '\x1b[22m', /\x1b\[22m/g),
italic: color('\x1b[3m', '\x1b[23m', /\x1b\[23m/g),
underline: color('\x1b[4m', '\x1b[24m', /\x1b\[24m/g),
inverse: color('\x1b[7m', '\x1b[27m', /\x1b\[27m/g),
hidden: color('\x1b[8m', '\x1b[28m', /\x1b\[28m/g),
strikethrough: color('\x1b[9m', '\x1b[29m', /\x1b\[29m/g),
black: color('\x1b[30m', close39, regexp39),
red: color('\x1b[31m', close39, regexp39),
green: color('\x1b[32m', close39, regexp39),
yellow: color('\x1b[33m', close39, regexp39),
blue: color('\x1b[34m', close39, regexp39),
magenta: color('\x1b[35m', close39, regexp39),
cyan: color('\x1b[36m', close39, regexp39),
white: color('\x1b[37m', close39, regexp39),
gray: color('\x1b[90m', close39, regexp39),
bgBlack: color('\x1b[40m', close49, regexp49),
bgRed: color('\x1b[41m', close49, regexp49),
bgGreen: color('\x1b[42m', close49, regexp49),
bgYellow: color('\x1b[43m', close49, regexp49),
bgBlue: color('\x1b[44m', close49, regexp49),
bgMagenta: color('\x1b[45m', close49, regexp49),
bgCyan: color('\x1b[46m', close49, regexp49),
bgWhite: color('\x1b[47m', close49, regexp49)
}
} else {
return {
isColorSupported: false,
reset: nope,
bold: nope,
dim: nope,
italic: nope,
underline: nope,
inverse: nope,
hidden: nope,
strikethrough: nope,
black: nope,
red: nope,
green: nope,
yellow: nope,
blue: nope,
magenta: nope,
cyan: nope,
white: nope,
gray: nope,
bgBlack: nope,
bgRed: nope,
bgGreen: nope,
bgYellow: nope,
bgBlue: nope,
bgMagenta: nope,
bgCyan: nope,
bgWhite: nope
}
}
}
export let {
reset,
bold,
dim,
italic,
underline,
inverse,
hidden,
strikethrough,
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
gray,
bgBlack,
bgRed,
bgGreen,
bgYellow,
bgBlue,
bgMagenta,
bgCyan,
bgWhite
} = createColors(isColorSupported)