diff --git a/cmd/root.go b/cmd/root.go index bf0a3d7c..5ce8c12a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,6 +24,7 @@ const ( var ( Verbose bool + NoColors bool rootPath string cfgFile string originConfig *viper.Viper @@ -52,10 +53,18 @@ func Execute() { } func init() { + rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") + rootCmd.PersistentFlags().BoolVar(&NoColors, "no-colors", false, "disable colored output") + + initAurora() cobra.OnInitialize(initConfig) - au = aurora.NewAurora(IsTTY()) + // re-init Aurora after config reading because `colors` can be specified in config + cobra.OnInitialize(initAurora) log.SetOutput(os.Stdout) - rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") +} + +func initAurora() { + au = aurora.NewAurora(EnableColors()) } func initConfig() { @@ -103,9 +112,17 @@ func check(e error) { } } -// IsTTY returns true if program is running with TTY -func IsTTY() bool { - return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) +// EnableColors shows is colors supported for current output or not. +// If `colors` explicitly specified in config, will return this value. +// Otherwise enabled for TTY and disabled for non-terminal output. +func EnableColors() bool { + if NoColors { + return false + } + if !viper.IsSet(colorsConfigKey) { + return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) + } + return viper.GetBool(colorsConfigKey) } // VerbosePrint print text if Verbose flag persist diff --git a/cmd/run.go b/cmd/run.go index 20a857e2..53e1cb5e 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -50,6 +50,7 @@ const ( skipConfigKey string = "skip" skipEmptyConfigKey string = "skip_empty" filesConfigKey string = "files" + colorsConfigKey string = "colors" parallelConfigKey string = "parallel" subFiles string = "{files}" subAllFiles string = "{all_files}"