Skip to content

Commit 173325a

Browse files
committed
👔 up: update detect env, use internal Level* instead of the terminfo.ColorLevel*
1 parent c5db0d0 commit 173325a

File tree

4 files changed

+373
-70
lines changed

4 files changed

+373
-70
lines changed

detect_env.go

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func detectTermColorLevel() (level Level, needVTP bool) {
4949
// detect WSL as it has True Color support
5050
if detectWSL() {
5151
debugf("True Color support on WSL environment")
52-
return terminfo.ColorLevelMillions, false
52+
return LevelRgb, false
5353
}
5454
}
5555

@@ -65,7 +65,7 @@ func detectTermColorLevel() (level Level, needVTP bool) {
6565
val := os.Getenv("TERMINAL_EMULATOR")
6666
if val == "JetBrains-JediTerm" {
6767
debugf("True Color support on JetBrains-JediTerm, is win: %v", isWin)
68-
return terminfo.ColorLevelMillions, isWin
68+
return LevelRgb, isWin
6969
}
7070
}
7171

@@ -74,7 +74,7 @@ func detectTermColorLevel() (level Level, needVTP bool) {
7474
debugf("color level by detectColorLevelFromEnv: %s", level.String())
7575

7676
// fallback: simple detect by TERM value string.
77-
if level == terminfo.ColorLevelNone {
77+
if level == LevelNo {
7878
debugf("level none - fallback check special term color support")
7979
// on Windows: enable VTP as it has True Color support
8080
level, needVTP = detectSpecialTermColor(termVal)
@@ -88,42 +88,40 @@ func detectTermColorLevel() (level Level, needVTP bool) {
8888
// refer the terminfo.ColorLevelFromEnv()
8989
// https://en.wikipedia.org/wiki/Terminfo
9090
func detectColorLevelFromEnv(termVal string, isWin bool) Level {
91+
// on TERM=screen: not support true-color
92+
if termVal == "screen" {
93+
return Level256
94+
}
95+
9196
// check for overriding environment variables
9297
colorTerm, termProg, forceColor := os.Getenv("COLORTERM"), os.Getenv("TERM_PROGRAM"), os.Getenv("FORCE_COLOR")
9398
switch {
9499
case strings.Contains(colorTerm, "truecolor") || strings.Contains(colorTerm, "24bit"):
95-
if termVal == "screen" { // on TERM=screen: not support true-color
96-
return terminfo.ColorLevelHundreds
97-
}
98-
return terminfo.ColorLevelMillions
100+
return LevelRgb
99101
case colorTerm != "" || forceColor != "":
100-
return terminfo.ColorLevelBasic
102+
if strings.Contains(termVal, "256color") {
103+
return Level256
104+
}
105+
return Level16
101106
case termProg == "Apple_Terminal":
102-
return terminfo.ColorLevelHundreds
107+
return Level256
103108
case termProg == "Terminus" || termProg == "Hyper":
104-
if termVal == "screen" { // on TERM=screen: not support true-color
105-
return terminfo.ColorLevelHundreds
106-
}
107-
return terminfo.ColorLevelMillions
109+
return LevelRgb
108110
case termProg == "iTerm.app":
109-
if termVal == "screen" { // on TERM=screen: not support true-color
110-
return terminfo.ColorLevelHundreds
111-
}
112-
113111
// check iTerm version
114112
ver := os.Getenv("TERM_PROGRAM_VERSION")
115113
if ver != "" {
116114
i, err := strconv.Atoi(strings.Split(ver, ".")[0])
117115
if err != nil {
118116
saveInternalError(terminfo.ErrInvalidTermProgramVersion)
119117
// return terminfo.ColorLevelNone
120-
return terminfo.ColorLevelHundreds
118+
return Level256
121119
}
122120
if i == 3 {
123-
return terminfo.ColorLevelMillions
121+
return LevelRgb
124122
}
125123
}
126-
return terminfo.ColorLevelHundreds
124+
return Level256
127125
}
128126

129127
// otherwise determine from TERM's max_colors capability
@@ -132,22 +130,22 @@ func detectColorLevelFromEnv(termVal string, isWin bool) Level {
132130
ti, err := terminfo.Load(termVal)
133131
if err != nil {
134132
saveInternalError(err)
135-
return terminfo.ColorLevelNone
133+
return LevelNo
136134
}
137135

138136
debugf("the loaded term info file is: %s", ti.File)
139137
v, ok := ti.Nums[terminfo.MaxColors]
140138
switch {
141139
case !ok || v <= 16:
142-
return terminfo.ColorLevelNone
140+
return LevelNo
143141
case ok && v >= 256:
144-
return terminfo.ColorLevelHundreds
142+
return Level256
145143
}
146-
return terminfo.ColorLevelBasic
144+
return Level16
147145
}
148146

149147
// no TERM env value. default return none level
150-
return terminfo.ColorLevelNone
148+
return LevelNo
151149
// return terminfo.ColorLevelBasic
152150
}
153151

@@ -161,11 +159,11 @@ func detectWSL() bool {
161159

162160
b := make([]byte, 1024)
163161
// `cat /proc/version`
164-
// on mac:
165-
// !not the file!
162+
// on Mac, Windows cmd/pwsh:
163+
// !NOT THE FILE!
166164
// on linux(debian,ubuntu,alpine):
167165
// Linux version 4.19.121-linuxkit (root@18b3f92ade35) (gcc version 9.2.0 (Alpine 9.2.0)) #1 SMP Thu Jan 21 15:36:34 UTC 2021
168-
// on win git bash, conEmu:
166+
// on Win git bash, conEmu:
169167
// MINGW64_NT-10.0-19042 version 3.1.7-340.x86_64 (@WIN-N0G619FD3UK) (gcc version 9.3.0 (GCC) ) 2020-10-23 13:08 UTC
170168
// on WSL:
171169
// Linux version 4.4.0-19041-Microsoft ([email protected]) (gcc version 5.4.0 (GCC) ) #488-Microsoft Mon Sep 01 13:43:00 PST 2020
@@ -221,9 +219,7 @@ func isWSL() bool {
221219
*************************************************************/
222220

223221
// IsWindows OS env
224-
func IsWindows() bool {
225-
return runtime.GOOS == "windows"
226-
}
222+
func IsWindows() bool { return runtime.GOOS == "windows" }
227223

228224
// IsConsole Determine whether w is one of stderr, stdout, stdin
229225
func IsConsole(w io.Writer) bool {
@@ -239,28 +235,23 @@ func IsConsole(w io.Writer) bool {
239235
}
240236

241237
// IsMSys msys(MINGW64) environment, does not necessarily support color
242-
func IsMSys() bool {
243-
// like "MSYSTEM=MINGW64"
244-
return len(os.Getenv("MSYSTEM")) > 0
245-
}
238+
func IsMSys() bool { /* like "MSYSTEM=MINGW64" */ return len(os.Getenv("MSYSTEM")) > 0 }
246239

247240
// IsSupportColor check current console is support color.
248241
//
249242
// NOTICE: The method will detect terminal info each times,
250243
//
251-
// if only want get current color level, please direct call SupportColor() or TermColorLevel()
252-
func IsSupportColor() bool {
253-
return IsSupport16Color()
254-
}
244+
// if only want to get current color level, please direct call SupportColor() or TermColorLevel()
245+
func IsSupportColor() bool { return IsSupport16Color() }
255246

256247
// IsSupport16Color check current console is support color.
257248
//
258249
// NOTICE: The method will detect terminal info each times,
259250
//
260-
// if only want get current color level, please direct call SupportColor() or TermColorLevel()
251+
// if only want to get current color level, please direct call SupportColor() or TermColorLevel()
261252
func IsSupport16Color() bool {
262253
level, _ := detectTermColorLevel()
263-
return level > terminfo.ColorLevelNone
254+
return level > LevelNo
264255
}
265256

266257
// IsSupport256Color render check
@@ -270,17 +261,15 @@ func IsSupport16Color() bool {
270261
// if only want to get current color level, please direct call SupportColor() or TermColorLevel()
271262
func IsSupport256Color() bool {
272263
level, _ := detectTermColorLevel()
273-
return level > terminfo.ColorLevelBasic
264+
return level > Level16
274265
}
275266

276267
// IsSupportRGBColor check. alias of the IsSupportTrueColor()
277268
//
278269
// NOTICE: The method will detect terminal info each times,
279270
//
280-
// if only want get current color level, please direct call SupportColor() or TermColorLevel()
281-
func IsSupportRGBColor() bool {
282-
return IsSupportTrueColor()
283-
}
271+
// if only want to get current color level, please direct call SupportColor() or TermColorLevel()
272+
func IsSupportRGBColor() bool { return IsSupportTrueColor() }
284273

285274
// IsSupportTrueColor render check.
286275
//
@@ -293,5 +282,5 @@ func IsSupportRGBColor() bool {
293282
// "COLORTERM=24bit"
294283
func IsSupportTrueColor() bool {
295284
level, _ := detectTermColorLevel()
296-
return level > terminfo.ColorLevelHundreds
285+
return level > Level256
297286
}

detect_nonwin.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,33 @@ package color
99
import (
1010
"strings"
1111
"syscall"
12-
13-
"github.com/xo/terminfo"
1412
)
1513

1614
// detect special term color support
1715
func detectSpecialTermColor(termVal string) (Level, bool) {
1816
if termVal == "" {
19-
return terminfo.ColorLevelNone, false
17+
return LevelNo, false
2018
}
2119

2220
debugf("terminfo check fail - fallback detect color by check TERM value")
2321

2422
// on TERM=screen:
2523
// - support 256, not support true-color. test on macOS
2624
if termVal == "screen" {
27-
return terminfo.ColorLevelHundreds, false
25+
return Level256, false
2826
}
2927

3028
if strings.Contains(termVal, "256color") {
31-
return terminfo.ColorLevelHundreds, false
29+
return Level256, false
3230
}
3331

3432
if strings.Contains(termVal, "xterm") {
35-
return terminfo.ColorLevelHundreds, false
33+
return Level256, false
3634
// return terminfo.ColorLevelBasic, false
3735
}
3836

39-
// return terminfo.ColorLevelNone, nil
40-
return terminfo.ColorLevelBasic, false
37+
// return LevelNo, nil
38+
return Level16, false
4139
}
4240

4341
// IsTerminal returns true if the given file descriptor is a terminal.

0 commit comments

Comments
 (0)