Skip to content

Measure terminal columns by glyph, not by char - #350

Open
yu2971512385-ui wants to merge 1 commit into
JamieMason:mainfrom
yu2971512385-ui:fix/measure-glyphs-not-chars
Open

yu2971512385-ui wants to merge 1 commit into
JamieMason:mainfrom
yu2971512385-ui:fix/measure-glyphs-not-chars

Conversation

@yu2971512385-ui

Copy link
Copy Markdown

update_row.rs imports UnicodeWidthChar and UnicodeWidthStr on one line and uses them for the two halves of the same arithmetic. visible_width asks the string. middle_truncate, format_group_header and both format_*_line budgets add up chars. The answers differ for anything a terminal draws as one glyph out of several chars:

glyph str.width() sum of char.width()
👨‍👩‍👧‍👦 family 2 8
👍🏽 skin tone 2 4
🏳️‍🌈 rainbow flag 2 3
1️⃣ keycap 2 1

@scope/pkg-👨‍👩‍👧‍👦-ui is 16 columns and counts as 22, so at max_width 20 it gets truncated when it fitted — and the cut lands inside the glyph:

shipped  "@scope/pk…‍👩‍👧‍👦-ui"    15 columns, tail opens on a dangling joiner
fixed    "@scope/pkg-👨‍👩‍👧‍👦-ui"   16 columns, untouched

Change

String measure everywhere. middle_truncate steps by grapheme cluster. unicode-segmentation is already in Cargo.lock, so nothing new is fetched.

Tests

Three added to middle_truncate_helper, over four glyphs at every budget from 2 to 20:

                              shipped   fixed
over budget                        11       0
cut a glyph in half                78       0
truncated a name that fitted        3       0
pinned expectations broken          0       0

What I ran

just test would not run in the sandbox I work in. I copied the rewritten middle_truncate verbatim into a scratch crate beside the shipped one, pinned to the unicode-width 0.2.2 and unicode-segmentation 1.13.3 your lock file carries, and ran both against every assertion above plus the ones update_row_test.rs already pins. Those figures are that program's output. CI is the real check.

First draft of the tests passed against the shipped code: I had put the glyph in the middle of a long name, where no cut from either end reached it. The strings now are ab{glyph}×6cd, where cuts land on a glyph.

Disclosure: written with AI assistance (Claude). Figures are real output.

🤖 Generated with Claude Code

update_row.rs imports UnicodeWidthChar and UnicodeWidthStr on one line
and uses them for the two halves of the same arithmetic. visible_width
asks the string; middle_truncate, format_group_header and the two
format_*_line budgets add up chars. The answers differ for anything a
terminal draws as one glyph out of several chars:

  family emoji   str 2, per char 8
  skin tone      str 2, per char 4
  rainbow flag   str 2, per char 3
  keycap         str 2, per char 1

So a dep name holding one is thought wider than it is and gets
truncated when it fitted, and middle_truncate's char-by-char head and
tail can stop inside a glyph, leaving a dangling joiner.

Use the string measure everywhere, and step middle_truncate by grapheme
cluster. unicode-segmentation is already in Cargo.lock.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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