Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 250114e

Browse files
authored
Merge pull request #742 from brettbuddin/no-color
Support NO_COLOR environment variable to disable color
2 parents baeb86a + 0cc41b4 commit 250114e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

utils/color.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"github.com/mgutz/ansi"
1010
)
1111

12+
var _isColorEnabled = true
1213
var _isStdoutTerminal = false
1314
var checkedTerminal = false
15+
var checkedNoColor = false
1416

1517
func isStdoutTerminal() bool {
1618
if !checkedTerminal {
@@ -33,13 +35,21 @@ func NewColorable(f *os.File) io.Writer {
3335
func makeColorFunc(color string) func(string) string {
3436
cf := ansi.ColorFunc(color)
3537
return func(arg string) string {
36-
if isStdoutTerminal() {
38+
if isColorEnabled() && isStdoutTerminal() {
3739
return cf(arg)
3840
}
3941
return arg
4042
}
4143
}
4244

45+
func isColorEnabled() bool {
46+
if !checkedNoColor {
47+
_isColorEnabled = os.Getenv("NO_COLOR") == ""
48+
checkedNoColor = true
49+
}
50+
return _isColorEnabled
51+
}
52+
4353
// Magenta outputs ANSI color if stdout is a tty
4454
var Magenta = makeColorFunc("magenta")
4555

utils/utils.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ func OpenInBrowser(url string) error {
2222
}
2323

2424
func RenderMarkdown(text string) (string, error) {
25-
return glamour.Render(text, "dark")
25+
style := "notty"
26+
if isColorEnabled() {
27+
style = "dark"
28+
}
29+
return glamour.Render(text, style)
2630
}
2731

2832
func Pluralize(num int, thing string) string {

0 commit comments

Comments
 (0)