diff --git a/command.go b/command.go index e6ba8b86..0d751617 100644 --- a/command.go +++ b/command.go @@ -45,7 +45,11 @@ const ( ColorMagenta ColorCyan ColorLightGray - ColorDarkGray +) + +// This sequence starts at 90, so we reset iota +const ( + ColorDarkGray = iota + 90 ColorLightRed ColorLightGreen ColorLightYellow @@ -494,10 +498,18 @@ var minNamePadding = 11 // NamePadding returns padding for the name. func (c *Command) NamePadding() int { + additionalPadding := c.additionalNamePadding() if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen { - return minNamePadding + return minNamePadding + additionalPadding } - return c.parent.commandsMaxNameLen + return c.parent.commandsMaxNameLen + additionalPadding +} + +func (c *Command) additionalNamePadding() int { + // additionalPadding is used to pad non visible characters + // This happens for example when using colors, where \033[31m isn't seen + // but still is counted towards the padding + return len(c.ColoredName()) - len(c.Name()) } // UsageTemplate returns usage template for the command. diff --git a/command_test.go b/command_test.go index a521b8b8..34fc5898 100644 --- a/command_test.go +++ b/command_test.go @@ -2001,6 +2001,9 @@ func TestColoredName(t *testing.T) { if c.Name() != c.ColoredName() { t.Error("Name and ColoredName should give the same result") } + if c.additionalNamePadding() != 0 { + t.Error("With no color, the additionalNamePadding should be 0") + } c = &Command{ Use: "cmd", Color: ColorRed, @@ -2015,4 +2018,7 @@ func TestColoredName(t *testing.T) { if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" { t.Error("ColoredName should only add color to the name") } + if c.additionalNamePadding() == 0 { + t.Error("With a color, the additionalNamePadding should be more than 0") + } }