Skip to content

Commit ca2f2b1

Browse files
committed
Add support for CLICOLOR "standard"
The CLICOLOR_FORCE environment variable can now be used to force color output even when stdout is redirected. https://bixense.com/clicolors/
1 parent abf83c0 commit ca2f2b1

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

pkg/cmd/root/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
6161
GLAMOUR_STYLE: the style to use for rendering Markdown. See
6262
https://github.com/charmbracelet/glamour#styles
6363
64-
NO_COLOR: avoid printing ANSI escape sequences for color output.
64+
NO_COLOR: set to any value to avoid printing ANSI escape sequences for color output.
65+
66+
CLICOLOR: set to "0" to disable printing ANSI colors in output.
67+
68+
CLICOLOR_FORCE: set to a value other than "0" to keep ANSI colors in output
69+
even when the output is piped.
6570
`),
6671
},
6772
}

pkg/iostreams/iostreams.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ func System() *IOStreams {
106106
stdoutIsTTY := isTerminal(os.Stdout)
107107
stderrIsTTY := isTerminal(os.Stderr)
108108

109+
envNoColor := os.Getenv("NO_COLOR") != "" || os.Getenv("CLICOLOR") == "0"
110+
envForceColor := os.Getenv("CLICOLOR_FORCE") != "" && os.Getenv("CLICOLOR_FORCE") != "0"
111+
109112
io := &IOStreams{
110113
In: os.Stdin,
111114
Out: colorable.NewColorable(os.Stdout),
112115
ErrOut: colorable.NewColorable(os.Stderr),
113-
colorEnabled: os.Getenv("NO_COLOR") == "" && stdoutIsTTY,
116+
colorEnabled: envForceColor || (!envNoColor && stdoutIsTTY),
114117
}
115118

116119
// prevent duplicate isTerminal queries now that we know the answer

utils/color.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ func makeColorFunc(color string) func(string) string {
3939
}
4040

4141
func isColorEnabled() bool {
42-
if os.Getenv("NO_COLOR") != "" {
42+
if os.Getenv("CLICOLOR_FORCE") != "" && os.Getenv("CLICOLOR_FORCE") != "0" {
43+
return true
44+
}
45+
46+
if os.Getenv("NO_COLOR") != "" || os.Getenv("CLICOLOR") == "0" {
4347
return false
4448
}
4549

0 commit comments

Comments
 (0)