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

IOColor (IOContext part 2?) #27430

Closed
wants to merge 15 commits into from
Closed

IOColor (IOContext part 2?) #27430

wants to merge 15 commits into from

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Jun 4, 2018

While working on the original IOContext PR, I had created a PoC of fully handling nested color. Here, I'm working on finally reviving and finishing that, building on all of the other new work towards better color handling.

Some color / format nesting in action:

julia> let buf = IOBuffer()
           Base.with_output_color(:red, IOContext(buf, :color => true)) do io; print(io, "red",
               Base.text_colors[:underline], " underline") # emit some formatting code directly too
           Base.with_output_color(:blue, io) do io; print(io, " blue") # nested blue
           Base.with_output_color(:bold, io) do io; print(io, " bold") # nested bold
           Base.with_output_color(:green, io) do io; print(io, " green\ngreen") # with a newline!
           end; print(io, " bold",
               Base.text_colors[:underline], " underline")
           end; print(io, " blue")
           end; print(io, " red")
           end
           String(take!(buf))
       end
"\e[31mred\e[4m underline\e[34m blue\e[1m bold\e[32m green\e[22m\e[39m\n\e[32m\e[1mgreen\e[34m bold\e[4m underline\e[22m blue\e[31m red\e[39m"

image

There's also some demo code in here of how that output could also be turned into html (ref #5239), which was probably the real purpose behind this work (although not actually finished or enabled here).

@vtjnash vtjnash added the io Involving the I/O subsystem: libuv, read, write, etc. label Jun 4, 2018
base/formatio.jl Outdated
pipe_writer(io::IOFormatBuffer) = io.buf
pipe_reader(io::IOFormatBuffer) = error("IOFormatBuffer not readable")

function with_output_color(f::Function, color::Union{Int, Symbol}, io::IO, args...; bold::Bool = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a good opportunity to rename with_output_color to something less ugly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some recollection that we'd already renamed it to styled or something like that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We changed print_with_color to printstyled, but left with_output_color unchanged, I forget why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe withstyle(f, io, color=..., bold=...)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps just styled(f, io, color=, bold=)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bold being some arbitrary special kwarg should probably be deprecated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why's that? In printstyled we have color and bold keyword arguments and that seems to work quite nicely.

@vtjnash
Copy link
Member Author

vtjnash commented Jun 9, 2018

Anyone seen this error before?
https://ci.appveyor.com/project/JuliaLang/julia/build/1.0.27586/job/9s6uqvwkwn1ebl8q#L3277

The global RNG seed was 0xd11701fbc3bda26f50d41cb8a2efb875.
3278
3279Error in testset SuiteSparse:
3280Error During Test at C:\projects\julia\julia-6ae2e7d268\share\julia\stdlib\v0.7\SuiteSparse\test\umfpack.jl:92
3281  Got exception LinearAlgebra.SingularException(0) outside of a @test
3282  LinearAlgebra.SingularException(0)
3283  Stacktrace:
3284   [1] #lu#1(::Bool, ::Function, ::SparseArrays.SparseMatrixCSC{Float64,Int32}) at C:\projects\julia\usr\share\julia\stdlib\v0.7\SuiteSparse\src\umfpack.jl:159
3285   [2] lu(::SparseArrays.SparseMatrixCSC{Float64,Int32}) at C:\projects\julia\usr\share\julia\stdlib\v0.7\SuiteSparse\src\umfpack.jl:152
3286   [3] macro expansion at C:\projects\julia\julia-6ae2e7d268\share\julia\stdlib\v0.7\SuiteSparse\test\umfpack.jl:76 [inlined]
3287   [4] macro expansion at C:\projects\julia\usr\share\julia\stdlib\v0.7\Test\src\Test.jl:1153 [inlined]
3288   [5] macro expansion at C:\projects\julia\julia-6ae2e7d268\share\julia\stdlib\v0.7\SuiteSparse\test\umfpack.jl:17 [inlined]
3289   [6] macro expansion at C:\projects\julia\usr\share\julia\stdlib\v0.7\Test\src\Test.jl:1080 [inlined]
3290   [7] top-level scope at C:\projects\julia\julia-6ae2e7d268\share\julia\stdlib\v0.7\SuiteSparse\test\umfpack.jl:7 [inlined]

And handle the attribute more consistently.

Also, ensure that `Terminals.supports_color` asks `tput` the right
question, and checks for getting back the required answer.
fully support nesting of ANSI color and format codes,
and provide a scaffold which could be used for supporting
more complex formatting (or other rich side information).
This is a port of `_truncate_at_width_or_chars` to any IO object.
While building complex output, it is sometimes useful to be able to
stage content in a separate buffer, then append it to the final output.
Support that, by deferring the final render when simply transfering content between IOFormatBuffers with write
@vtjnash vtjnash force-pushed the jn/iocolor2 branch 2 times, most recently from be2cfa7 to da14d3c Compare June 24, 2018 00:21
The call `with_format(io) do io; end` is very beneficial for
mono-morphizing IO code (enabling it to be precompiled)
and buffering it (potentially making it faster and reducing interleaving of output)

And try to improve some IO code to utilize it more / better
@KristofferC
Copy link
Member

I think this can be closed since my guess is that this will be incompatible with the styled strings stuff.

@vtjnash
Copy link
Member Author

vtjnash commented May 13, 2024

It is mostly all compatible, as they have similar designs, but a lot of work possibly to filter through here and pick up any fixes that didn't already get separately put in master

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

Successfully merging this pull request may close these issues.

4 participants