Skip to content

Commit

Permalink
fix: color detect error on win and WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 1, 2021
1 parent 49e9693 commit 9e0126d
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 234 deletions.
File renamed without changes.
15 changes: 10 additions & 5 deletions _examples/envcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
)

// go run ./_examples/envcheck.go
// COLOR_DEBUG_MODE=on go run ./_examples/envcheck.go
func main() {
fmt.Println("OS", runtime.GOOS)
fmt.Println("current OS:", runtime.GOOS)

fmt.Println("TermColorLevel:", color.TermColorLevel())
fmt.Println("IsSupportColor:", color.IsSupportColor())
fmt.Println("IsSupport256Color:", color.IsSupport256Color())
fmt.Println("IsSupportRGBColor:", color.IsSupportRGBColor())
fmt.Println("Terminal Color Level:", color.TermColorLevel())
fmt.Println("Support Basic Color:", color.SupportColor())
fmt.Println("Support 256 Color:", color.Support256Color())
fmt.Println("Support True Color:", color.SupportTrueColor())

if es := color.InnerErrs(); len(es) > 0 {
fmt.Println(es)
}

// dump.P(os.Environ())
// fmt.Println(os.Environ())
Expand Down
1 change: 0 additions & 1 deletion _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.12
require (
github.com/gookit/color v0.0.1
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 // indirect
)

replace github.com/gookit/color => ../
21 changes: 8 additions & 13 deletions _examples/go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.2.9 h1:1gwRqF/u6wZrxzn+thlROF7D4Toi9eVGUidZNAxAZHE=
github.com/gookit/color v1.2.9/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/gookit/goutil v0.3.5 h1:95hWrCXcz7wuwlvcHw7YyUbFH0fV15YM0WPioYMcZww=
github.com/gookit/goutil v0.3.5/go.mod h1:OHs5W5Xmfj4pCMXHnMxsDPrCc0SRbHLgJ2qs6wr5fxM=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
119 changes: 119 additions & 0 deletions _examples/ref/env-term-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# ENV grep term data

by run

```bash
env | grep -i term
```

## Windows

**git bash**

```text
TERM_PROGRAM=mintty
TERM_PROGRAM_VERSION=3.4.4
TERM=xterm
```

**git bash on windows terminal**

```text
TERM=xterm-256color
```

**git bash on vscode**

```text
COLORTERM=truecolor
TERM_PROGRAM=vscode
TERM_PROGRAM_VERSION=1.54.3
TERM=xterm-256color
```

**git bash on ConEmu**

```text
TERM=xterm-256color
```

```bash
$ env | grep -i ansi
ConEmuANSI=ON
ANSICON=104x32766 (104x26)
```

**git bash on JetBrains Terminal**

```text
TERMINAL_EMULATOR=JetBrains-JediTerm
TERM=xterm-256color
```

**cmd**

default not support color.

can by enable VTP for support True color

**cmd on windows terminal**

```text
TERM=xterm-256color
```

- support True color, but detect is `basic`

special ENV:

```text
WSLENV=WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID={574e775e-4f2a-5b96-ac1e-a2962a402336}
WT_SESSION=e68dfdf0-3f4e-4702-9b76-8768a6fbe784
```

**powerShell**

default not support color.

can by enable VTP for support True color

**powerShell on windows terminal**

```text
TERM=xterm-256color
```

- support True color, but detect is `basic`

special ENV:

```text
WSLENV=WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID={574e775e-4f2a-5b96-ac1e-a2962a402336}
WT_SESSION=e68dfdf0-3f4e-4702-9b76-8768a6fbe784
```

### WSL

> tests on the `debian WSL`
- print `runtime.GOOS` is `Linux`

```bash
$ env | grep -i term
TERM=xterm-256color
```

special ENV:

```bash
$ env | grep -i wsl
WSL_DISTRO_NAME=Debian
WSLENV=WT_SESSION::WT_PROFILE_ID
```

## Unix

contains `Mac OS X/Linux`

27 changes: 22 additions & 5 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ var (
Enable = true
// RenderTag render HTML tag on call color.Xprint, color.PrintX
RenderTag = true
// errors on windows render OR print to io.Writer
errors []error
// debug mode for development.
//
// set env:
// COLOR_DEBUG_MODE=on
// or:
// COLOR_DEBUG_MODE=on go run ./_examples/envcheck.go
debugMode = os.Getenv("COLOR_DEBUG_MODE")
// inner errors record on detect color level
innerErrs []error
// output the default io.Writer message print
output io.Writer = os.Stdout
// mark current env, It's like in `cmd.exe`
Expand All @@ -73,6 +80,16 @@ func SupportColor() bool {
return colorLevel > terminfo.ColorLevelNone
}

// Support256Color on the current ENV
func Support256Color() bool {
return colorLevel > terminfo.ColorLevelBasic
}

// SupportTrueColor on the current ENV
func SupportTrueColor() bool {
return colorLevel > terminfo.ColorLevelHundreds
}

/*************************************************************
* global settings
*************************************************************/
Expand Down Expand Up @@ -155,9 +172,9 @@ func IsLikeInCmd() bool {
return isLikeInCmd
}

// GetErrors info
func GetErrors() []error {
return errors
// InnerErrs info
func InnerErrs() []error {
return innerErrs
}

/*************************************************************
Expand Down
4 changes: 2 additions & 2 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestSet(t *testing.T) {
}
} else {
is.False(IsLikeInCmd())
is.Empty(GetErrors())
is.Empty(InnerErrs())
}

// set
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestRenderCode(t *testing.T) {
is.Equal("Text", str)
Enable = true

is.Empty(GetErrors())
is.Empty(InnerErrs())
}

func TestClearCode(t *testing.T) {
Expand Down
Loading

0 comments on commit 9e0126d

Please sign in to comment.