Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color aliases for custom color support #1101

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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