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

redirect_* doesn't work with have_color #43586

Open
Roger-luo opened this issue Dec 29, 2021 · 2 comments
Open

redirect_* doesn't work with have_color #43586

Roger-luo opened this issue Dec 29, 2021 · 2 comments
Labels
io Involving the I/O subsystem: libuv, read, write, etc.

Comments

@Roger-luo
Copy link
Contributor

Roger-luo commented Dec 29, 2021

I'm not sure when exactly it stops working, consider the following code that redirects the colored printing to a Pipe buffer then read the String from it, I'm expecting to get the string with the corresponding ASCII color, but I only get the no-color printing.

old_stdout = stdout
stdout_buf = redirect_stdout()
stdout_reader = @async read(stdout_buf, String)
printstyled("aaa";color=:red,bold=true)
redirect_stdout(old_stdout)
close(stdout_buf)
print(fetch(stdout_reader))

I believe this is currently causing the color_output macro from Suppressor not working anymore (JuliaIO/Suppressor.jl#40, this is a separate issue due to the change of return value of redirect_stdout from Tuple{PipeEndpoint, PipeEndpoint} to PipeEndpoint). And it seems because the PipeEndpoint will return false for get(io, :color, false) inside with_output_color.

Currently the difference is for the default stdout, we have

get(::TTY, key::Symbol, default) = key === :color ? get_have_color() : default

but not for PipeEndpoint, I notice there has been discussion on this here #36671, thus I'm wondering what would be the best way to fix this? define a similar overload for PipeEndpoint seems not preferred since that could be a file? How do we allow users to turn on/off the color option when trying to dump the stdout to a string then?

@Roger-luo Roger-luo changed the title redirect_* doesn't work with have_color anymore redirect_* doesn't work with have_color Dec 29, 2021
@JeffBezanson
Copy link
Sponsor Member

The workaround would be to use redirect_stdout(IOContext(Pipe(), :color=>Base.get_have_color())).
We should possibly copy properties from the original stdout in redirect_stdout(), but I'm not sure...

related: #34721 #42649

@vtjnash
Copy link
Sponsor Member

vtjnash commented Jan 7, 2022

I would say this is intentional, and not intended that we clone values from old stdout (which you can do explicitly, as you show). But perhaps we should apply --color=yes to the new stdout/stderr, if the user set that command line flag?

@vtjnash vtjnash added the io Involving the I/O subsystem: libuv, read, write, etc. label Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
io Involving the I/O subsystem: libuv, read, write, etc.
Projects
None yet
Development

No branches or pull requests

3 participants