-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcolors.go
346 lines (283 loc) · 7.61 KB
/
colors.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// This package is inspired by the chalk's ansi-styles and supports-color
// (JavaScript).
//
// See: https://en.wikipedia.org/wiki/ANSI_escape_code .
// See: https://misc.flogisoft.com/bash/tip_colors_and_formatting .
// See: https://www.npmjs.com/package/ansi-styles .
// See: https://www.npmjs.com/package/supports-color .
// See: https://github.com/termstandard/colors .
package colors
import (
"os"
"regexp"
"strconv"
"github.com/mattn/go-isatty"
)
type termSupports uint8
const (
supportsColor termSupports = 1 << iota
supportsANSI256
supportsTrueColor
)
// TODO(SuperPaintman):
// Remove using the regexp package. Because we need it only for one thing.
var (
teamCityVersionRe = regexp.MustCompile(`^(9\.(0*[1-9]\d*)\.|\d{2,}\.)`)
termRe = regexp.MustCompile(`(?i)^screen|^xterm|^vt100|^vt220|^rxvt`)
term256ColorRe = regexp.MustCompile(`(?i)-256(color)?$`)
iTermAppVersionRe = regexp.MustCompile(`^(\d+)\.`)
)
func terminalSupports(lookup func(key string) (string, bool)) (s termSupports) {
// Terminals.
term, ok := lookup("TERM")
if ok && term == "dumb" {
return
}
if colorTerm, ok := lookup("COLORTERM"); ok {
s |= supportsColor
if colorTerm == "truecolor" {
s |= supportsANSI256
s |= supportsTrueColor
}
return
}
if termProg, ok := lookup("TERM_PROGRAM"); ok {
if termProg == "iTerm.app" {
s |= supportsColor
s |= supportsANSI256
if v, ok := lookup("TERM_PROGRAM_VERSION"); ok {
raw := iTermAppVersionRe.FindString(v)
if raw == "" {
return
}
version, err := strconv.Atoi(raw[:len(raw)-1]) // Eat '.'.
if err == nil && version >= 3 {
s |= supportsTrueColor
}
}
return
}
if termProg == "Apple_Terminal" {
s |= supportsColor
s |= supportsANSI256
s |= supportsTrueColor
return
}
}
if term != "" {
if term256ColorRe.MatchString(term) {
s |= supportsColor
s |= supportsANSI256
return
}
if termRe.MatchString(term) {
s |= supportsColor
return
}
}
// TODO(SuperPaintman): add win32 checker.
// CI.
if _, ok := lookup("CI"); ok {
cis := [...]string{
"TRAVIS",
"CIRCLECI",
"APPVEYOR",
"GITLAB_CI",
"GITHUB_ACTIONS",
"BUILDKITE",
"DRONE",
}
for _, name := range cis {
if _, ok := lookup(name); ok {
s |= supportsColor
return
}
}
if name, ok := lookup("CI_NAME"); ok && name == "codeship" {
s |= supportsColor
return
}
}
// TeamCity.
if version, ok := lookup("TEAMCITY_VERSION"); ok {
if teamCityVersionRe.MatchString(version) {
s |= supportsColor
return
}
}
// TODO(SuperPaintman): add cygwin.
return
}
func SupportsColor() bool { return supports&supportsColor != 0 }
func SupportsANSI256() bool { return supports&supportsANSI256 != 0 }
func SupportsTrueColor() bool { return supports&supportsTrueColor != 0 }
type Mode uint8
const (
Auto Mode = 1 << iota >> 1
Never
Always
ForceANSI256
ForceTrueColor
)
var (
supports termSupports
mode Mode
shouldUseColors, shouldUseANSI256, shouldUseTrueColor bool
)
func init() {
// Check is TTY.
isTTY := isatty.IsTerminal(os.Stdout.Fd()) ||
isatty.IsCygwinTerminal(os.Stdout.Fd())
if isTTY {
supports = terminalSupports(os.LookupEnv)
}
shouldUseColors, shouldUseANSI256, shouldUseTrueColor = computeShouldUse(mode, supports)
}
func SetMode(m Mode) {
mode = m
shouldUseColors, shouldUseANSI256, shouldUseTrueColor = computeShouldUse(mode, supports)
}
func computeShouldUse(m Mode, s termSupports) (colors bool, ansi256 bool, trueColor bool) {
if m&Never == 0 {
colors = m&Always != 0 || s&supportsColor != 0
ansi256 = colors &&
(s&supportsANSI256 != 0 || m&ForceANSI256 != 0)
trueColor = colors &&
(s&supportsTrueColor != 0 || m&ForceTrueColor != 0)
}
return
}
type Attribute uint8
func (a Attribute) String() string {
if !shouldUseColors {
return ""
}
return attributeToString(uint8(a))
}
//go:generate python ./generate_reset_attributes.py
func (a Attribute) Reset() Attribute {
return resetAttributes[a]
}
const (
Reset Attribute = 0
ResetBold Attribute = 22 // 21 isn't widely supported and 22 does the same thing.
ResetDim Attribute = 22
ResetItalic Attribute = 23
ResetUnderline Attribute = 24
ResetInverse Attribute = 27
ResetHidden Attribute = 28
ResetStrikethrough Attribute = 29
ResetOverline Attribute = 55
Bold Attribute = 1
Dim Attribute = 2
Italic Attribute = 3
Underline Attribute = 4
Inverse Attribute = 7
Hidden Attribute = 8
Strikethrough Attribute = 9
Overline Attribute = 53
)
const (
ResetColor Attribute = 39
Black Attribute = 30
Red Attribute = 31
Green Attribute = 32
Yellow Attribute = 33
Blue Attribute = 34
Magenta Attribute = 35
Cyan Attribute = 36
White Attribute = 37
BlackBright Attribute = 90
RedBright Attribute = 91
GreenBright Attribute = 92
YellowBright Attribute = 93
BlueBright Attribute = 94
MagentaBright Attribute = 95
CyanBright Attribute = 96
WhiteBright Attribute = 97
// Aliases.
Gray Attribute = BlackBright
)
const (
ResetBgColor Attribute = 49
BgBlack Attribute = 40
BgRed Attribute = 41
BgGreen Attribute = 42
BgYellow Attribute = 43
BgBlue Attribute = 44
BgMagenta Attribute = 45
BgCyan Attribute = 46
BgWhite Attribute = 47
BgBlackBright Attribute = 100
BgRedBright Attribute = 101
BgGreenBright Attribute = 102
BgYellowBright Attribute = 103
BgBlueBright Attribute = 104
BgMagentaBright Attribute = 105
BgCyanBright Attribute = 106
BgWhiteBright Attribute = 107
// Aliases.
BgGray Attribute = BgBlackBright
)
//go:generate python ./generate_ansi_attribute_string.py
// attributeToString converts Attribute to string.
//
// A hack for fast and inlinable Attribute to string converion (like what the
// stringer does).
//
// Unfortunately Go can't inline strconv.Itoa (at least Go 1.16) and we can't
// write a regular function. So we need some evil slice hacks :(.
//
// If you look in the git log you will find a faster version of this function
// but it has fewer ways to be inlined.
//
// NOTE(SuperPaintman): I would like to remove it in the future.
func attributeToString(i uint8) string {
return ansiAttributeString[ansiAttributeIndex[uint16(i)]:ansiAttributeIndex[uint16(i)+1]]
}
// TODO(SuperPaintman): make it inlinable.
func ANSI256(color uint8) string {
if !shouldUseANSI256 {
return ""
}
// TODO(SuperPaintman): optimize it with a preassembled slice.
return "\x1b[38;5;" + strconv.Itoa(int(color)) + "m"
}
// TODO(SuperPaintman): make it inlinable.
func BgANSI256(color uint8) string {
if !shouldUseANSI256 {
return ""
}
// TODO(SuperPaintman): optimize it with a preassembled slice.
return "\x1b[48;5;" + strconv.Itoa(int(color)) + "m"
}
// 24-bit or truecolor or ANSI 16 millions.
func TrueColor(r, g, b uint8) string {
if !shouldUseTrueColor {
return ""
}
// TODO(SuperPaintman): optimize it with a preassembled slice.
return "\x1b[38;2;" +
strconv.Itoa(int(r)) + ";" +
strconv.Itoa(int(g)) + ";" +
strconv.Itoa(int(b)) + "m"
}
func BgTrueColor(r, g, b uint8) string {
if !shouldUseTrueColor {
return ""
}
// TODO(SuperPaintman): optimize it with a preassembled slice for uint8.
return "\x1b[48;2;" +
strconv.Itoa(int(r)) + ";" +
strconv.Itoa(int(g)) + ";" +
strconv.Itoa(int(b)) + "m"
}
type RGB struct {
R, G, B uint8
}
func TrueColorRGB(color RGB) string {
return TrueColor(color.R, color.G, color.B)
}
func BgTrueColorRGB(color RGB) string {
return BgTrueColor(color.R, color.G, color.B)
}