Skip to content

fix print_child_key with color#130

Open
Roger-luo wants to merge 1 commit into
JuliaCollections:masterfrom
Roger-luo:roger/color
Open

fix print_child_key with color#130
Roger-luo wants to merge 1 commit into
JuliaCollections:masterfrom
Roger-luo:roger/color

Conversation

@Roger-luo

Copy link
Copy Markdown

textwidth does not escape ASCII escape chars, which as a result gives wrong estimation of the text width, this might need to be fixed upstream (or use a more accurate function), but this PR is a workaround to this.

@nhz2

nhz2 commented Feb 10, 2023

Copy link
Copy Markdown
Contributor

I think this function will work to correct the textwidth result in case there are colors.

function colored_textwidth(x::AbstractString)
    i = firstindex(x)
    width = textwidth(x)
    while true
        found = findnext(r"\x1b\[[0-9;]*m", x, i)
        isnothing(found) && break
        width -= (length(found) - 1)
        i = found[end] + 1
    end
    width
end

@oscardssmith

Copy link
Copy Markdown
Member

This seems like it probably belongs in Base.

@Roger-luo

Roger-luo commented Feb 11, 2023

Copy link
Copy Markdown
Author

perhaps @nhz2 could submit this as a PR to Base then? Or I could do it if you are busy.

@nhz2

nhz2 commented Feb 13, 2023

Copy link
Copy Markdown
Contributor

Yes, feel free to submit the function as a PR to Base as I will be pretty busy for the next two weeks. Also, the changes in this PR look good to me, though ideally with a colored_textwidth function the print_child_key function would only need to be called once.

@Roger-luo

Copy link
Copy Markdown
Author

True, I think even with this fixed in Base, it will take a quite long time for AbstractTree get fixed, so maybe we should have this patch included in this package and add a @static if VERSION < v"1.9-" on it so it only triggers in lower Julia version.

@nhz2

nhz2 commented Mar 21, 2023

Copy link
Copy Markdown
Contributor

https://github.com/JuliaIO/WidthLimitedIO.jl Seems to have some ANSI escape code detection without regex. Maybe this function could be added to WidthLimitedIO.jl?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants