Repository navigation
Wrap lines by glyph so emoji are not torn in half - #447
Open
yu2971512385-ui wants to merge 1 commit into
Open
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
doWrap advanced a rune at a time and summed runewidth.RuneWidth over the runes it passed. A grapheme cluster is several runes and one glyph, so a flag, an emoji carrying a skin tone or a family joined by zero-width joiners was counted once per rune: a family came to eight columns where a terminal draws two. Lines therefore broke long before they were full, and because the walk could stop between the runes of one cluster, a break could leave a trailing zero-width joiner on one line and orphan the rest of the glyph on the next. Advance by grapheme cluster using uniseg, already a direct dependency, and measure each cluster with runewidth.StringWidth, which is the measure the rest of fx renders with. The key is measured with StringWidth for the same reason. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
doWrapwalks the value a rune at a time and sumsrunewidth.RuneWidthover the runes it passes. A grapheme cluster is several runes and one glyph, so a flag, an emoji carrying a skin tone, or a family joined by zero-width joiners is counted once per rune — and because the walk can stop between the runes of one cluster, a break can leave a trailing zero-width joiner at the end of a line and orphan the rest of the glyph at the start of the next.Six family emoji occupy twelve columns, so with the quotes they fit a twenty column terminal. On
masterthey wrap three times:Three lines of 7, 6 and 5 columns in a terminal 20 wide, with two glyphs torn in half.
The change
Advance by grapheme cluster with
uniseg.FirstGraphemeClusterInStringinstead of by rune, and measure each cluster withrunewidth.StringWidth.Boundaries come from
unisegand widths fromrunewidthon purpose:unisegis already a direct dependency and decides where a glyph ends, whilerunewidthis the measure the rest of fx renders with, so this introduces no second width model. The key is measured withStringWidthfor the same reason.go.modis unchanged.After:
Tests
internal/jsonx/wrap_test.gois new and holds three cases:TestWrap_MeasuresGlyphsNotRunes— a value narrower than the terminal must not wrap. Fails on master (3 lines instead of 1).TestWrap_KeepsGlyphsWhole— ZWJ family, regional indicator flag, emoji with skin tone, each at four widths; every break must land on a cluster boundary and the lines must rejoin into the value unchanged. All three subtests fail on master, e.g.termWidth 4: the break after line 0 (byte 8) falls inside a glyph.TestWrap_LinesFitTheTerminal— no line wider than the terminal. This one passes on master too: over-counting only ever breaks early, never late. It is a guard against the opposite regression rather than a reproduction of this bug.What I ran, on this branch: the
internal/jsonxsuite, andgo test . -run Wrap,-run Chunk,-run Searchon the root package, plusgo vetandgofmt. All pass. I did not run the whole root suite locally, so CI is the real check on the TUI tests I did not select.One thing I noticed and deliberately left alone: the first line is indented
n.Depth * ident.IdentWidthwhile continuation lines usen.Depth * 2, which differ wheneverIdentWidthis not 2. That looks like a separate bug and did not belong in this change.Disclosure: written with AI assistance (Claude). The figures above are real output from this branch.
🤖 Generated with Claude Code