Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit fa37277

Browse files
authored
Respect NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment (#475)
1 parent 82a5fab commit fa37277

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

core/template.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"bytes"
5+
"os"
56
"sync"
67
"text/template"
78

@@ -23,6 +24,17 @@ var TemplateFuncsNoColor = map[string]interface{}{
2324
},
2425
}
2526

27+
// envColorDisabled returns if output colors are forbid by environment variables
28+
func envColorDisabled() bool {
29+
return os.Getenv("NO_COLOR") != "" || os.Getenv("CLICOLOR") == "0"
30+
}
31+
32+
// envColorForced returns if output colors are forced from environment variables
33+
func envColorForced() bool {
34+
val, ok := os.LookupEnv("CLICOLOR_FORCE")
35+
return ok && val != "0"
36+
}
37+
2638
// RunTemplate returns two formatted strings given a template and
2739
// the data it requires. The first string returned is generated for
2840
// user-facing output and may or may not contain ANSI escape codes
@@ -74,7 +86,8 @@ func GetTemplatePair(tmpl string) ([2]*template.Template, error) {
7486

7587
templatePair[1] = templateNoColor
7688

77-
if DisableColor {
89+
envColorHide := envColorDisabled() && !envColorForced()
90+
if DisableColor || envColorHide {
7891
templatePair[0] = templatePair[1]
7992
} else {
8093
templateWithColor, err := template.New("prompt").Funcs(TemplateFuncsWithColor).Parse(tmpl)

0 commit comments

Comments
 (0)