-
Notifications
You must be signed in to change notification settings - Fork 84
/
utils.go
82 lines (68 loc) · 1.59 KB
/
utils.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
package color
import (
"io"
"os"
"strings"
)
// IsConsole 判断 w 是否为 stderr、stdout、stdin 三者之一
func IsConsole(out io.Writer) bool {
o, ok := out.(*os.File)
if !ok {
return false
}
return o == os.Stdout || o == os.Stderr || o == os.Stdin
}
// IsMSys msys(MINGW64) 环境,不一定支持颜色
func IsMSys() bool {
// like "MSYSTEM=MINGW64"
if len(os.Getenv("MSYSTEM")) > 0 {
return true
}
return false
}
// IsSupportColor check current console is support color.
//
// Supported:
// linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe
// Not support:
// windows cmd.exe, powerShell.exe
func IsSupportColor() bool {
// "TERM=xterm" support color
// "TERM=xterm-vt220" support color
// "TERM=xterm-256color" support color
// "TERM=cygwin" don't support color
if strings.Contains(os.Getenv("TERM"), "xterm") {
return true
}
// like on ConEmu software, e.g "ConEmuANSI=ON"
if os.Getenv("ConEmuANSI") == "ON" {
return true
}
// like on ConEmu software, e.g "ANSICON=189x2000 (189x43)"
if os.Getenv("ANSICON") != "" {
return true
}
return false
}
// IsSupport256Color render
func IsSupport256Color() bool {
// "TERM=xterm-256color"
return strings.Contains(os.Getenv("TERM"), "256color")
}
// its Win system. linux windows darwin
// func isWindows() bool {
// return runtime.GOOS == "windows"
// }
func stringToArr(str, sep string) (arr []string) {
str = strings.TrimSpace(str)
if str == "" {
return
}
ss := strings.Split(str, sep)
for _, val := range ss {
if val = strings.TrimSpace(val); val != "" {
arr = append(arr, val)
}
}
return
}