-
Notifications
You must be signed in to change notification settings - Fork 0
/
supcolor_plan9.go
84 lines (72 loc) · 1.31 KB
/
supcolor_plan9.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
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
}
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
}