Skip to content

Commit 60240d1

Browse files
authoredDec 14, 2022
Override formatter colors from envvars (#1095)
* 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 <mail@blacksails.dev>
1 parent 3643823 commit 60240d1

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed
 

‎formatter/formatter.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,34 @@ func NewWithNoColorBool(noColor bool) Formatter {
5050
}
5151

5252
func New(colorMode ColorMode) Formatter {
53+
getColor := func(color, defaultEscapeCode string) string {
54+
color = strings.ToUpper(strings.ReplaceAll(color, "-", "_"))
55+
envVar := fmt.Sprintf("GINKGO_CLI_COLOR_%s", color)
56+
if escapeCode := os.Getenv(envVar); escapeCode != "" {
57+
return escapeCode
58+
}
59+
return defaultEscapeCode
60+
}
61+
5362
f := Formatter{
5463
ColorMode: colorMode,
5564
colors: map[string]string{
5665
"/": "\x1b[0m",
5766
"bold": "\x1b[1m",
5867
"underline": "\x1b[4m",
5968

60-
"red": "\x1b[38;5;9m",
61-
"orange": "\x1b[38;5;214m",
62-
"coral": "\x1b[38;5;204m",
63-
"magenta": "\x1b[38;5;13m",
64-
"green": "\x1b[38;5;10m",
65-
"dark-green": "\x1b[38;5;28m",
66-
"yellow": "\x1b[38;5;11m",
67-
"light-yellow": "\x1b[38;5;228m",
68-
"cyan": "\x1b[38;5;14m",
69-
"gray": "\x1b[38;5;243m",
70-
"light-gray": "\x1b[38;5;246m",
71-
"blue": "\x1b[38;5;12m",
69+
"red": getColor("red", "\x1b[38;5;9m"),
70+
"orange": getColor("orange", "\x1b[38;5;214m"),
71+
"coral": getColor("coral", "\x1b[38;5;204m"),
72+
"magenta": getColor("magenta", "\x1b[38;5;13m"),
73+
"green": getColor("green", "\x1b[38;5;10m"),
74+
"dark-green": getColor("dark-green", "\x1b[38;5;28m"),
75+
"yellow": getColor("yellow", "\x1b[38;5;11m"),
76+
"light-yellow": getColor("light-yellow", "\x1b[38;5;228m"),
77+
"cyan": getColor("cyan", "\x1b[38;5;14m"),
78+
"gray": getColor("gray", "\x1b[38;5;243m"),
79+
"light-gray": getColor("light-gray", "\x1b[38;5;246m"),
80+
"blue": getColor("blue", "\x1b[38;5;12m"),
7281
},
7382
}
7483
colors := []string{}

‎formatter/formatter_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package formatter_test
22

33
import (
4+
"os"
45
"strings"
56

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

1516
BeforeEach(func() {
1617
colorMode = formatter.ColorModeTerminal
18+
os.Unsetenv("GINKGO_CLI_COLOR_RED")
19+
os.Unsetenv("GINKGO_CLI_COLOR_ORANGE")
20+
os.Unsetenv("GINKGO_CLI_COLOR_CORAL")
21+
os.Unsetenv("GINKGO_CLI_COLOR_MAGENTA")
22+
os.Unsetenv("GINKGO_CLI_COLOR_GREEN")
23+
os.Unsetenv("GINKGO_CLI_COLOR_DARK_GREEN")
24+
os.Unsetenv("GINKGO_CLI_COLOR_YELLOW")
25+
os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_YELLOW")
26+
os.Unsetenv("GINKGO_CLI_COLOR_CYAN")
27+
os.Unsetenv("GINKGO_CLI_COLOR_LIGHT_GRAY")
28+
os.Unsetenv("GINKGO_CLI_COLOR_BLUE")
1729
})
1830

1931
JustBeforeEach(func() {
@@ -50,6 +62,20 @@ var _ = Describe("Formatter", func() {
5062
})
5163
})
5264

65+
Context("with environment overrides", func() {
66+
BeforeEach(func() {
67+
os.Setenv("GINKGO_CLI_COLOR_RED", "\x1b[31m")
68+
})
69+
70+
AfterEach(func() {
71+
os.Unsetenv("GINKGO_CLI_COLOR_RED")
72+
})
73+
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+
})
78+
5379
Describe("NewWithNoColorBool", func() {
5480
Context("when the noColor bool is true", func() {
5581
It("strips out color information", func() {

0 commit comments

Comments
 (0)
Please sign in to comment.