Repository navigation
gitstatus: clip diff lines by glyph, not by rune - #330
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
clipToWidth measures the same line two ways. Its escape-sequence fallback asks lipgloss.Width, and padRight just below it does too, but the main path adds up runewidth.RuneWidth rune by rune. The answers differ for anything a terminal draws as one glyph out of several runes: a family emoji is two columns and counts as eight. So a diff line holding one is clipped against one measure and padded against another, and because the cut lands on a rune boundary rather than a glyph boundary it can leave a dangling zero-width joiner at the end of the line. Step a grapheme cluster at a time with uniseg, already a direct dependency, and measure each with ansi.StringWidth, which this file already imports and which is what lipgloss.Width calls underneath. The early exit on the width budget is kept. ASCII output is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This branch has not been deployed
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.
clipToWidthmeasures the same line two ways. Its escape-sequence fallback askslipgloss.Width, andpadRightright below it does too, but the main path adds uprunewidth.RuneWidthrune by rune. The answers differ for anything a terminal draws as one glyph out of several runes:lipgloss.WidthRuneWidthSo a diff line holding one is clipped against one measure and padded against the other, and because the cut lands on a rune boundary rather than a glyph boundary it can leave a dangling zero-width joiner at the end of the line.
Over
"+ " + glyph×8 + " end"at every width from 1 to 20:The change
Step a grapheme cluster at a time with
uniseg, already a direct dependency, and measure each withansi.StringWidth— which this file already imports, and which is whatlipgloss.Widthcalls underneath, so the clip and the pad now agree.The early exit on the width budget is kept, so a huge line is still not walked to the end.
Checks
ASCII is unaffected. Over
"","a","hello world","x"×200and"+ added a line of code"at every width from 0 to 30, both versions return byte-identical results.TestClipToWidth_KeepsGlyphsWholeis added todiff_renderer_test.go: the result stays a prefix of the line, and the cut lands on a grapheme boundary.What I ran, and what I did not
The sandbox I work in would not run
go test ./...here. I put both versions ofclipToWidthinto a scratch module pinned to thego-runewidthv0.0.23,charm.land/lipgloss/v2v2.0.3 andrivo/unisegv0.4.7 this repo locks, and ran both over the cases above; the tables are that program's output. CI is the real check.One thing worth knowing:
lipgloss.Widthandrunewidth.StringWidthdo not always agree — under these pinsrunewidthcalls a flag one column where lipgloss v2 calls it two. That is why the width here comes fromansi.StringWidthrather thanrunewidth.StringWidth: it keeps one measure across the clip, the fallback and the pad.Disclosure: written with AI assistance (Claude). The figures above are real output.
🤖 Generated with Claude Code