Parcourir la source

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

Alexey Alexandrov il y a 6 ans
Parent
révision
ef43755294
No account linked to committer's email address
1 fichiers modifiés avec 6 ajouts et 3 suppressions
  1. 6
    3
      pprof.go

+ 6
- 3
pprof.go Voir le fichier

@@ -20,6 +20,7 @@ import (
20 20
 	"fmt"
21 21
 	"os"
22 22
 	"strings"
23
+	"syscall"
23 24
 
24 25
 	"github.com/chzyer/readline"
25 26
 	"github.com/google/pprof/driver"
@@ -76,7 +77,10 @@ func (r *readlineUI) PrintErr(args ...interface{}) {
76 77
 	if !strings.HasSuffix(text, "\n") {
77 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 86
 // colorize the msg using ANSI color escapes.
@@ -90,8 +94,7 @@ func colorize(msg string) string {
90 94
 // IsTerminal returns whether the UI is known to be tied to an
91 95
 // interactive terminal (as opposed to being redirected to a file).
92 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 100
 // Start a browser on interactive mode.