-
Notifications
You must be signed in to change notification settings - Fork 0
/
supcolor_linux.go
111 lines (95 loc) · 1.89 KB
/
supcolor_linux.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
package supcolor
import (
"os"
"strconv"
"strings"
"gopkg.ilharper.com/x/isatty"
)
func SupColor(stream *os.File) int8 {
env := os.Environ()
for _, e := range env {
if strings.HasPrefix(e, "FORCE_COLOR=") {
// Use user forced color mode
s := e[12:]
if len(s) == 0 {
return 1
}
if s == "true" {
return 1
}
if s == "false" {
return 0
}
i64, err := strconv.ParseInt(s, 10, 8)
i := int8(i64)
if err != nil {
// Something that's not integer
return 1
}
if i < 3 {
return i
}
return 3
}
}
if stream == nil {
// There's even no stream??
return 0
}
for _, e := range env {
if strings.HasPrefix(e, "CI=") {
if strings.HasPrefix(e, "TRAVIS=") ||
strings.HasPrefix(e, "CIRCLECI=") ||
strings.HasPrefix(e, "APPVEYOR=") ||
strings.HasPrefix(e, "GITLAB_CI=") ||
strings.HasPrefix(e, "GITHUB_ACTIONS=") ||
strings.HasPrefix(e, "BUILDKITE=") ||
strings.HasPrefix(e, "DRONE=") {
return 1
}
}
if e == "CI_NAME=codeship" {
return 1
}
if strings.HasPrefix(e, "TEAMCITY_VERSION=") {
return 1
}
if strings.HasPrefix(e, "TF_BUILD=") &&
strings.HasPrefix(e, "AGENT_NAME=") {
return 1
}
}
if !isatty.Isatty(stream.Fd()) {
// Isn't a TTY
return 0
}
for _, e := range env {
if e == "TERM=dumb" {
return 0
}
if strings.HasPrefix(e, "COLORTERM=") {
if e == "COLORTERM=truecolor" {
return 3
}
return 1
}
if strings.HasPrefix(e, "TERM=") {
s := e[5:]
if strings.Contains(s, "256") {
return 3
}
if strings.Contains(s, "screen") ||
strings.Contains(s, "xterm") ||
strings.Contains(s, "vt100") ||
strings.Contains(s, "vt220") ||
strings.Contains(s, "rxvt") ||
strings.Contains(s, "color") ||
strings.Contains(s, "ansi") ||
strings.Contains(s, "cygwin") ||
strings.Contains(s, "linux") {
return 1
}
}
}
return 0
}