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

with_output_color writes control codes to non-terminal streams #19682

Closed
stevengj opened this issue Dec 22, 2016 · 9 comments · Fixed by #25067
Closed

with_output_color writes control codes to non-terminal streams #19682

stevengj opened this issue Dec 22, 2016 · 9 comments · Fixed by #25067
Assignees
Labels
display and printing Aesthetics and correctness of printed representations of objects. help wanted Indicates that a maintainer wants help on an issue or pull request io Involving the I/O subsystem: libuv, read, write, etc.
Milestone

Comments

@stevengj
Copy link
Member

stevengj commented Dec 22, 2016

This doesn't seem right:

julia> buf = IOBuffer(); warn(buf, "foo"); String(take!(buf))
"\e[1m\e[33mWARNING: \e[39m\e[22m\e[33mfoo\e[39m\n"

This seems to be the behavior in 0.4 and 0.5 as well. However, something related to this has recently started breaking this test in Compat.jl. The most recent change to the relevant file base/util.jl seems to be #18628; @KristofferC, could that have affected the Compat test?

@stevengj stevengj added the io Involving the I/O subsystem: libuv, read, write, etc. label Dec 22, 2016
@KristofferC
Copy link
Member

KristofferC commented Dec 22, 2016

The problem here is that before that PR the output was something like:

\e[1m\e[33mWARNING: foo\e[0m\n

Previously, the whole string "WARNING: foo" was in one "piece" with no ANSI codes in between and the contains call thus passed. Now, the WARNING part is in bold but the rest isn't, so there is a terminating ANSI code after the "WARNING" part.

The only thing that determines or not whether Julia should use colors is the global variable have_color, i.e. it is independent of the stream.

@stevengj
Copy link
Member Author

Yeah, I just realized that Jupyter supports ANSI color codes even though it is not a TTY. Still, I wonder if this should be some kind of IOContext thing.

@KristofferC
Copy link
Member

Yeah, it most likely should. It has also been discussed in some places. Maybe most of it could be solved by moving to an IOContext with a :hascolor key and just let STDOUT and STDERR be IOContexts with that field set to true/false in the same way that Base.have_color is now determined.

@stevengj
Copy link
Member Author

You could also just define getindex(::TTY, key) = key == :hascolor ? have_color : throw(KeyError()) to avoid changing STDOUT and STDERR.

@StefanKarpinski
Copy link
Member

Can this be closed now that JuliaLang/Compat.jl#293 has been merged or is there still something to be done in Base Julia?

@stevengj
Copy link
Member Author

stevengj commented Jan 3, 2017

There is the still question of whether have_color should be an IO attribute rather than a global.

@JeffBezanson JeffBezanson added the display and printing Aesthetics and correctness of printed representations of objects. label Jan 3, 2017
@vtjnash vtjnash self-assigned this Oct 6, 2017
@vtjnash vtjnash modified the milestones: 1.1, 1.0 Oct 6, 2017
@JeffBezanson JeffBezanson added the triage This should be discussed on a triage call label Oct 11, 2017
@StefanKarpinski
Copy link
Member

This strikes me as a bugfix that would be eligible for fixing at any time, but we may as well fix it. We need IOContext(io, :color => [true|false] to fix this correctly.

@JeffBezanson
Copy link
Member

And ideally remove the have_color global.

@JeffBezanson
Copy link
Member

We can add a get method to TTY that returns true or false for :color based on the detection we do at startup.

@vtjnash vtjnash added help wanted Indicates that a maintainer wants help on an issue or pull request Hacktoberfest Good for Hacktoberfest participants labels Oct 12, 2017
@JeffBezanson JeffBezanson removed the triage This should be discussed on a triage call label Oct 12, 2017
@JeffBezanson JeffBezanson removed the Hacktoberfest Good for Hacktoberfest participants label Nov 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects. help wanted Indicates that a maintainer wants help on an issue or pull request io Involving the I/O subsystem: libuv, read, write, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants