Skip to content

RFC: handle colors in Dict limit printing#37568

Merged
KristofferC merged 3 commits intomasterfrom
kc/color_stuff
May 11, 2022
Merged

RFC: handle colors in Dict limit printing#37568
KristofferC merged 3 commits intomasterfrom
kc/color_stuff

Conversation

@KristofferC
Copy link
Copy Markdown
Member

@KristofferC KristofferC commented Sep 14, 2020

When a dictionary prints values compactly, it cuts off the values after a certain number of characters and writes a couple of dots. In presence of colors, this doesn't work properly, because the computation of the width of the line will be wrong, and the printer might cut off the printing in the middle of the color sequence, corrupting the terminal color state. As an example, the following code

using Random

struct RainBowString
    s::String
end

function Base.show(io::IO, rbs::RainBowString)
    for s in rbs.s
        _, color = rand(Base.text_colors)
        print(io, color, s, "\e[0m")
    end
end


d = Dict([randstring(8) => [RainBowString(randstring(8)) for i in 1:10] for j in 1:5]...)

now shows in the REPL as

Screenshot-20200914144516-606x195

with this PR, it shows as:

Screenshot-20200914144438-784x148

The original reason for making this PR is that I made some struct print with color but then it printed as:

Screenshot-20200914144907-881x613

With this PR, it prints as:
Screenshot-20200914144854-917x617

Marking as RFC because I don't know if we want to deal with colors inside containers in the display system outside of MIME("text/plain").
Haven't added tests yet because they are annoying to write and want to see that people are ok with this first.

Fixes #44718

@KristofferC KristofferC added the display and printing Aesthetics and correctness of printed representations of objects. label Sep 14, 2020
base/show.jl Outdated
return (i_prev => c), (i, m_st)
end

function _truncate_at_width_or_chars(io::IO, str, width, chars="", truncmark="…")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure it is worthwhile to compile this function for every io type it might encounter (which is usually on the order of 5 with various IOContext/TTY/IOBuffer combinations)

Suggested change
function _truncate_at_width_or_chars(io::IO, str, width, chars="", truncmark="")
function _truncate_at_width_or_chars(ignore_ansi::Bool, str, width, chars="", truncmark="")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point. Updated.

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Showing Dicts with printstyled elements can color-bleed when truncated

2 participants