-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
201 lines (178 loc) · 5.55 KB
/
index.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
let enabled = true;
// Support both browser and node environments
const globalVar =
typeof self !== 'undefined'
? self
: typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: ({} as any);
export const enum SupportLevel {
none,
ansi,
ansi256,
trueColor,
}
/**
* Detect how much colors the current terminal supports
*/
let supportLevel: SupportLevel = SupportLevel.none;
if (globalVar.process && globalVar.process.env && globalVar.process.stdout) {
const { FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, COLORTERM } =
globalVar.process.env;
if (NODE_DISABLE_COLORS || NO_COLOR || FORCE_COLOR === '0') {
enabled = false;
} else if (
FORCE_COLOR === '1' ||
FORCE_COLOR === '2' ||
FORCE_COLOR === '3'
) {
enabled = true;
} else if (TERM === 'dumb') {
enabled = false;
} else if (
'CI' in globalVar.process.env &&
[
'TRAVIS',
'CIRCLECI',
'APPVEYOR',
'GITLAB_CI',
'GITHUB_ACTIONS',
'BUILDKITE',
'DRONE',
].some(vendor => vendor in globalVar.process.env)
) {
enabled = true;
} else {
enabled = process.stdout.isTTY;
}
if (enabled) {
// Windows supports 24bit True Colors since Windows 10 revision #14931,
// see https://devblogs.microsoft.com/commandline/24-bit-color-in-the-windows-console/
if (process.platform === 'win32') {
supportLevel = SupportLevel.trueColor;
} else {
if (COLORTERM && (COLORTERM === 'truecolor' || COLORTERM === '24bit')) {
supportLevel = SupportLevel.trueColor;
} else if (TERM && (TERM.endsWith('-256color') || TERM.endsWith('256'))) {
supportLevel = SupportLevel.ansi256;
} else {
supportLevel = SupportLevel.ansi;
}
}
}
}
export let options = {
enabled,
supportLevel,
};
function kolorist(
start: number | string,
end: number | string,
level: SupportLevel = SupportLevel.ansi
) {
const open = `\x1b[${start}m`;
const close = `\x1b[${end}m`;
const regex = new RegExp(`\\x1b\\[${end}m`, 'g');
return (str: string | number) => {
return options.enabled && options.supportLevel >= level
? open + ('' + str).replace(regex, open) + close
: '' + str;
};
}
// Lower colors into 256 color space
// Taken from https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
// which is MIT licensed and copyright by Heather Arthur and Josh Junon
function rgbToAnsi256(r: number, g: number, b: number): number {
// We use the extended greyscale palette here, with the exception of
// black and white. normal palette only has 4 greyscale shades.
if (r >> 4 === g >> 4 && g >> 4 === b >> 4) {
if (r < 8) {
return 16;
}
if (r > 248) {
return 231;
}
return Math.round(((r - 8) / 247) * 24) + 232;
}
const ansi =
16 +
36 * Math.round((r / 255) * 5) +
6 * Math.round((g / 255) * 5) +
Math.round((b / 255) * 5);
return ansi;
}
export function stripColors(str: string | number) {
return ('' + str)
.replace(/\x1b\[[0-9;]+m/g, '')
.replace(/\x1b\]8;;.*?\x07(.*?)\x1b\]8;;\x07/g, (_, group) => group);
}
// modifiers
export const reset = kolorist(0, 0);
export const bold = kolorist(1, 22);
export const dim = kolorist(2, 22);
export const italic = kolorist(3, 23);
export const underline = kolorist(4, 24);
export const inverse = kolorist(7, 27);
export const hidden = kolorist(8, 28);
export const strikethrough = kolorist(9, 29);
// colors
export const black = kolorist(30, 39);
export const red = kolorist(31, 39);
export const green = kolorist(32, 39);
export const yellow = kolorist(33, 39);
export const blue = kolorist(34, 39);
export const magenta = kolorist(35, 39);
export const cyan = kolorist(36, 39);
export const white = kolorist(97, 39);
export const gray = kolorist(90, 39);
export const lightGray = kolorist(37, 39);
export const lightRed = kolorist(91, 39);
export const lightGreen = kolorist(92, 39);
export const lightYellow = kolorist(93, 39);
export const lightBlue = kolorist(94, 39);
export const lightMagenta = kolorist(95, 39);
export const lightCyan = kolorist(96, 39);
// background colors
export const bgBlack = kolorist(40, 49);
export const bgRed = kolorist(41, 49);
export const bgGreen = kolorist(42, 49);
export const bgYellow = kolorist(43, 49);
export const bgBlue = kolorist(44, 49);
export const bgMagenta = kolorist(45, 49);
export const bgCyan = kolorist(46, 49);
export const bgWhite = kolorist(107, 49);
export const bgGray = kolorist(100, 49);
export const bgLightRed = kolorist(101, 49);
export const bgLightGreen = kolorist(102, 49);
export const bgLightYellow = kolorist(103, 49);
export const bgLightBlue = kolorist(104, 49);
export const bgLightMagenta = kolorist(105, 49);
export const bgLightCyan = kolorist(106, 49);
export const bgLightGray = kolorist(47, 49);
// 256 support
export const ansi256 = (n: number) =>
kolorist('38;5;' + n, 0, SupportLevel.ansi256);
export const ansi256Bg = (n: number) =>
kolorist('48;5;' + n, 0, SupportLevel.ansi256);
// TrueColor 24bit support
export const trueColor = (r: number, g: number, b: number) => {
return options.supportLevel === SupportLevel.ansi256
? ansi256(rgbToAnsi256(r, g, b))
: kolorist(`38;2;${r};${g};${b}`, 0, SupportLevel.trueColor);
};
export const trueColorBg = (r: number, g: number, b: number) => {
return options.supportLevel === SupportLevel.ansi256
? ansi256Bg(rgbToAnsi256(r, g, b))
: kolorist(`48;2;${r};${g};${b}`, 0, SupportLevel.trueColor);
};
// Links
const OSC = '\u001B]';
const BEL = '\u0007';
const SEP = ';';
export function link(text: string, url: string) {
return options.enabled
? OSC + '8' + SEP + SEP + url + BEL + text + OSC + '8' + SEP + SEP + BEL
: `${text} (\u200B${url}\u200B)`;
}