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

fix print_child_key with color #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Roger-luo
Copy link

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
Copy link
Contributor

nhz2 commented Feb 10, 2023

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
Member

This seems like it probably belongs in Base.

@Roger-luo
Copy link
Author

Roger-luo commented Feb 11, 2023

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

@nhz2
Copy link
Contributor

nhz2 commented Feb 13, 2023

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
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
Copy link
Contributor

nhz2 commented Mar 21, 2023

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.

None yet

3 participants