fix: calculate display width with unicode-width to handle emoji#539
Open
cedricpinson wants to merge 1 commit into
Open
fix: calculate display width with unicode-width to handle emoji#539cedricpinson wants to merge 1 commit into
cedricpinson wants to merge 1 commit into
Conversation
String width was computed with grapheme cluster count, but a single emoji is one grapheme yet occupies two terminal columns. This made the layout underestimate width by one column per emoji, shifting/overlapping following spans. Use unicode-width's display width in layout_span, the text() helper and the overflow/truncation logic, and make truncation accumulate display width instead of grapheme count.
Author
|
Hi, Thanks |
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.
Problem
String display width was computed with grapheme cluster count (
graphemes(true).count()). A single emoji such as 🚀 is one grapheme cluster but occupies two terminal columns. As a result the layout underestimated the width by one column per emoji, shifting and overlapping the spans that follow on the same line.Fix
Use the actual terminal display width from the
unicode-widthcrate wherever a string's rendered width is needed:layout_spaninsrc/ui.rs(the central function used for rendering)text()helper insrc/ui/layout/mod.rsspan_width(overflow detection) insrc/screen/mod.rs, and the ellipsis truncation, which now accumulates display width instead of grapheme count so a wide character is never split and the…column is correctly reservedunicode-widthwas already a dev-dependency (used in test helpers); it is moved to[dependencies]since it is now needed at runtime.Tests
emoji_display_widthtest: a single emoji measures 2 columns and the following span starts at column 2.