Skip to content

Commit

Permalink
Override formatter colors from envvars (#1095)
Browse files Browse the repository at this point in the history
* Override environment formatter colors from envvars

This allows a user to set environment variables
`GINKGO_CLI_COLOR_<color>` with asci code overrides. This helps a lot
when the colorscheme that you are using doesn't play well with the
colors from the 256 color scheme which currently is hard coded.

Example `GINKGO_CLI_COLOR_RED=$'\x1b[31m' ginkgo` will use the red from
the 8bit colors configured in the terminal.

* make formatter suite support being run with overrides

Co-authored-by: Benjamin Nørgaard <[email protected]>
  • Loading branch information
blacksails and blacksails authored Dec 14, 2022
1 parent 3643823 commit 60240d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
33 changes: 21 additions & 12 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,34 @@ func NewWithNoColorBool(noColor bool) Formatter {
}

func New(colorMode ColorMode) Formatter {
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
}
return defaultEscapeCode
}

f := Formatter{
ColorMode: colorMode,
colors: map[string]string{
"/": "\x1b[0m",
"bold": "\x1b[1m",
"underline": "\x1b[4m",

"red": "\x1b[38;5;9m",
"orange": "\x1b[38;5;214m",
"coral": "\x1b[38;5;204m",
"magenta": "\x1b[38;5;13m",
"green": "\x1b[38;5;10m",
"dark-green": "\x1b[38;5;28m",
"yellow": "\x1b[38;5;11m",
"light-yellow": "\x1b[38;5;228m",
"cyan": "\x1b[38;5;14m",
"gray": "\x1b[38;5;243m",
"light-gray": "\x1b[38;5;246m",
"blue": "\x1b[38;5;12m",
"red": getColor("red", "\x1b[38;5;9m"),
"orange": getColor("orange", "\x1b[38;5;214m"),
"coral": getColor("coral", "\x1b[38;5;204m"),
"magenta": getColor("magenta", "\x1b[38;5;13m"),
"green": getColor("green", "\x1b[38;5;10m"),
"dark-green": getColor("dark-green", "\x1b[38;5;28m"),
"yellow": getColor("yellow", "\x1b[38;5;11m"),
"light-yellow": getColor("light-yellow", "\x1b[38;5;228m"),
"cyan": getColor("cyan", "\x1b[38;5;14m"),
"gray": getColor("gray", "\x1b[38;5;243m"),
"light-gray": getColor("light-gray", "\x1b[38;5;246m"),
"blue": getColor("blue", "\x1b[38;5;12m"),
},
}
colors := []string{}
Expand Down
26 changes: 26 additions & 0 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package formatter_test

import (
"os"
"strings"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -14,6 +15,17 @@ var _ = Describe("Formatter", func() {

BeforeEach(func() {
colorMode = formatter.ColorModeTerminal
os.Unsetenv("GINKGO_CLI_COLOR_RED")
os.Unsetenv("GINKGO_CLI_COLOR_ORANGE")
os.Unsetenv("GINKGO_CLI_COLOR_CORAL")
os.Unsetenv("GINKGO_CLI_COLOR_MAGENTA")
os.Unsetenv("GINKGO_CLI_COLOR_GREEN")
os.Unsetenv("GINKGO_CLI_COLOR_DARK_GREEN")
os.Unsetenv("GINKGO_CLI_COLOR_YELLOW")
os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_YELLOW")
os.Unsetenv("GINKGO_CLI_COLOR_CYAN")
os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_GRAY")
os.Unsetenv("GINKGO_CLI_COLOR_BLUE")
})

JustBeforeEach(func() {
Expand Down Expand Up @@ -50,6 +62,20 @@ var _ = Describe("Formatter", func() {
})
})

Context("with environment overrides", func() {
BeforeEach(func() {
os.Setenv("GINKGO_CLI_COLOR_RED", "\x1b[31m")
})

AfterEach(func() {
os.Unsetenv("GINKGO_CLI_COLOR_RED")
})

It("uses the escape codes from the environment variables", func() {
Ω(f.F("{{red}}hi there{{/}}")).Should(Equal("\x1b[31mhi there\x1b[0m"))
})
})

Describe("NewWithNoColorBool", func() {
Context("when the noColor bool is true", func() {
It("strips out color information", func() {
Expand Down

0 comments on commit 60240d1

Please sign in to comment.