fix light colors and pading when using a color

This commit is contained in:
JulesDT 2020-10-19 15:44:53 -06:00
parent 6291a093ca
commit 3e335655ae
2 changed files with 21 additions and 3 deletions

View file

@ -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.

View file

@ -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")
}
}