Skip to content

Commit

Permalink
Merge pull request #36 from orsinium/colors-flag
Browse files Browse the repository at this point in the history
Flag to force or disable colored output
  • Loading branch information
Abroskin Alexander authored Aug 1, 2019
2 parents 16d3faa + cc59b5a commit 1c35728
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (

var (
Verbose bool
NoColors bool
rootPath string
cfgFile string
originConfig *viper.Viper
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 1c35728

Please sign in to comment.