diff --git a/CHANGELOG.md b/CHANGELOG.md index 9da36ec2..ae51c1b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +- Allow to override Task colors using environment variables: + `TASK_COLOR_RESET`, `TASK_COLOR_BLUE`, `TASK_COLOR_GREEN`, + `TASK_COLOR_CYAN`, `TASK_COLOR_YELLOW`, `TASK_COLOR_MAGENTA` + and `TASK_COLOR_RED` + ([#568](https://github.com/go-task/task/pull/568), [#792](https://github.com/go-task/task/pull/792)). - Fixed bug when using the `output: group` mode where STDOUT and STDERR were being print in separated blocks instead of in the right order ([#779](https://github.com/go-task/task/issues/779)). diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 20c865f6..2e65ef88 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -2,6 +2,8 @@ package logger import ( "io" + "os" + "strconv" "github.com/fatih/color" ) @@ -9,13 +11,36 @@ import ( type Color func() PrintFunc type PrintFunc func(io.Writer, string, ...interface{}) -func Default() PrintFunc { return color.New(color.Reset).FprintfFunc() } -func Blue() PrintFunc { return color.New(color.FgBlue).FprintfFunc() } -func Green() PrintFunc { return color.New(color.FgGreen).FprintfFunc() } -func Cyan() PrintFunc { return color.New(color.FgCyan).FprintfFunc() } -func Yellow() PrintFunc { return color.New(color.FgYellow).FprintfFunc() } -func Magenta() PrintFunc { return color.New(color.FgMagenta).FprintfFunc() } -func Red() PrintFunc { return color.New(color.FgRed).FprintfFunc() } +func Default() PrintFunc { + return color.New(envColor("TASK_COLOR_RESET", color.Reset)).FprintfFunc() +} +func Blue() PrintFunc { + return color.New(envColor("TASK_COLOR_BLUE", color.FgBlue)).FprintfFunc() +} +func Green() PrintFunc { + return color.New(envColor("TASK_COLOR_GREEN", color.FgGreen)).FprintfFunc() +} +func Cyan() PrintFunc { + return color.New(envColor("TASK_COLOR_CYAN", color.FgCyan)).FprintfFunc() +} +func Yellow() PrintFunc { + return color.New(envColor("TASK_COLOR_YELLOW", color.FgYellow)).FprintfFunc() +} +func Magenta() PrintFunc { + return color.New(envColor("TASK_COLOR_MAGENTA", color.FgMagenta)).FprintfFunc() +} +func Red() PrintFunc { + return color.New(envColor("TASK_COLOR_RED", color.FgRed)).FprintfFunc() +} + +func envColor(env string, defaultColor color.Attribute) color.Attribute { + override, err := strconv.Atoi(os.Getenv(env)) + if err == nil { + return color.Attribute(override) + + } + return defaultColor +} // Logger is just a wrapper that prints stuff to STDOUT or STDERR, // with optional color.