Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting color.SetWriter / UnsetWriter #124

Closed
itchyny opened this issue Dec 16, 2020 · 5 comments · Fixed by #182
Closed

Exporting color.SetWriter / UnsetWriter #124

itchyny opened this issue Dec 16, 2020 · 5 comments · Fixed by #182

Comments

@itchyny
Copy link

itchyny commented Dec 16, 2020

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
Copy link
Owner

fatih commented Dec 17, 2020

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

@zepatrik
Copy link

zepatrik commented Jan 8, 2021

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
Copy link
Author

itchyny commented Jan 9, 2021

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
Copy link

abiosoft commented Jun 1, 2021

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 added a commit that referenced this issue Jan 22, 2023
For some users performance is very important and hence having access to
the underlying writer functions allow them to use bytes buffer and avoid
string allocations.

closes #124
@fatih
Copy link
Owner

fatih commented Jan 22, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants