diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eefe3668..c985a9e67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Add support for the `NO_COLOR` environment variable. + ([#459](https://github.com/go-task/task/issues/459), [fatih/color#137](https://github.com/fatih/color/pull/137)). - Fix bug where sources were not considering the right directory in `--watch` mode ([#484](https://github.com/go-task/task/issues/484), [#485](https://github.com/go-task/task/pull/485)). diff --git a/cmd/task/task.go b/cmd/task/task.go index 4831c4de5..b481efe5f 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -89,7 +89,7 @@ func main() { pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution") pflag.StringVarP(&entrypoint, "taskfile", "t", "", `choose which Taskfile to run. Defaults to "Taskfile.yml"`) pflag.StringVarP(&output, "output", "o", "", "sets output style: [interleaved|group|prefixed]") - pflag.BoolVarP(&color, "color", "c", true, "colored output") + pflag.BoolVarP(&color, "color", "c", true, "colored output. Enabled by default. Set flag to false or use NO_COLOR=1 to disable") pflag.IntVarP(&concurrency, "concurrency", "C", 0, "limit number tasks to run concurrently") pflag.Parse() diff --git a/internal/logger/logger.go b/internal/logger/logger.go index ee1caec90..20c865f68 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -6,17 +6,16 @@ import ( "github.com/fatih/color" ) +type Color func() PrintFunc type PrintFunc func(io.Writer, string, ...interface{}) -var ( - Default PrintFunc = color.New(color.Reset).FprintfFunc() - Blue PrintFunc = color.New(color.FgBlue).FprintfFunc() - Green PrintFunc = color.New(color.FgGreen).FprintfFunc() - Cyan PrintFunc = color.New(color.FgCyan).FprintfFunc() - Yellow PrintFunc = color.New(color.FgYellow).FprintfFunc() - Magenta PrintFunc = color.New(color.FgMagenta).FprintfFunc() - Red PrintFunc = color.New(color.FgRed).FprintfFunc() -) +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() } // Logger is just a wrapper that prints stuff to STDOUT or STDERR, // with optional color. @@ -28,37 +27,39 @@ type Logger struct { } // Outf prints stuff to STDOUT. -func (l *Logger) Outf(print PrintFunc, s string, args ...interface{}) { +func (l *Logger) Outf(color Color, s string, args ...interface{}) { if len(args) == 0 { s, args = "%s", []interface{}{s} } if !l.Color { - print = Default + color = Default } + print := color() print(l.Stdout, s+"\n", args...) } // VerboseOutf prints stuff to STDOUT if verbose mode is enabled. -func (l *Logger) VerboseOutf(print PrintFunc, s string, args ...interface{}) { +func (l *Logger) VerboseOutf(color Color, s string, args ...interface{}) { if l.Verbose { - l.Outf(print, s, args...) + l.Outf(color, s, args...) } } // Errf prints stuff to STDERR. -func (l *Logger) Errf(print PrintFunc, s string, args ...interface{}) { +func (l *Logger) Errf(color Color, s string, args ...interface{}) { if len(args) == 0 { s, args = "%s", []interface{}{s} } if !l.Color { - print = Default + color = Default } + print := color() print(l.Stderr, s+"\n", args...) } // VerboseErrf prints stuff to STDERR if verbose mode is enabled. -func (l *Logger) VerboseErrf(print PrintFunc, s string, args ...interface{}) { +func (l *Logger) VerboseErrf(color Color, s string, args ...interface{}) { if l.Verbose { - l.Errf(print, s, args...) + l.Errf(color, s, args...) } } diff --git a/task_test.go b/task_test.go index 6eed315ef..89b466ced 100644 --- a/task_test.go +++ b/task_test.go @@ -17,6 +17,10 @@ import ( "github.com/go-task/task/v3/taskfile" ) +func init() { + _ = os.Setenv("NO_COLOR", "1") +} + // fileContentTest provides a basic reusable test-case for running a Taskfile // and inspect generated files. type fileContentTest struct {