fatih / color

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

[bug] color.Output has indent error when using tabwriter.Writer #175

Open fanybook opened 1 year ago

fanybook commented 1 year ago

Env: OS: windows 10 go: 1.17.2 bash: cmd powershell gitbash

package main

import (
    "fmt"
    "github.com/fatih/color"
    "io"
    "text/tabwriter"
)

func main() {
    tabOut := getTabOutWithWriter(color.Output)
    fmt.Fprint(tabOut, "\x1b[0;32m  demo\t\tStart the server\n\x1b[0m")
    fmt.Fprint(tabOut, "\x1b[0;32m  help\t\tHelp about any command\n\x1b[0m")
    fmt.Fprint(tabOut, "\x1b[0;32m  server:start\t\tStart the server\n\x1b[0m")
    tabOut.Flush()
}

func getTabOutWithWriter(writer io.Writer) *tabwriter.Writer {
    aTabOut := new(tabwriter.Writer)
    aTabOut.Init(writer, 0, 8, 1, '\t', 0)
    return aTabOut
}

Output:

  demo                          Start the server
  help                  Help about any command
  server:start          Start the server
montanaflynn commented 1 year ago

Just ran into the same thing, did you end up solving it?

I tried both color.RedString and color.New(color.FgRed).SprintFunc() with no luck...

Before using color:

Screenshot 2023-02-05 at 9 01 19 PM

After:

Screenshot 2023-02-05 at 9 00 42 PM

Edit: I think this is actually a text/tabwriter problem because the same issue when just using color codes directly:

        colorReset := "\033[0m"
        colorRed := "\033[31m"
        colorGreen := "\033[32m"
        colorYellow := "\033[33m"
rmsrob commented 1 year ago

for me this fix works

w.Init(os.Stdout, 0, 8, 4, ' ', 0)