Skip to content

Commit

Permalink
Color aliases for custom color support (#1101)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
blacksails and blacksails authored Dec 16, 2022
1 parent e7e3db7 commit 49fab7a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
29 changes: 26 additions & 3 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -50,13 +51,35 @@ func NewWithNoColorBool(noColor bool) Formatter {
}

func New(colorMode ColorMode) Formatter {
colorAliases := map[string]int{
"black": 0,
"red": 1,
"green": 2,
"yellow": 3,
"blue": 4,
"magenta": 5,
"cyan": 6,
"white": 7,
}
for colorAlias, n := range colorAliases {
colorAliases[fmt.Sprintf("bright-%s", colorAlias)] = n + 8
}

getColor := func(color, defaultEscapeCode string) string {
color = strings.ToUpper(strings.ReplaceAll(color, "-", "_"))
envVar := fmt.Sprintf("GINKGO_CLI_COLOR_%s", color)
if escapeCode := os.Getenv(envVar); escapeCode != "" {
return escapeCode
envVarColor := os.Getenv(envVar)
if envVarColor == "" {
return defaultEscapeCode
}
if colorCode, ok := colorAliases[envVarColor]; ok {
return fmt.Sprintf("\x1b[38;5;%dm", colorCode)
}
colorCode, err := strconv.Atoi(envVarColor)
if err != nil || colorCode < 0 || colorCode > 255 {
return defaultEscapeCode
}
return defaultEscapeCode
return fmt.Sprintf("\x1b[38;5;%dm", colorCode)
}

f := Formatter{
Expand Down
49 changes: 38 additions & 11 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,46 @@ var _ = Describe("Formatter", func() {
})
})

Context("with environment overrides", func() {
BeforeEach(func() {
os.Setenv("GINKGO_CLI_COLOR_RED", "\x1b[31m")
})
DescribeTable("with environment overrides",
func(envVars map[string]string, input, expected string) {
for envVar, value := range envVars {
os.Setenv(envVar, value)
}
f := formatter.New(colorMode)
Ω(f.F(input)).Should(Equal(expected))
for envVar := range envVars {
os.Unsetenv(envVar)
}
},

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

It("uses the escape codes from the environment variables", func() {
Ω(f.F("{{red}}hi there{{/}}")).Should(Equal("\x1b[31mhi there\x1b[0m"))
})
})
Entry("uses default for too high codes", map[string]string{
"GINKGO_CLI_COLOR_RED": "256",
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),

Entry("supports literal alias for 8bit color", map[string]string{
"GINKGO_CLI_COLOR_RED": "red",
}, "{{red}}hi there{{/}}", "\x1b[38;5;1mhi there\x1b[0m"),

Entry("supports number alias for 8bit color", map[string]string{
"GINKGO_CLI_COLOR_RED": "1",
}, "{{red}}hi there{{/}}", "\x1b[38;5;1mhi there\x1b[0m"),

Entry("supports 16bit colors (bright)", map[string]string{
"GINKGO_CLI_COLOR_RED": "9",
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),

Entry("supports 16bit color literal aliases (bright)", map[string]string{
"GINKGO_CLI_COLOR_RED": "bright-red",
}, "{{red}}hi there{{/}}", "\x1b[38;5;9mhi there\x1b[0m"),

Entry("supports extended 256 colors", map[string]string{
"GINKGO_CLI_COLOR_RED": "16",
}, "{{red}}hi there{{/}}", "\x1b[38;5;16mhi there\x1b[0m"),
)

Describe("NewWithNoColorBool", func() {
Context("when the noColor bool is true", func() {
Expand Down

0 comments on commit 49fab7a

Please sign in to comment.