Skip to content

Commit 24abe97

Browse files
Merge pull request #270 from cfunkhouser/respect-colorization-on-show-output
Do not output colorized config when environment does not support it
2 parents 796c879 + e0d9d4b commit 24abe97

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

cli/apiconfig.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ func (a APIConfig) Save() error {
6868

6969
// Return colorized string of configuration in JSON or YAML
7070
func (a APIConfig) GetPrettyDisplay(outFormat string) ([]byte, error) {
71-
var prettyConfig []byte
72-
7371
// marshal
7472
if outFormat == "auto" {
7573
outFormat = "json"
@@ -79,13 +77,17 @@ func (a APIConfig) GetPrettyDisplay(outFormat string) ([]byte, error) {
7977
return nil, errors.New("unable to render configuration")
8078
}
8179

80+
if !useColor {
81+
return marshalled, nil
82+
}
83+
8284
// colorize
83-
prettyConfig, err = Highlight(outFormat, marshalled)
85+
marshalled, err = Highlight(outFormat, marshalled)
8486
if err != nil {
8587
return nil, errors.New("unable to colorize output")
8688
}
8789

88-
return prettyConfig, nil
90+
return marshalled, nil
8991
}
9092

9193
type apiConfigs map[string]*APIConfig

cli/apiconfig_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ func TestAPIContentTypes(t *testing.T) {
1515
}
1616

1717
func TestAPIShow(t *testing.T) {
18-
reset(false)
19-
configs["test"] = &APIConfig{
20-
name: "test",
21-
Base: "https://api.example.com",
18+
for tn, tc := range map[string]struct {
19+
color bool
20+
want string
21+
}{
22+
"no color": {false, "{\n \"base\": \"https://api.example.com\"\n}\n"},
23+
"color": {true, "\x1b[38;5;247m{\x1b[0m\n \x1b[38;5;74m\"base\"\x1b[0m\x1b[38;5;247m:\x1b[0m \x1b[38;5;150m\"https://api.example.com\"\x1b[0m\n\x1b[38;5;247m}\x1b[0m\n"},
24+
} {
25+
t.Run(tn, func(t *testing.T) {
26+
reset(tc.color)
27+
configs["test"] = &APIConfig{
28+
name: "test",
29+
Base: "https://api.example.com",
30+
}
31+
captured := runNoReset("api show test")
32+
assert.Equal(t, captured, tc.want)
33+
})
2234
}
23-
captured := runNoReset("api show test")
24-
assert.Equal(t, captured, "\x1b[38;5;247m{\x1b[0m\n \x1b[38;5;74m\"base\"\x1b[0m\x1b[38;5;247m:\x1b[0m \x1b[38;5;150m\"https://api.example.com\"\x1b[0m\n\x1b[38;5;247m}\x1b[0m\n")
2535
}
2636

2737
func TestAPIClearCache(t *testing.T) {

0 commit comments

Comments
 (0)