Skip to content

Commit

Permalink
fix cannot check color on tmux env
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 2, 2020
1 parent 55a2e61 commit 9b514c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
33 changes: 23 additions & 10 deletions envutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,22 @@ func HasShellEnv(shell string) bool {
return sysutil.HasShellEnv(shell)
}

// IsSupportColor check console is support color.
// supported: linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe
// not support: windows cmd, powerShell
// 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") {
// Support color:
// "TERM=xterm"
// "TERM=xterm-vt220"
// "TERM=xterm-256color"
// "TERM=screen-256color"
// Don't support color:
// "TERM=cygwin"
envTerm := os.Getenv("TERM")
if strings.Contains(envTerm, "xterm") || strings.Contains(envTerm, "screen") {
return true
}

Expand All @@ -67,8 +74,14 @@ func IsSupportColor() bool {
return false
}

// IsSupport256Color is support 256 color
// IsSupport256Color render
func IsSupport256Color() bool {
// "TERM=xterm-256color"
// "TERM=xterm-256color" "TERM=screen-256color"
return strings.Contains(os.Getenv("TERM"), "256color")
}

// IsSupportTrueColor render. IsSupportRGBColor
func IsSupportTrueColor() bool {
// "COLORTERM=truecolor"
return strings.Contains(os.Getenv("COLORTERM"), "truecolor")
}
10 changes: 10 additions & 0 deletions envutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func TestIsSupportColor(t *testing.T) {
is.True(envutil.IsSupportColor())
})

// "COLORTERM=truecolor"
testutil.MockEnvValue("COLORTERM", "truecolor", func(_ string) {
is.True(envutil.IsSupportTrueColor())
})

// TERM
testutil.MockEnvValue("TERM", "screen-256color", func(_ string) {
is.True(envutil.IsSupportColor())
})

is.NoError(os.Setenv("TERM", "xterm-vt220"))
is.True(envutil.IsSupportColor())
// revert
Expand Down

0 comments on commit 9b514c6

Please sign in to comment.