mirror of
https://github.com/spf13/cobra
synced 2024-08-22 16:56:30 +00:00
fix light colors and pading when using a color
This commit is contained in:
parent
6291a093ca
commit
3e335655ae
18
command.go
18
command.go
|
@ -45,7 +45,11 @@ const (
|
||||||
ColorMagenta
|
ColorMagenta
|
||||||
ColorCyan
|
ColorCyan
|
||||||
ColorLightGray
|
ColorLightGray
|
||||||
ColorDarkGray
|
)
|
||||||
|
|
||||||
|
// This sequence starts at 90, so we reset iota
|
||||||
|
const (
|
||||||
|
ColorDarkGray = iota + 90
|
||||||
ColorLightRed
|
ColorLightRed
|
||||||
ColorLightGreen
|
ColorLightGreen
|
||||||
ColorLightYellow
|
ColorLightYellow
|
||||||
|
@ -494,10 +498,18 @@ var minNamePadding = 11
|
||||||
|
|
||||||
// NamePadding returns padding for the name.
|
// NamePadding returns padding for the name.
|
||||||
func (c *Command) NamePadding() int {
|
func (c *Command) NamePadding() int {
|
||||||
|
additionalPadding := c.additionalNamePadding()
|
||||||
if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
|
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.
|
// UsageTemplate returns usage template for the command.
|
||||||
|
|
|
@ -2001,6 +2001,9 @@ func TestColoredName(t *testing.T) {
|
||||||
if c.Name() != c.ColoredName() {
|
if c.Name() != c.ColoredName() {
|
||||||
t.Error("Name and ColoredName should give the same result")
|
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{
|
c = &Command{
|
||||||
Use: "cmd",
|
Use: "cmd",
|
||||||
Color: ColorRed,
|
Color: ColorRed,
|
||||||
|
@ -2015,4 +2018,7 @@ func TestColoredName(t *testing.T) {
|
||||||
if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" {
|
if c.ColoredName() != "\033[31m"+c.Name()+"\033[0m" {
|
||||||
t.Error("ColoredName should only add color to the name")
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue