Improve Unicode support with grapheme clusters#396
Open
jquast wants to merge 1 commit intopeterbrittain:masterfrom
Open
Improve Unicode support with grapheme clusters#396jquast wants to merge 1 commit intopeterbrittain:masterfrom
jquast wants to merge 1 commit intopeterbrittain:masterfrom
Conversation
ac684a5 to
944238c
Compare
**Problem**: asciimatics text utilities wrongly split up grapheme clusters (emoji ZWJ sequences like 👨👩👧gional flags like 🇨🇦skin tone modifiers, combining characters), causing display corruption and incorrect width calculations. **Solution**: Integrate with wcwidth >= 0.5.0 by using: - https://wcwidth.readthedocs.io/en/latest/api.html#wcwidth.iter_graphemes for iteration in _enforce_width_ext(), _find_min_start(), _get_offset() - https://wcwidth.readthedocs.io/en/latest/api.html#wcwidth.wrap for grapheme-aware word wrapping in _split_text() - https://wcwidth.readthedocs.io/en/latest/api.html#wcwidth.ljust for line padding in SpeechBubble I notice that there is a choice to "ignore unicode" for performance improvement, but I can suggest that wcwidth has many "fast path" checks for pure-ascii strings to return len(string) and so on, along with lru_cache, the performance is negligble to always support unicode.
944238c to
f49700d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issues fixed by this PR
What does this implement/fix?
Problem: asciimatics text utilities wrongly split up grapheme clusters (emoji ZWJ sequences like 👨👩👧, regional flags like 🇨🇦, skin tone modifiers, combining characters, etc) -- this causes display corruption in the
SpeechBubble, or incorrect width calculations and padding in other places due to missing grapheme support.Solution: Integrate with
wcwidth>=0.5.0by using:_enforce_width_ext(),_find_min_start(),_get_offset()._split_text().**kwargsfor all of the additional built-in features supported by wcwidth.wrap() also, like requested by Add a way to let the user provide a custom text wrapping function #386Any other comments?
View benchmarks of wcwidth 0.2.14 to 0.5.0:
BASEHEADtest_center_asciitest_rjust_cjktest_center_cjktest_truncate_asciitest_center_ansitest_truncate_cjktest_length_cjktest_truncate_emoji_zwjtest_rjust_ansitest_truncate_ansitest_length_ansitest_length_asciitest_rjust_asciitest_ljust_ansitest_ljust_asciitest_ljust_cjktest_length_emoji_vs16try/exceptofimport wcwidth, with hasattr, making it an optional co-dependency, to help solve for Reducing number of dependencies #95 and Non-unicode version? Wcwidth is a resource hog. #155Let me know if you would like any such changes or additional PR's, happy to help.