fatih / color

Color package for Go (golang)
https://pkg.go.dev/github.com/fatih/color
MIT License
7.36k stars 619 forks source link

width padding not working with SprintFunc #33

Closed pszxzsd closed 8 years ago

pszxzsd commented 8 years ago

e.g.:

cyan := color.New(color.FgCyan).SprintFunc()
fmt.Printf("%-5v%v\n", cyan("foo"), "bar")

should print "foo bar" (with 2 spaces) but prints "foobar"

mgeisler commented 8 years ago

That is not because of a problem with SprintFunc, but because cyan("foo") returns a string with 5 bytes. Or put differently: fmt.Printf does not take the color codes into account when formatting the string.

I suggest using SprintfFunc to format the string before the color codes are added:

cyan := color.New(color.FgCyan).SprintfFunc()
fmt.Println(cyan("%-5v", "foo") + "bar")
fatih commented 8 years ago

Thanks @mgeisler for the clarification. Closing as this is the indented way. There is not much we can do. @pszxzsd I recommend to use it like @mgeisler recommended. Thanks.