Browse Source

Only colorize error messages if stderr is a terminal. (#383)

Alexey Alexandrov 6 years ago
parent
commit
ef43755294
No account linked to committer's email address
1 changed files with 6 additions and 3 deletions
  1. 6
    3
      pprof.go

+ 6
- 3
pprof.go View File

20
 	"fmt"
20
 	"fmt"
21
 	"os"
21
 	"os"
22
 	"strings"
22
 	"strings"
23
+	"syscall"
23
 
24
 
24
 	"github.com/chzyer/readline"
25
 	"github.com/chzyer/readline"
25
 	"github.com/google/pprof/driver"
26
 	"github.com/google/pprof/driver"
76
 	if !strings.HasSuffix(text, "\n") {
77
 	if !strings.HasSuffix(text, "\n") {
77
 		text += "\n"
78
 		text += "\n"
78
 	}
79
 	}
79
-	fmt.Fprint(r.rl.Stderr(), colorize(text))
80
+	if readline.IsTerminal(int(syscall.Stderr)) {
81
+		text = colorize(text)
82
+	}
83
+	fmt.Fprint(r.rl.Stderr(), text)
80
 }
84
 }
81
 
85
 
82
 // colorize the msg using ANSI color escapes.
86
 // colorize the msg using ANSI color escapes.
90
 // IsTerminal returns whether the UI is known to be tied to an
94
 // IsTerminal returns whether the UI is known to be tied to an
91
 // interactive terminal (as opposed to being redirected to a file).
95
 // interactive terminal (as opposed to being redirected to a file).
92
 func (r *readlineUI) IsTerminal() bool {
96
 func (r *readlineUI) IsTerminal() bool {
93
-	const stdout = 1
94
-	return readline.IsTerminal(stdout)
97
+	return readline.IsTerminal(int(syscall.Stdout))
95
 }
98
 }
96
 
99
 
97
 // Start a browser on interactive mode.
100
 // Start a browser on interactive mode.