diff --git a/README.md b/README.md index 72547bb..470bdd8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,13 @@ $ gotest -v github.com/rakyll/hey ``` ![go test output](https://i.imgur.com/udjWuZx.gif) -gotest comes with many colors! Configure the color of the output by setting the following env variable: +gotest comes with many colors! Configure the color of the output by setting the following environment variables: + +- `GOTEST_FAIL` +- `GOTEST_PASS` +- `GOTEST_SKIP` + +Alternatively you can use a single environment supporting a list of colors, in the order: fail, pass, and skip. ``` $ GOTEST_PALETTE="magenta,white" @@ -29,3 +35,13 @@ $ GOTEST_PALETTE="magenta,white" The output will have magenta for failed cases, white for success. Available colors: black, hiblack, red, hired, green, higreen, yellow, hiyellow, blue, hiblue, magenta, himagenta, cyan, hicyan, white, hiwhite. + +Do note that the individually set environment variables take precedence over the palette variable + +For the setting: + +``` +$ GOTEST_PASS="hiblue" GOTEST_PALETTE="magenta,white" +``` + +The output will have magenta for failed cases, hiblue for success. diff --git a/main.go b/main.go index 18a5bef..20aa55d 100644 --- a/main.go +++ b/main.go @@ -21,16 +21,21 @@ import ( "github.com/fatih/color" ) +const ( + paletteEnv = "GOTEST_PALETTE" + failEnv = "GOTEST_FAIL_COLOR" + passEnv = "GOTEST_PASS_COLOR" + skipEnv = "GOTEST_SKIP_COLOR" +) + var ( pass = color.FgGreen skip = color.FgYellow fail = color.FgHiRed ) -const paletteEnv = "GOTEST_PALETTE" - func main() { - setPalette() + parseEnvAndSetPalette() enableOnCI() os.Exit(gotest(os.Args[1:])) } @@ -144,20 +149,48 @@ func enableOnCI() { } } -func setPalette() { +func parseEnvAndSetPalette() { + parsePaletteEnv() + parseColorEnvs() +} + +func parsePaletteEnv() { v := os.Getenv(paletteEnv) - if v == "" { - return - } - vals := strings.Split(v, ",") - if len(vals) != 2 { - return - } - if c, ok := colors[vals[0]]; ok { - fail = c + + if v != "" { + vals := strings.Split(v, ",") + states := []color.Attribute{fail, pass, skip} + for i := range vals { + if c, ok := colors[vals[i]]; ok { + states[i] = color.Attribute(c) + } + } + + fail = states[0] + pass = states[1] + skip = states[2] } - if c, ok := colors[vals[1]]; ok { - pass = c +} + +func parseColorEnvs() { + + envArray := [3]string{skipEnv, failEnv, passEnv} + + for _, e := range envArray { + v := os.Getenv(e) + if v == "" { + continue + } + if c, ok := colors[v]; ok { + switch e { + case failEnv: + fail = c + case passEnv: + pass = c + case skipEnv: + skip = c + } + } } }