Skip to content

Commit 49fab7a

Browse files
authoredDec 16, 2022
Color aliases for custom color support (#1101)
This moves the custom color support away from the raw ASCI escapes and instead uses words for the standard 16 terminal colors or optionally 0-255 for the extended 256 color palette. Also add missing envvar unset in the test Co-authored-by: Benjamin Nørgaard <mail@blacksails.dev>
1 parent e7e3db7 commit 49fab7a

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed
 

‎formatter/formatter.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"regexp"
7+
"strconv"
78
"strings"
89
)
910

@@ -50,13 +51,35 @@ func NewWithNoColorBool(noColor bool) Formatter {
5051
}
5152

5253
func New(colorMode ColorMode) Formatter {
54+
colorAliases := map[string]int{
55+
"black": 0,
56+
"red": 1,
57+
"green": 2,
58+
"yellow": 3,
59+
"blue": 4,
60+
"magenta": 5,
61+
"cyan": 6,
62+
"white": 7,
63+
}
64+
for colorAlias, n := range colorAliases {
65+
colorAliases[fmt.Sprintf("bright-%s", colorAlias)] = n + 8
66+
}
67+
5368
getColor := func(color, defaultEscapeCode string) string {
5469
color = strings.ToUpper(strings.ReplaceAll(color, "-", "_"))
5570
envVar := fmt.Sprintf("GINKGO_CLI_COLOR_%s", color)
56-
if escapeCode := os.Getenv(envVar); escapeCode != "" {
57-
return escapeCode
71+
envVarColor := os.Getenv(envVar)
72+
if envVarColor == "" {
73+
return defaultEscapeCode
74+
}
75+
if colorCode, ok := colorAliases[envVarColor]; ok {
76+
return fmt.Sprintf("\x1b[38;5;%dm", colorCode)
77+
}
78+
colorCode, err := strconv.Atoi(envVarColor)
79+
if err != nil || colorCode < 0 || colorCode > 255 {
80+
return defaultEscapeCode
5881
}
59-
return defaultEscapeCode
82+
return fmt.Sprintf("\x1b[38;5;%dm", colorCode)
6083
}
6184

6285
f := Formatter{

‎formatter/formatter_test.go

+38-11
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,46 @@ var _ = Describe("Formatter", func() {
6262
})
6363
})
6464

65-
Context("with environment overrides", func() {
66-
BeforeEach(func() {
67-
os.Setenv("GINKGO_CLI_COLOR_RED", "\x1b[31m")
68-
})
65+
DescribeTable("with environment overrides",
66+
func(envVars map[string]string, input, expected string) {
67+
for envVar, value := range envVars {
68+
os.Setenv(envVar, value)
69+
}
70+
f := formatter.New(colorMode)
71+
Ω(f.F(input)).Should(Equal(expected))
72+
for envVar := range envVars {
73+
os.Unsetenv(envVar)
74+
}
75+
},
6976

70-
AfterEach(func() {
71-
os.Unsetenv("GINKGO_CLI_COLOR_RED")
72-
})
77+
Entry("uses default for too low codes", map[string]string{
78+
"GINKGO_CLI_COLOR_RED": "-1",
79+
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
7380

74-
It("uses the escape codes from the environment variables", func() {
75-
Ω(f.F("{{red}}hi there{{/}}")).Should(Equal("\x1b[31mhi there\x1b[0m"))
76-
})
77-
})
81+
Entry("uses default for too high codes", map[string]string{
82+
"GINKGO_CLI_COLOR_RED": "256",
83+
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
84+
85+
Entry("supports literal alias for 8bit color", map[string]string{
86+
"GINKGO_CLI_COLOR_RED": "red",
87+
}, "{{red}}hi there{{/}}", "\x1b[38;5;1mhi there\x1b[0m"),
88+
89+
Entry("supports number alias for 8bit color", map[string]string{
90+
"GINKGO_CLI_COLOR_RED": "1",
91+
}, "{{red}}hi there{{/}}", "\x1b[38;5;1mhi there\x1b[0m"),
92+
93+
Entry("supports 16bit colors (bright)", map[string]string{
94+
"GINKGO_CLI_COLOR_RED": "9",
95+
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
96+
97+
Entry("supports 16bit color literal aliases (bright)", map[string]string{
98+
"GINKGO_CLI_COLOR_RED": "bright-red",
99+
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),
100+
101+
Entry("supports extended 256 colors", map[string]string{
102+
"GINKGO_CLI_COLOR_RED": "16",
103+
}, "{{red}}hi there{{/}}", "\x1b[38;5;16mhi there\x1b[0m"),
104+
)
78105

79106
Describe("NewWithNoColorBool", func() {
80107
Context("when the noColor bool is true", func() {

0 commit comments

Comments
 (0)
Please sign in to comment.