-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make with_output_color
respect have_color
#36671
Conversation
Why doesn't the io object have the color flag set? I don't think checking the global flag is correct here. |
On GitHub Actions it's not clear.
Fair enough, but at least checking whether |
--color=yes should just force the color flag to be set on stdout. It's supposed to be a force override for the tty detection. |
On GitHub Actions,
But ANSI escape sequences are rendered correctly as colours when forced. |
Right, so |
Sorry, I'm not sure what you suggest to do. |
Maybe do this Line 25 in 8320fcc
for |
But I think it's bad how |
Just wrap stdout in an iocontext when the io object is initialized and the color flag is set |
Doesn't it break |
julia> methods(redirect_stdout)
# 4 methods for generic function "redirect_stdout":
[1] redirect_stdout(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1148
[2] redirect_stdout() in Base at stream.jl:1154
[3] redirect_stdout(::Base.DevNull) in Base at stream.jl:1160
[4] redirect_stdout(f::Function, stream) in Base at stream.jl:1213
To edit a specific method, type the corresponding number into the REPL and press Ctrl+Q
julia> redirect_stdout(IOContext(stdout, :color => false))
ERROR: MethodError: no method matching redirect_stdout(::IOContext{Base.TTY}) |
Yeah, just fix those to make them work with an IOContext |
cf #30703 |
Yes, also with a pipe
|
I had a look at wrapping Line 31 in 7748761
IOContext is defined in Lines 222 to 230 in 7748761
get_have_color is defined in Lines 17 to 21 in 7748761
base/coreio.jl is the very first file included in base/Base.jl Line 36 in 7748761
stdout as suggested without reshuffling some definitions (which I assume isn't that straightforward to do).
As an alternative, what about this change in this PR: @@ -69,7 +69,7 @@ text_colors
function with_output_color(@nospecialize(f::Function), color::Union{Int, Symbol}, io::IO, args...; bold::Bool = false)
buf = IOBuffer()
- iscolor = get(io, :color, false)::Bool
+ iscolor = io === stdout ? get_have_color() : get(io, :color, false)::Bool
try f(IOContext(buf, io), args...)
finally
str = String(take!(buf))
Related to #30703, with this change:
|
Codecov Report
@@ Coverage Diff @@
## master #36671 +/- ##
==========================================
+ Coverage 86.14% 86.17% +0.02%
==========================================
Files 349 349
Lines 65855 65855
==========================================
+ Hits 56734 56753 +19
+ Misses 9121 9102 -19
Continue to review full report at Codecov.
|
I think the main problem is that multiple places check |
We maybe can find a good interface?
Not if
It doesn't look like julia> methods(redirect_stdout)
# 3 methods for generic function "redirect_stdout":
[1] redirect_stdout() in Base at stream.jl:1098
[2] redirect_stdout(handle::Union{IOStream, Base.LibuvStream}) in Base at stream.jl:1092
[3] redirect_stdout(f::Function, stream) in Base at stream.jl:1148 |
Good news; it does not need to be set that early. We can re-set |
Closing in favour of #36689 🙂 |
Since v1.6 has to be the most colourful release yet, I thought it would be nice to have logging use colours in CI, which is currently never the case in GitHub Actions for example.
I'm not sure this is the right way to check whether the function should use colours since it ignores the stream, but I believe
iscolor
should betrue
when--color=yes
is passed. I figured I'd open a questionable PR to get the right answer 🙂