Skip to content

Commit

Permalink
up: fallback on terminfo load error, simple detect by TERM value string
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 7, 2021
1 parent 350627c commit bea68e7
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v2

- name: Use Go ${{ matrix.go_version }}
timeout-minutes: 3
timeout-minutes: 5
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go_version }}
Expand Down
18 changes: 1 addition & 17 deletions README.md
Expand Up @@ -45,22 +45,6 @@ Now, 256 colors and RGB colors have also been supported to work in Windows CMD a
go get github.com/gookit/color
```

### NOTICE

If you want print custom colors message on windows, should use `color.PrintX` instead of `fmt.PrintX`

```go
str := color.Red.Sprint("an colored message string")

// Color will not be output under Windows
fmt.Println(str)

// Color will be output under Windows
color.Println(str)
```

> `color.PrintX` is universal, you can use it directly instead of `fmt.PrintX`
## Quick start

```go
Expand Down Expand Up @@ -166,7 +150,7 @@ color.Bold.Println("bold message")
color.Yellow.Println("yellow message")
```

Run demo: `go run ./_examples/basiccolor.go`
Run demo: `go run ./_examples/color_16.go`

![basic-color](_examples/images/basic-color.png)

Expand Down
20 changes: 2 additions & 18 deletions README.zh-CN.md
Expand Up @@ -26,7 +26,7 @@ Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用
- 使用简单方便,无其他依赖
- 支持丰富的颜色输出, 16色(4bit),256色(8bit),RGB色彩(24bit, RGB)
- 16色(4bit)是最常用和支持最广的,支持Windows `cmd.exe`
-`v1.2.4`**256色(8bit),RGB色彩(24bit)均支持windows CMD和PowerShell终端**
-`v1.2.4`**256色(8bit),RGB色彩(24bit)均支持Windows CMD和PowerShell终端**
- 请查看 [this gist](https://gist.github.com/XVilka/8346728) 了解支持RGB色彩的终端
- 通用的API方法:`Print` `Printf` `Println` `Sprint` `Sprintf`
- 同时支持html标签式的颜色渲染. eg: `<green>message</>`
Expand All @@ -45,22 +45,6 @@ Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用
go get github.com/gookit/color
```

### 提示

如果要在Windows上打印颜色消息,应使用 `color.PrintX` 而不是 `fmt.PrintX`

```go
str := color.Red.Sprint("an colored message string")

// Windows 下将不会输出颜色
fmt.Println(str)

// Windows 也可以输出色彩
color.Println(str)
```

> `color.PrintX` 系列方法是通用的,您可以直接使用它们替代 `fmt.PrintX` 方法
## 快速开始

如下,引入当前包就可以快速的使用
Expand Down Expand Up @@ -168,7 +152,7 @@ color.Bold.Println("bold message")
color.Yellow.Println("yellow message")
```

> 运行demo: `go run ./_examples/basiccolor.go`
> 运行demo: `go run ./_examples/color_16.go`
![basic-color](_examples/images/basic-color.png)

Expand Down
58 changes: 58 additions & 0 deletions _examples/README.md
@@ -0,0 +1,58 @@
# Examples

there are some demo for use gookit/color.

## tests on docker

check ENV info:

```bash
# on manjaro
docker run --rm -ti golang:1.16 env | grep -i term
docker run --rm -ti jonathonf/manjaro bash
docker run --rm -ti -v ~/Workspace/godev/gookit/color:/var/gocolor jonathonf/manjaro bash

# on alpine linux
docker run --rm -ti alpine:3.12 env | grep -i term

# on alpine linux with golang
docker run --rm -ti golang:1.16-alpine env | grep -i term

# on ubuntu linux
docker run --rm -ti ubuntu:19.04 env | grep -i term

# on debian linux with golang
docker run --rm -ti -v ~/Workspace/godev/gookit/color:/var/gocolor golang:1.16 bash
```

run demo on docker with golang:

```bash
# on alpine linux with golang
docker run --rm -ti -v ~/Workspace/godev/gookit/color:/var/gocolor golang:1.16-alpine sh

# on debian linux with golang
docker run --rm -ti -v ~/Workspace/godev/gookit/color:/var/gocolor golang:1.16 bash
```

> TIPS: can set go proxy for fetch deps. eg: `export GOPROXY=https://goproxy.cn,direct`
run example demos:

```bash
/go # cd /var/gocolor/
/var/gocolor # export GOPROXY=https://goproxy.cn,direct
/var/gocolor # go run ./_examples/color_256.go
```

more examples:

```bash
COLOR_DEBUG_MODE=on go run ./_examples/envcheck.go

# examples:
go run ./_examples/color_16.go
go run ./_examples/color_256.go
go run ./_examples/color_tag.go
go run ./_examples/color_rgb.go
```
37 changes: 0 additions & 37 deletions _examples/basiccolor.go

This file was deleted.

9 changes: 6 additions & 3 deletions _examples/color_tag.go
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gookit/color"
)

// go run ./_examples/color_tag1.go
// go run ./_examples/color_tag.go
func main() {
i := 0
fmt.Println("Current env whether support color:", color.IsSupportColor())
Expand All @@ -19,13 +19,16 @@ func main() {
}

i++
color.Tag(tag).Print(tag+" tag")
color.Tag(tag).Printf("tag: %-14s", tag)
// taggedText := color.WrapTag("tag:" + tag, tag)
// color.Printf("%s", taggedText)

if i%5 == 0 {
fmt.Print("\n")
} else {
fmt.Print(" ")
}
}

fmt.Printf("\n\ntotal tags: %d\n", i)
fmt.Printf("\n\nBuilt-in Tags Total Number: %d\n", i)
}
32 changes: 0 additions & 32 deletions _examples/color_tag1.go

This file was deleted.

31 changes: 0 additions & 31 deletions _examples/colortag.go

This file was deleted.

2 changes: 1 addition & 1 deletion _examples/envcheck.go
Expand Up @@ -25,7 +25,7 @@ func main() {
}

termVal := os.Getenv("TERM")
fmt.Println("----------------TERM value is", termVal, "---------------")
fmt.Println("----------------TERM value is:", termVal, "---------------")
fmt.Println(
termVal[0:1],
strconv.FormatUint(uint64(termVal[0]), 16),
Expand Down
15 changes: 9 additions & 6 deletions tag.go → color_tag.go
Expand Up @@ -9,20 +9,23 @@ import (
// output colored text like use html tag. (not support windows cmd)
const (
// MatchExpr regex to match color tags
// Notice: golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+)
//
// Notice: golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+)
// MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>`
// 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>"
// 支持自定义颜色属性的tag "<fg=white;bg=blue;op=bold>content</>"
//
// allow custom attrs, eg: "<fg=white;bg=blue;op=bold>content</>"
// (?s:...) s - 让 "." 匹配换行
MatchExpr = `<([a-zA-Z_=,;]+)>(?s:(.*?))<\/>`
MatchExpr = `<([0-9a-zA-Z_=,;]+)>(?s:(.*?))<\/>`

// AttrExpr regex to match color attributes
AttrExpr = `(fg|bg|op)[\s]*=[\s]*([a-zA-Z,]+);?`
// AttrExpr regex to match custom color attributes
// eg: "<fg=white;bg=blue;op=bold>content</>"
AttrExpr = `(fg|bg|op)[\s]*=[\s]*([0-9a-zA-Z,]+);?`

// StripExpr regex used for removing color tags
// StripExpr = `<[\/]?[a-zA-Z=;]+>`
// 随着上面的做一些调整
StripExpr = `<[\/]?[a-zA-Z_=,;]*>`
StripExpr = `<[\/]?[0-9a-zA-Z_=,;]*>`
)

var (
Expand Down
File renamed without changes.
40 changes: 21 additions & 19 deletions detect_env.go
Expand Up @@ -44,10 +44,9 @@ func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
}

isWin := runtime.GOOS == "windows"
// TERM=screen
termVal := os.Getenv("TERM")

// on `screen` not support True-color
// on TERM=screen: not support true-color
if termVal != "screen" {
// On JetBrains Terminal
// - support true-color
Expand All @@ -60,21 +59,16 @@ func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
}

// level, err = terminfo.ColorLevelFromEnv()
level, err = detectColorLevelFromEnv(termVal)
// fmt.Println(level.String(), err)
// debugf("color level by terminfo.ColorLevelFromEnv(): %s", level)
level, err = detectColorLevelFromEnv(termVal, isWin)
if err != nil {
// if on windows OS
if isWin {
debugf("fallback1 check special term color on windows")
level, needVTP = detectSpecialTermColor()
}
return
}

// enable VTP as it has True Color support
debugf("color level by detectColorLevelFromEnv: %s", level.String())

// on Windows: enable VTP as it has True Color support
if level == terminfo.ColorLevelNone && isWin {
debugf("fallback2 check special term color on windows")
debugf("level none - fallback check term color on windows")
level, needVTP = detectSpecialTermColor()
}
return
Expand All @@ -85,10 +79,7 @@ func detectTermColorLevel() (level terminfo.ColorLevel, needVTP bool) {
//
// refer the terminfo.ColorLevelFromEnv()
// https://en.wikipedia.org/wiki/Terminfo
func detectColorLevelFromEnv(termVal string) (terminfo.ColorLevel, error) {
// on TERM=screen: not support true-color
// termVal := os.Getenv("TERM")

func detectColorLevelFromEnv(termVal string, isWin bool) (terminfo.ColorLevel, error) {
// check for overriding environment variables
colorTerm, termProg, forceColor := os.Getenv("COLORTERM"), os.Getenv("TERM_PROGRAM"), os.Getenv("FORCE_COLOR")
switch {
Expand Down Expand Up @@ -127,10 +118,21 @@ func detectColorLevelFromEnv(termVal string) (terminfo.ColorLevel, error) {
}

// otherwise determine from TERM's max_colors capability
if termVal != "" {
debugf("check term color level by load TERM=%s info file", termVal)
if !isWin && termVal != "" {
debugf("TERM=%s - check color level by load terminfo file", termVal)
ti, err := terminfo.Load(termVal)

// fallback: simple detect by TERM value string.
if err != nil {
debugf("terminfo.Load error - fallback detect color by check TERM value")
if strings.Contains(termVal, "256color") {
return terminfo.ColorLevelHundreds, nil
}
if strings.Contains(termVal, "xterm") {
return terminfo.ColorLevelHundreds, nil
// return terminfo.ColorLevelBasic, nil
}

saveInternalError(err)
return terminfo.ColorLevelNone, err
}
Expand All @@ -153,7 +155,7 @@ func detectColorLevelFromEnv(termVal string) (terminfo.ColorLevel, error) {
return terminfo.ColorLevelBasic, nil
}

// Not TERM env value. default return none level
// no TERM env value. default return none level
return terminfo.ColorLevelNone, nil
// return terminfo.ColorLevelBasic, nil
}
Expand Down

0 comments on commit bea68e7

Please sign in to comment.