-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add color
keyword argument
#1
Conversation
This adds `color` keyword argument to `iocapture()`. Currently, this argument does not work on Julia v1.5 or earlier, but to avoid version-based conditional branching on the user side, it does not throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay. The implementation and tests look great, thanks a lot!
-
I wonder if the default to
color
should befalse
though. My argument would be that the most common use case ofiocapture
is probably where you want to do some post-processing on the output (e.g. to regex out some information). At that point, you probably prefer not having the color escapes in the output. To counter the intuitiveness point, I think for me it would be slightly more intuitive if the captured output (by default) only contains the characters that I can see in the terminal.It would also make the default behavior consistent across Julia versions, with the ability to capture color, that must be explicitly enabled, only having an effect in 1.6+.
Do these arguments sway you, or do you think
true
is still the better option? -
Currently, if I read the tests right, we test slightly different code paths depending on settings (e.g. depending on
--color
or Julia version)? I wonder if we could have cases where we force-enable and force-disable color on stdout/err somehow, to make sure we test all the cases every time? It's not too important though, i.e. not worth investing too much time into this I think.
src/IOCapture.jl
Outdated
redirect_stderr(pipe.in) | ||
if VERSION >= v"1.6.0-DEV.481" # https://github.com/JuliaLang/julia/pull/36688 | ||
pe_stdout = IOContext(pipe.in, :color => get(stdout, :color, false) & color) | ||
pe_stderr = IOContext(pipe.in, :color => get(stdout, :color, false) & color) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be get(stderr, ...)
?
Agreed, if we can have consistent behavior across versions that would be less surprising. |
src/IOCapture.jl
Outdated
@@ -62,10 +66,17 @@ function iocapture(f; throwerrors::Union{Bool,Symbol}=true) | |||
# Redirect both the `stdout` and `stderr` streams to a single `Pipe` object. | |||
pipe = Pipe() | |||
Base.link_pipe!(pipe; reader_supports_async = true, writer_supports_async = true) | |||
redirect_stdout(pipe.in) | |||
redirect_stderr(pipe.in) | |||
if VERSION >= v"1.6.0-DEV.481" # https://github.com/JuliaLang/julia/pull/36688 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth sticking a @static
in here to avoid the runtime branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm skeptical that there is a runtime branch, but I'll stick the @static
to be clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, looks like it's smart enough these days:
julia> function f(x, y)
if VERSION >= v"1.4.0"
x + y
else
x - y
end
end
julia> @code_typed f(1, 2)
CodeInfo(
1 ─ %1 = Base.add_int(x, y)::Int64
└── return %1
) => Int64
No point adding @static
then.
This also fixes a typo (stdout --> stderr).
Thanks for the reviews! |
Co-authored-by: Morten Piibeleht <[email protected]>
Thanks again! Will tag this shortly, so that the Documenter PR could make use of this without having to jump through hoops. |
This is a change intended for Documenter's ANSI color support (cf. JuliaDocs/Documenter.jl#1441)
This adds
color
keyword argument toiocapture()
. Currently, this argument does not work on Julia v1.5 or earlier, but to avoid version-based conditional branching on the user side, it does not throw an error.Of course, it is a reasonable option to set
color
tofalse
by default. However, I chosetrue
for the following reasons.stdout
output matches the capture result.