Skip to content

Commit e6e3187

Browse files
authored
fix(message): respect no-color terminal settings (#64)
1 parent baedf27 commit e6e3187

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

internal/display/message/message.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"strings"
78

89
"golang.org/x/term"
910
)
@@ -72,5 +73,18 @@ func supportsColor(w io.Writer) bool {
7273
return false
7374
}
7475

75-
return term.IsTerminal(int(file.Fd()))
76+
return shouldUseColor(term.IsTerminal(int(file.Fd())), os.Getenv("TERM"), os.Getenv("NO_COLOR"))
77+
}
78+
79+
func shouldUseColor(isTerminal bool, termEnv, noColorEnv string) bool {
80+
if !isTerminal {
81+
return false
82+
}
83+
if strings.TrimSpace(noColorEnv) != "" {
84+
return false
85+
}
86+
if strings.EqualFold(strings.TrimSpace(termEnv), "dumb") {
87+
return false
88+
}
89+
return true
7690
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package message
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestShouldUseColor(t *testing.T) {
10+
t.Run("returns false for non-terminal writers", func(t *testing.T) {
11+
assert.False(t, shouldUseColor(false, "xterm-256color", ""))
12+
})
13+
14+
t.Run("returns false when no color is requested", func(t *testing.T) {
15+
assert.False(t, shouldUseColor(true, "xterm-256color", "1"))
16+
})
17+
18+
t.Run("returns false for dumb terminals", func(t *testing.T) {
19+
assert.False(t, shouldUseColor(true, "dumb", ""))
20+
})
21+
22+
t.Run("returns true for interactive color-capable terminals", func(t *testing.T) {
23+
assert.True(t, shouldUseColor(true, "xterm-256color", ""))
24+
})
25+
}

0 commit comments

Comments
 (0)