From 60240d1b6479bb8bba394760ec0034b98d5c3f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20N=C3=B8rgaard?= Date: Wed, 14 Dec 2022 18:13:43 +0100 Subject: [PATCH] Override formatter colors from envvars (#1095) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Override environment formatter colors from envvars This allows a user to set environment variables `GINKGO_CLI_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 --- formatter/formatter.go | 33 +++++++++++++++++++++------------ formatter/formatter_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/formatter/formatter.go b/formatter/formatter.go index 43b16211d..da224eab6 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -50,6 +50,15 @@ 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{ @@ -57,18 +66,18 @@ func New(colorMode ColorMode) Formatter { "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{} diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index fab3f6234..b8db73164 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -1,6 +1,7 @@ package formatter_test import ( + "os" "strings" . "github.com/onsi/ginkgo/v2" @@ -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() { @@ -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() {