Skip to content

Wrap lines by glyph so emoji are not torn in half - #447

Open
yu2971512385-ui wants to merge 1 commit into
antonmedv:masterfrom
yu2971512385-ui:fix/wrap-grapheme-clusters
Open

yu2971512385-ui wants to merge 1 commit into
antonmedv:masterfrom
yu2971512385-ui:fix/wrap-grapheme-clusters

Conversation

@yu2971512385-ui

Copy link
Copy Markdown

doWrap walks the value a rune at a time and sums runewidth.RuneWidth over 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 master they wrap three times:

value = "👨‍👩‍👧‍👦" x 6
  runewidth.StringWidth  = 12   <- what a terminal draws
  sum of RuneWidth       = 48   <- what doWrap counts

wrapped at termWidth=20 -> 3 lines
  0: width 7   "👨‍👩‍👧‍👦👨‍👩‍👧‍👦👨‍      <- ends on a dangling ZWJ, byte 58 is mid-cluster
  1: width 6   👩‍👧‍👦👨‍👩‍👧‍👦👨‍👩‍👧‍
  2: width 5   👦👨‍👩‍👧‍👦"

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.FirstGraphemeClusterInString instead of by rune, and measure each cluster with runewidth.StringWidth.

Boundaries come from uniseg and widths from runewidth on purpose: uniseg is already a direct dependency and decides where a glyph ends, while runewidth is the measure the rest of fx renders with, so this introduces no second width model. The key is measured with StringWidth for the same reason. go.mod is unchanged.

After:

termWidth=20 -> 1 line   (width 14, it fits)
termWidth=8  -> 2 lines  (widths 7, 7)
termWidth=6  -> 3 lines  (widths 5, 6, 3)
every break falls on a grapheme cluster boundary

Tests

internal/jsonx/wrap_test.go is 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/jsonx suite, and go test . -run Wrap, -run Chunk, -run Search on the root package, plus go vet and gofmt. 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.IdentWidth while continuation lines use n.Depth * 2, which differ whenever IdentWidth is 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

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>
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.

1 participant