Skip to content

Commit 2f5b36d

Browse files
committed
chore: add color level alias of the terminfo.ColorLevel
1 parent c3c9e47 commit 2f5b36d

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

detect_env.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package color
22

33
import (
44
"io"
5-
"io/ioutil"
65
"os"
76
"runtime"
87
"strconv"
@@ -12,23 +11,34 @@ import (
1211
"github.com/xo/terminfo"
1312
)
1413

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+
1525
/*************************************************************
1626
* helper methods for detect color supports
1727
*************************************************************/
1828

1929
// DetectColorLevel for current env
2030
//
2131
// 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 {
2434
level, _ := detectTermColorLevel()
2535
return level
2636
}
2737

2838
// detect terminal color support level
2939
//
3040
// refer https://github.com/Delta456/box-cli-maker
31-
func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
41+
func detectTermColorLevel() (level Level, needVTP bool) {
3242
// on windows WSL:
3343
// - runtime.GOOS == "Linux"
3444
// - support true-color
@@ -76,7 +86,7 @@ func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
7686
//
7787
// refer the terminfo.ColorLevelFromEnv()
7888
// https://en.wikipedia.org/wiki/Terminfo
79-
func detectColorLevelFromEnv(termVal string, isWin bool) terminfo.ColorLevel {
89+
func detectColorLevelFromEnv(termVal string, isWin bool) Level {
8090
// check for overriding environment variables
8191
colorTerm, termProg, forceColor := os.Getenv("COLORTERM"), os.Getenv("TERM_PROGRAM"), os.Getenv("FORCE_COLOR")
8292
switch {
@@ -172,6 +182,7 @@ func detectWSL() bool {
172182
return false
173183
}
174184

185+
/*
175186
// refer
176187
// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_unix.go#L27-L45
177188
// detect WSL as it has True Color support
@@ -198,10 +209,11 @@ func isWSL() bool {
198209
}
199210
200211
// it gives "Microsoft" for WSL and "microsoft" for WSL 2
201-
// it support True-color
212+
// it supports True-color
202213
content := strings.ToLower(string(wsl))
203214
return strings.Contains(content, "microsoft")
204215
}
216+
*/
205217

206218
/*************************************************************
207219
* helper methods for check env
@@ -228,11 +240,7 @@ func IsConsole(w io.Writer) bool {
228240
// IsMSys msys(MINGW64) environment, does not necessarily support color
229241
func IsMSys() bool {
230242
// 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
236244
}
237245

238246
// IsSupportColor check current console is support color.
@@ -243,7 +251,7 @@ func IsSupportColor() bool {
243251
return IsSupport16Color()
244252
}
245253

246-
// IsSupportColor check current console is support color.
254+
// IsSupport16Color check current console is support color.
247255
//
248256
// NOTICE: The method will detect terminal info each times,
249257
// if only want get current color level, please direct call SupportColor() or TermColorLevel()
@@ -255,7 +263,7 @@ func IsSupport16Color() bool {
255263
// IsSupport256Color render check
256264
//
257265
// 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()
259267
func IsSupport256Color() bool {
260268
level, _ := detectTermColorLevel()
261269
return level > terminfo.ColorLevelBasic

detect_nonwin.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !windows
12
// +build !windows
23

34
// The method in the file has no effect
@@ -13,7 +14,7 @@ import (
1314
)
1415

1516
// detect special term color support
16-
func detectSpecialTermColor(termVal string) (terminfo.ColorLevel, bool) {
17+
func detectSpecialTermColor(termVal string) (Level, bool) {
1718
if termVal == "" {
1819
return terminfo.ColorLevelNone, false
1920
}

detect_windows.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
// Display color on windows
@@ -113,8 +114,8 @@ var (
113114
// refer
114115
// https://github.com/Delta456/box-cli-maker/blob/7b5a1ad8a016ce181e7d8b05e24b54ff60b4b38a/detect_windows.go#L30-L57
115116
// https://github.com/gookit/color/issues/25#issuecomment-738727917
116-
// detects the Color Level Supported on windows: cmd, powerShell
117-
func detectSpecialTermColor(termVal string) (tl terminfo.ColorLevel, needVTP bool) {
117+
// detects the color level supported on Windows: cmd, powerShell
118+
func detectSpecialTermColor(termVal string) (tl Level, needVTP bool) {
118119
if os.Getenv("ConEmuANSI") == "ON" {
119120
debugf("support True Color by ConEmuANSI=ON")
120121
// ConEmuANSI is "ON" for generic ANSI support

0 commit comments

Comments
 (0)