@@ -2,7 +2,6 @@ package color
2
2
3
3
import (
4
4
"io"
5
- "io/ioutil"
6
5
"os"
7
6
"runtime"
8
7
"strconv"
@@ -12,23 +11,34 @@ import (
12
11
"github.com/xo/terminfo"
13
12
)
14
13
14
+ // Level is the color level supported by a terminal.
15
+ type Level = terminfo.ColorLevel
16
+
17
+ // terminal color available level alias of the terminfo.ColorLevel*
18
+ const (
19
+ LevelNo = terminfo .ColorLevelNone // not support color.
20
+ Level16 = terminfo .ColorLevelBasic // basic - 3/4 bit color supported
21
+ Level256 = terminfo .ColorLevelHundreds // hundreds - 8-bit color supported
22
+ LevelRgb = terminfo .ColorLevelMillions // millions - (24 bit)true color supported
23
+ )
24
+
15
25
/*************************************************************
16
26
* helper methods for detect color supports
17
27
*************************************************************/
18
28
19
29
// DetectColorLevel for current env
20
30
//
21
31
// NOTICE: The method will detect terminal info each times,
22
- // if only want get current color level, please direct call SupportColor() or TermColorLevel()
23
- func DetectColorLevel () terminfo. ColorLevel {
32
+ // if only want to get current color level, please direct call SupportColor() or TermColorLevel()
33
+ func DetectColorLevel () Level {
24
34
level , _ := detectTermColorLevel ()
25
35
return level
26
36
}
27
37
28
38
// detect terminal color support level
29
39
//
30
40
// refer https://github.com/Delta456/box-cli-maker
31
- func detectTermColorLevel () (level terminfo. ColorLevel , needVTP bool ) {
41
+ func detectTermColorLevel () (level Level , needVTP bool ) {
32
42
// on windows WSL:
33
43
// - runtime.GOOS == "Linux"
34
44
// - support true-color
@@ -76,7 +86,7 @@ func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
76
86
//
77
87
// refer the terminfo.ColorLevelFromEnv()
78
88
// https://en.wikipedia.org/wiki/Terminfo
79
- func detectColorLevelFromEnv (termVal string , isWin bool ) terminfo. ColorLevel {
89
+ func detectColorLevelFromEnv (termVal string , isWin bool ) Level {
80
90
// check for overriding environment variables
81
91
colorTerm , termProg , forceColor := os .Getenv ("COLORTERM" ), os .Getenv ("TERM_PROGRAM" ), os .Getenv ("FORCE_COLOR" )
82
92
switch {
@@ -172,6 +182,7 @@ func detectWSL() bool {
172
182
return false
173
183
}
174
184
185
+ /*
175
186
// refer
176
187
// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_unix.go#L27-L45
177
188
// detect WSL as it has True Color support
@@ -198,10 +209,11 @@ func isWSL() bool {
198
209
}
199
210
200
211
// it gives "Microsoft" for WSL and "microsoft" for WSL 2
201
- // it support True-color
212
+ // it supports True-color
202
213
content := strings.ToLower(string(wsl))
203
214
return strings.Contains(content, "microsoft")
204
215
}
216
+ */
205
217
206
218
/*************************************************************
207
219
* helper methods for check env
@@ -228,11 +240,7 @@ func IsConsole(w io.Writer) bool {
228
240
// IsMSys msys(MINGW64) environment, does not necessarily support color
229
241
func IsMSys () bool {
230
242
// like "MSYSTEM=MINGW64"
231
- if len (os .Getenv ("MSYSTEM" )) > 0 {
232
- return true
233
- }
234
-
235
- return false
243
+ return len (os .Getenv ("MSYSTEM" )) > 0
236
244
}
237
245
238
246
// IsSupportColor check current console is support color.
@@ -243,7 +251,7 @@ func IsSupportColor() bool {
243
251
return IsSupport16Color ()
244
252
}
245
253
246
- // IsSupportColor check current console is support color.
254
+ // IsSupport16Color check current console is support color.
247
255
//
248
256
// NOTICE: The method will detect terminal info each times,
249
257
// if only want get current color level, please direct call SupportColor() or TermColorLevel()
@@ -255,7 +263,7 @@ func IsSupport16Color() bool {
255
263
// IsSupport256Color render check
256
264
//
257
265
// NOTICE: The method will detect terminal info each times,
258
- // if only want get current color level, please direct call SupportColor() or TermColorLevel()
266
+ // if only want to get current color level, please direct call SupportColor() or TermColorLevel()
259
267
func IsSupport256Color () bool {
260
268
level , _ := detectTermColorLevel ()
261
269
return level > terminfo .ColorLevelBasic
0 commit comments