fatih/color

Exporting color.SetWriter / UnsetWriter

itchyny opened this issue · 5 comments

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

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

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.

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.

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

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.