From e6e8f701a2b3832806649d56490115c447ded754 Mon Sep 17 00:00:00 2001 From: gabgodBB Date: Fri, 12 Jun 2020 14:26:38 -0300 Subject: [PATCH 1/4] Issue #930 - Removing color for output files --- utils/color.go | 4 ++++ utils/utils.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/utils/color.go b/utils/color.go index 4940fe26b..e88549667 100644 --- a/utils/color.go +++ b/utils/color.go @@ -53,6 +53,10 @@ func makeColorFunc(color string) func(string) string { } func isColorEnabled() bool { + if !isStdoutTerminal() { + return false + } + if !checkedNoColor { _isColorEnabled = os.Getenv("NO_COLOR") == "" checkedNoColor = true diff --git a/utils/utils.go b/utils/utils.go index c84860d9e..9441bc2bc 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -23,6 +23,10 @@ func OpenInBrowser(url string) error { func RenderMarkdown(text string) (string, error) { style := "notty" + if !isColorEnabled() { + return text, nil + } + if isColorEnabled() { style = "dark" } From 55be0d2a9ca7282c2c25e13d3bd3c67bc27347b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 22 Jun 2020 19:13:28 +0200 Subject: [PATCH 2/4] Tweak `isColorEnabled` --- utils/color.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/utils/color.go b/utils/color.go index e88549667..447765273 100644 --- a/utils/color.go +++ b/utils/color.go @@ -10,8 +10,8 @@ import ( ) var ( - _isColorEnabled bool = true - _isStdoutTerminal, checkedTerminal, checkedNoColor bool + _isColorEnabled bool = true + _isStdoutTerminal, checkedTerminal bool // Outputs ANSI color if stdout is a tty Magenta = makeColorFunc("magenta") @@ -45,7 +45,7 @@ func NewColorable(f *os.File) io.Writer { func makeColorFunc(color string) func(string) string { cf := ansi.ColorFunc(color) return func(arg string) string { - if isColorEnabled() && isStdoutTerminal() { + if isColorEnabled() { return cf(arg) } return arg @@ -53,13 +53,9 @@ func makeColorFunc(color string) func(string) string { } func isColorEnabled() bool { - if !isStdoutTerminal() { + if os.Getenv("NO_COLOR") != "" { return false } - if !checkedNoColor { - _isColorEnabled = os.Getenv("NO_COLOR") == "" - checkedNoColor = true - } - return _isColorEnabled + return isStdoutTerminal() } From 04b18ea8cb2933d096f5e6b38c559538c4e26fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 22 Jun 2020 19:32:06 +0200 Subject: [PATCH 3/4] :fire: unused var --- utils/color.go | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/color.go b/utils/color.go index 447765273..dd9a7d11c 100644 --- a/utils/color.go +++ b/utils/color.go @@ -10,7 +10,6 @@ import ( ) var ( - _isColorEnabled bool = true _isStdoutTerminal, checkedTerminal bool // Outputs ANSI color if stdout is a tty From ac7d5ecc44a7821265b5cc8a00a9eace6428da56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 22 Jun 2020 19:44:16 +0200 Subject: [PATCH 4/4] Ensure markdown still passed through Glamour in no-color mode --- utils/utils.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 9441bc2bc..c84860d9e 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -23,10 +23,6 @@ func OpenInBrowser(url string) error { func RenderMarkdown(text string) (string, error) { style := "notty" - if !isColorEnabled() { - return text, nil - } - if isColorEnabled() { style = "dark" }