mirror of
https://github.com/spf13/cobra
synced 2024-09-12 11:06:27 +00:00
add colors to CLI commands
This commit is contained in:
parent
7f8e83d936
commit
6291a093ca
37
command.go
37
command.go
|
@ -31,6 +31,30 @@ import (
|
||||||
// FParseErrWhitelist configures Flag parse errors to be ignored
|
// FParseErrWhitelist configures Flag parse errors to be ignored
|
||||||
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
||||||
|
|
||||||
|
// TerminalColor is a type used for the names of the different
|
||||||
|
// colors in the terminal
|
||||||
|
type TerminalColor int
|
||||||
|
|
||||||
|
// Colors represents the different colors one can use in the terminal
|
||||||
|
const (
|
||||||
|
ColorBlack TerminalColor = iota + 30
|
||||||
|
ColorRed
|
||||||
|
ColorGreen
|
||||||
|
ColorYellow
|
||||||
|
ColorBlue
|
||||||
|
ColorMagenta
|
||||||
|
ColorCyan
|
||||||
|
ColorLightGray
|
||||||
|
ColorDarkGray
|
||||||
|
ColorLightRed
|
||||||
|
ColorLightGreen
|
||||||
|
ColorLightYellow
|
||||||
|
ColorLightBlue
|
||||||
|
ColorLightMagenta
|
||||||
|
ColorLightCyan
|
||||||
|
ColorWhite
|
||||||
|
)
|
||||||
|
|
||||||
// Command is just that, a command for your application.
|
// Command is just that, a command for your application.
|
||||||
// E.g. 'go run ...' - 'run' is the command. Cobra requires
|
// E.g. 'go run ...' - 'run' is the command. Cobra requires
|
||||||
// you to define the usage and description as part of your command
|
// you to define the usage and description as part of your command
|
||||||
|
@ -47,6 +71,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
|
||||||
|
|
||||||
|
// Color represents the color to use to print the command in the terminal
|
||||||
|
Color TerminalColor
|
||||||
|
|
||||||
// Aliases is an array of aliases that can be used instead of the first word in Use.
|
// Aliases is an array of aliases that can be used instead of the first word in Use.
|
||||||
Aliases []string
|
Aliases []string
|
||||||
|
|
||||||
|
@ -493,7 +520,7 @@ Examples:
|
||||||
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
|
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
|
||||||
|
|
||||||
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
|
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
|
||||||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
|
{{rpad .ColoredName .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
|
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
|
||||||
|
@ -1293,6 +1320,14 @@ func (c *Command) Name() string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ColoredName returns the command's Name in the correct color if specified
|
||||||
|
func (c *Command) ColoredName() string {
|
||||||
|
if c.Color != 0 {
|
||||||
|
return fmt.Sprintf("\033[%dm%s\033[0m", c.Color, c.Name())
|
||||||
|
}
|
||||||
|
return c.Name()
|
||||||
|
}
|
||||||
|
|
||||||
// HasAlias determines if a given string is an alias of the command.
|
// HasAlias determines if a given string is an alias of the command.
|
||||||
func (c *Command) HasAlias(s string) bool {
|
func (c *Command) HasAlias(s string) bool {
|
||||||
for _, a := range c.Aliases {
|
for _, a := range c.Aliases {
|
||||||
|
|
|
@ -1989,3 +1989,30 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
|
||||||
}
|
}
|
||||||
checkStringContains(t, output, "unknown flag: --unknown")
|
checkStringContains(t, output, "unknown flag: --unknown")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestColoredName(t *testing.T) {
|
||||||
|
c := &Command{
|
||||||
|
Use: "cmd",
|
||||||
|
}
|
||||||
|
if c.Name() != "cmd" {
|
||||||
|
t.Error("Unexpected name with simple Command")
|
||||||
|
}
|
||||||
|
// If no color is specified, the ColoredName should equal the Name
|
||||||
|
if c.Name() != c.ColoredName() {
|
||||||
|
t.Error("Name and ColoredName should give the same result")
|
||||||
|
}
|
||||||
|
c = &Command{
|
||||||
|
Use: "cmd",
|
||||||
|
Color: ColorRed,
|
||||||
|
}
|
||||||
|
if c.Name() != "cmd" {
|
||||||
|
t.Errorf("Unexpected name with Colored Command: %s\n", c.Name())
|
||||||
|
}
|
||||||
|
// If a color is specified, the ColoredName and the Name should be different
|
||||||
|
if c.Name() == c.ColoredName() {
|
||||||
|
t.Error("Name and ColoredName should not give the same result")
|
||||||
|
}
|
||||||
|
if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" {
|
||||||
|
t.Error("ColoredName should only add color to the name")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue