Skip to content

Commit 2c7dd8a

Browse files
committed
Fix colorestream on white screens
1 parent 945cfd0 commit 2c7dd8a

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

tsuru/cmd/render.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
)
1212

1313
const (
14-
pattern = "\033[%d;%d;%dm%s\033[0m"
15-
bgFactor = 10
14+
pattern = "\033[%d;%d;%dm%s\033[0m"
15+
effectPattern = "\033[%dm%s\033[0m"
16+
bgFactor = 10
1617
)
1718

1819
var fontColors = map[string]int{
@@ -43,5 +44,8 @@ func Colorfy(msg string, fontcolor string, background string, effect string) str
4344
if DisableColors {
4445
return msg
4546
}
47+
if fontcolor == "" && background == "" && effect != "" {
48+
return fmt.Sprintf(effectPattern, fontEffects[effect], msg)
49+
}
4650
return fmt.Sprintf(pattern, fontEffects[effect], fontColors[fontcolor], fontColors[background]+bgFactor, msg)
4751
}

tsuru/cmd/v2/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/spf13/viper"
1313
"github.com/tsuru/go-tsuruclient/pkg/config"
1414
"github.com/tsuru/tablecli"
15+
"golang.org/x/term"
1516
)
1617

1718
var defaultViper = preSetupViper(nil)
@@ -29,8 +30,17 @@ func Pager() (pager string, found bool) {
2930
}
3031

3132
func ColorStream() bool {
32-
// TODO: detect by current terminal capabilities
33-
return !ColorDisabled() && defaultViper.GetBool("color-stream")
33+
if ColorDisabled() {
34+
return false
35+
}
36+
37+
def := term.IsTerminal(int(os.Stdout.Fd()))
38+
39+
key := "color-stream"
40+
if defaultViper.IsSet(key) {
41+
return defaultViper.GetBool(key)
42+
}
43+
return def
3444
}
3545

3646
// preSetupViper prepares viper for being used by NewProductionTsuruContext()

tsuru/formatter/coloredstream.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (w *coloredEncoderWriter) writeSectionLine(line string) {
8787
content := line[len(streamfmt.SectionPrefix) : len(line)-len(streamfmt.SectionSuffix)]
8888

8989
fmt.Fprint(w.Encoder, cmd.Colorfy(sectionIndicator, "blue", "", ""))
90-
fmt.Fprintf(w.Encoder, " %s \n", cmd.Colorfy(content, "white", "", "bold"))
90+
fmt.Fprintf(w.Encoder, " %s \n", cmd.Colorfy(content, "", "", "bold"))
9191
}
9292

9393
// writeActionLine writes an action line with green arrow, preserving indentation.
@@ -98,15 +98,15 @@ func (w *coloredEncoderWriter) writeActionLine(line string) {
9898

9999
fmt.Fprint(w.Encoder, strings.Repeat(" ", leadingSpaces))
100100
fmt.Fprint(w.Encoder, cmd.Colorfy(actionArrow, "green", "", "bold"))
101-
fmt.Fprintf(w.Encoder, " %s\n", cmd.Colorfy(content, "white", "", ""))
101+
fmt.Fprintf(w.Encoder, " %s\n", cmd.Colorfy(content, "", "", "reset"))
102102
}
103103

104104
// writeErrorLine writes an error line with red cross indicator.
105105
func (w *coloredEncoderWriter) writeErrorLine(line string) {
106106
content := line[len(streamfmt.ErrorPrefix) : len(line)-len(streamfmt.ErrorSuffix)]
107107

108108
fmt.Fprint(w.Encoder, cmd.Colorfy(errorCross+errorCross, "red", "", "bold"))
109-
fmt.Fprintf(w.Encoder, " %s\n", cmd.Colorfy(content, "red", "", ""))
109+
fmt.Fprintf(w.Encoder, " %s\n", cmd.Colorfy(content, "red", "", "reset"))
110110
}
111111

112112
func NewColoredStreamWriter(encoder io.Writer) io.Writer {

0 commit comments

Comments
 (0)