Skip to content

Commit

Permalink
add colors to CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesDT committed Sep 22, 2020
1 parent 7f8e83d commit 6291a09
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
37 changes: 36 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ import (
// FParseErrWhitelist configures Flag parse errors to be ignored
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.
// E.g. 'go run ...' - 'run' is the command. Cobra requires
// you to define the usage and description as part of your command
Expand All @@ -47,6 +71,9 @@ type Command struct {
// Example: add [-F file | -D dir]... [-f format] profile
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 []string

Expand Down Expand Up @@ -493,7 +520,7 @@ Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
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:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Expand Down Expand Up @@ -1293,6 +1320,14 @@ func (c *Command) Name() string {
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.
func (c *Command) HasAlias(s string) bool {
for _, a := range c.Aliases {
Expand Down
27 changes: 27 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1989,3 +1989,30 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
}
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")
}
}

0 comments on commit 6291a09

Please sign in to comment.