fatih / color

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

Exporting color.SetWriter / UnsetWriter #124

Closed itchyny closed 1 year ago

itchyny commented 3 years ago

Now color.Set and color.Unset are exported so that we can color output to the standard output. Can you export color.SetWriter and color.UnsetWriter? This is pretty useful when we cannot change the code to Fprint style. Also using w.Write beats Fprint* functions in performance because it does not scan for formatting.

var myColor = color.New(color.FgYellow)

func F(w io.Writer) {
  myColor.SetWriter(w)
  defer myColor.UnsetWriter(w)
  someComplexFunction(w)
}
fatih commented 3 years ago

Hi @itchyny this looks like something we can do. Let me think about this a little bit. Thank you for your feedback.

zepatrik commented 3 years ago

I would be happy to implement this. What do you think about implementing io.Writer as well? It would enable one to wrap any writer with a color, and use it in the existing code without changing anything.

itchyny commented 3 years ago

I'm ok with only exporting the two functions. Currently I use go:linkname to call color.(*Color).setWriter. I'm not sure what lib users would expect when they nest coloring writers.

abiosoft commented 3 years ago

I'm in same boat as @itchyny. I am working with bytes and io.Writer and the print functions (as easy and convenient as they are) do not suit my use case.

fatih commented 1 year ago

This is fixed now, I'm going to release a new version soon with the fields being exported. Sorry for the late changes on this, 2021 was when I moved back from the US to Turkey, and I'm slowly going over my some of my OSS work.