X Tutup
Skip to content

Commit 49ab3ec

Browse files
author
nate smith
committed
check for tty before creating colorables
1 parent 734959c commit 49ab3ec

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pkg/iostreams/iostreams.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,20 @@ func (s *IOStreams) TerminalWidth() int {
103103
}
104104

105105
func System() *IOStreams {
106-
return &IOStreams{
106+
stdoutIsTTY := isTerminal(os.Stdout)
107+
stderrIsTTY := isTerminal(os.Stderr)
108+
109+
io := &IOStreams{
107110
In: os.Stdin,
108111
Out: colorable.NewColorable(os.Stdout),
109112
ErrOut: colorable.NewColorable(os.Stderr),
110-
colorEnabled: os.Getenv("NO_COLOR") == "" && isTerminal(os.Stdout),
113+
colorEnabled: os.Getenv("NO_COLOR") == "" && stdoutIsTTY,
111114
}
115+
116+
// prevent duplicate isTerminal queries now that we know the answer
117+
io.SetStdoutTTY(stdoutIsTTY)
118+
io.SetStderrTTY(stderrIsTTY)
119+
return io
112120
}
113121

114122
func Test() (*IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {

0 commit comments

Comments
 (0)
X Tutup