fatih / color

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

Using color messes with formatting of tabwriter #106

Closed FUTC-Coding closed 5 years ago

FUTC-Coding commented 5 years ago

I am having a weird problem. When using a color It destroys the formatting of the text/tabwriter for some reason. When I don't use any colors the formatting is fine. This is the code I wrote for testing this:

func main() {
//first table
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)

fmt.Fprintln(w, "a\tb\tc\td")

fmt.Fprint(w, "abc" + "\t")
fmt.Fprint(w, "def" + "\t")
fmt.Fprint(w, "ghi" + "\t")
fmt.Fprintln(w, "jkl" + "\t")
w.Flush()
fmt.Println()

//second table
k := new(tabwriter.Writer)
k.Init(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)

fmt.Fprintln(k, "a\tb\tc\td")

yellow := color.New(color.FgYellow)
yellow.Fprint(k, "abc" + "\t")
fmt.Fprint(k, "def" + "\t")
fmt.Fprint(k, "ghi" + "\t")
fmt.Fprintln(k, "jkl" + "\t")
k.Flush()
}

and this is the output I am getting: screenshot 2018-09-30 at 16 33 07

is this a bug or am I doing something wrong?

FUTC-Coding commented 5 years ago

if I just use the ansi color directly and not through the library I have the same problem so it's actually not the libraries fault I guess.

const (
color = "\033[1;34m%s\033[0m"
)

func main() {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)
fmt.Fprintln(w, "a\tb\tc")

fmt.Fprintf(w, color, "abc\tdef\tghi")
w.Flush()
}

output: screenshot 2018-10-01 at 11 31 04

fatih commented 5 years ago

Hi,

Unfortunately, I'm archiving this project and will no longer maintaining it. For more information, please read my blog post: Taking an indefinite sabbatical from my projects.

Thanks for the feedback and contribution.