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

RFC: handle colors in Dict limit printing #37568

Merged
merged 3 commits into from
May 11, 2022
Merged

RFC: handle colors in Dict limit printing #37568

merged 3 commits into from
May 11, 2022

Conversation

KristofferC
Copy link
Sponsor 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

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
Sponsor 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
Sponsor 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
domain: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