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

Commit

Permalink
Respect NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeg committed Dec 8, 2022
1 parent 82a5fab commit fa37277
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/template.go
Expand Up @@ -2,6 +2,7 @@ package core

import (
"bytes"
"os"
"sync"
"text/template"

Expand All @@ -23,6 +24,17 @@ var TemplateFuncsNoColor = map[string]interface{}{
},
}

// envColorDisabled returns if output colors are forbid by environment variables
func envColorDisabled() bool {
return os.Getenv("NO_COLOR") != "" || os.Getenv("CLICOLOR") == "0"
}

// envColorForced returns if output colors are forced from environment variables
func envColorForced() bool {
val, ok := os.LookupEnv("CLICOLOR_FORCE")
return ok && val != "0"
}

// RunTemplate returns two formatted strings given a template and
// the data it requires. The first string returned is generated for
// user-facing output and may or may not contain ANSI escape codes
Expand Down Expand Up @@ -74,7 +86,8 @@ func GetTemplatePair(tmpl string) ([2]*template.Template, error) {

templatePair[1] = templateNoColor

if DisableColor {
envColorHide := envColorDisabled() && !envColorForced()
if DisableColor || envColorHide {
templatePair[0] = templatePair[1]
} else {
templateWithColor, err := template.New("prompt").Funcs(TemplateFuncsWithColor).Parse(tmpl)
Expand Down

0 comments on commit fa37277

Please sign in to comment.