mirror of
https://github.com/spf13/cobra
synced 2024-11-25 07:07:15 +00:00
use the NO_COLOR env variable and a possible command flag
This commit is contained in:
parent
58d9bfdbcb
commit
4ac302ae86
1 changed files with 15 additions and 1 deletions
16
command.go
16
command.go
|
@ -75,6 +75,9 @@ type Command struct {
|
||||||
// Example: add [-F file | -D dir]... [-f format] profile
|
// Example: add [-F file | -D dir]... [-f format] profile
|
||||||
Use string
|
Use string
|
||||||
|
|
||||||
|
// DisableColors is a boolean used to disable the coloring in the command line
|
||||||
|
DisableColors bool
|
||||||
|
|
||||||
// Color represents the color to use to print the command in the terminal
|
// Color represents the color to use to print the command in the terminal
|
||||||
Color TerminalColor
|
Color TerminalColor
|
||||||
|
|
||||||
|
@ -1332,9 +1335,20 @@ func (c *Command) Name() string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isColoringEnabled will be queried to know whether or not we should enable
|
||||||
|
// the coloring on a command. This will usually be called on the Root command
|
||||||
|
// and applied for every command.
|
||||||
|
func (c *Command) isColoringEnabled() bool {
|
||||||
|
_, noColorEnv := os.LookupEnv("NO_COLOR")
|
||||||
|
if c.DisableColors || noColorEnv {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ColoredName returns the command's Name in the correct color if specified
|
// ColoredName returns the command's Name in the correct color if specified
|
||||||
func (c *Command) ColoredName() string {
|
func (c *Command) ColoredName() string {
|
||||||
if c.Color != 0 {
|
if c.Color != 0 && c.Root().isColoringEnabled() {
|
||||||
return fmt.Sprintf("\033[%dm%s\033[0m", c.Color, c.Name())
|
return fmt.Sprintf("\033[%dm%s\033[0m", c.Color, c.Name())
|
||||||
}
|
}
|
||||||
return c.Name()
|
return c.Name()
|
||||||
|
|
Loading…
Reference in a new issue