Repository navigation
fix(terminal): lay out emoji sequences in libghostty as xterm does - #2948
Conversation
libghostty gave a ZWJ family, skin tone, flag, keycap or emoji-presentation selector one cell per code point, so text after it was drawn columns to the right of xterm's cells (cursor, selection, links). Turn grapheme cluster mode 2027 on as libghostty's reset default and keep it on when a program turns it off, since xterm ignores that mode. Fixes #2934
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 4 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Follow a CSI ? Pm l across writes with a small linear state machine instead of rescanning digits (quadratic on numeric output, blind to a sequence split between writes), and read a grapheme longer than the 120-byte buffer with the size libghostty asks for.
…dr and xterm do Grapheme clustering joined a spacing mark (a Devanagari vowel sign, Thai SARA AM) to the letter before it, so at the end of a line or when overwritten the layer drew it one column off xterm's grid. Print each spacing mark with mode 2027 off. Also keep scanning a mode 2027 off through C0 controls inside the sequence and across reset(), as libghostty and xterm parse it.
Mode 2027 on everywhere also clustered what herdr and xterm keep apart: Bengali and Devanagari vowel signs, Indic conjuncts, Thai SARA AM, a text-presentation selector. Keep 2027 off, as libghostty lays out like xterm there, and print only the code points that join an emoji (ZWJ and the pictograph after it, VS16, skin tones, keycap, tags, regional indicators) with it on. A program's own 2027 on (h, or r) is turned off again, through C0 controls or DEL inside the sequence, across writes and across reset().
xterm starts a new cell after an escape sequence, so a skin tone, flag half or ZWJ after a style change, OSC title or other escape is not joined. Skip a non-private CSI to its final byte and an OSC, DCS or APC to its BEL or ST, so their parameters do not count as printed text.
Review rounds (omo + Codex Terra xhigh)The fix was reworked after review. Turning mode 2027 on everywhere (the first version) also clustered what herdr and xterm keep apart: Bengali and Devanagari vowel signs, Indic conjuncts, and Thai SARA AM. Now 2027 stays off, and only the code points that join an emoji are printed with it on. Fixed along the way, each covered by a test in
Codex's last pass at
|
|
Local merge gate (owner-approved): |
…ll, as herdr does (#2972) Fixes #2954 ## What was wrong An emoji followed by the text-presentation selector U+FE0E (`⚽︎`) was two columns wide in the browser's xterm (`src/lib/terminalWidths.ts`) and, after #2948, in libghostty too. herdr's own grid gives it one column. So the text, cursor, selection and links after it sat one column to the right of where herdr put them. This happened with or without Settings > Terminal > Draw with libghostty. Measured with a DSR (`ESC [ 6 n`) after `printf 'A<seq>X'` in an isolated session, column of the cursor (1-based): | sequence | herdr 0.9.5 | herdr-core 0.1.17 (desktop app) | herdr-core 0.1.14 | |---|---|---|---| | `A⚽︎X` (U+26BD U+FE0E) | 4 | 4 | 5 | | `A⚽X` | 5 | 5 | 5 | | `A😀︎X` (no text form) | 5 | 5 | 5 | | `A🈚︎X` (U+1F21A U+FE0E) | 4 | 4 | n/a | | `A⚽︎️X` (FE0E then FE0F) | 4 | 4 | n/a | | `A👍🏽︎X` (FE0E after a skin tone) | 5 | 5 | n/a | | `A⌚︎⌚X` (ZWJ sequence after FE0E) | 5 | 5 | n/a | | `A👍︎🏽X` (skin tone after FE0E) | 6 | 6 | n/a | herdr and herdr-core 0.1.17 agree. They narrow an emoji to one column only when it has a text form (it is listed in Unicode's `emoji-variation-sequences.txt`) and U+FE0E comes right after it. A selector after that does not widen it again. A pane that fills its last column wraps the emoji first and narrows it on the next row. Both do this. ## Fix - `src/lib/terminalWidths.ts`: the width provider gives U+FE0E one column when it joins one of the 152 two-column emoji that have a text form, and keeps the cluster at one column through any selector after it. A ZWJ ends the narrowing, and a skin tone after it starts a new cell. xterm asks for each character in order and hands back the answer for the one before. So the provider remembers which answer belonged to a lone text-form emoji. - `patches/@XTerm%[email protected]`: xterm's join only ever widened a cell. When a joining character narrows the cell, it now gives the freed column back as an empty cell and moves the cursor back. - `src/lib/ghostty/vt.ts`: U+FE0E joins the emoji before it with mode 2027 on, like the other selectors. libghostty then gives the same widths as herdr for every row in the table. - `scripts/build-xterm.ts`: a `patchedTerminal()` helper. The width tests now run against the patched xterm the app ships. The package's own build does not have the patch. ## Validation - `bun test src/lib/terminalWidths.test.ts src/lib/ghostty/vt.test.ts scripts/build-xterm.test.ts scripts/xterm-composition.test.ts`: 84 pass. - Review round: Codex found that a ZWJ sequence or a skin tone after `⚽︎` stayed narrow. The grid measurements above showed herdr does not narrow them, so that is fixed here with two more tests that fail without the fix. - Failing first: with `main`'s `terminalWidths.ts` and patch, 5 of the new xterm tests fail. Without `0xfe0e` in `joinsEmoji`, 5 of the libghostty tests fail. - `bun run check fast`: pass (typecheck, build, unit tests) at the merged head - Screenshots (isolated herdr session, xterm, 390 and 1280 px, light and dark) were checked by eye: in `A⚽︎X|`, the `|` sits one column left of the one in `0123|`, as herdr's grid has it. ## Known remaining (LOW) - If the font has no text glyph for the emoji, the browser draws the colour emoji over the two columns. That is how the font draws it; the layout is right. - `agentCopy.ts` counts cells with `wcwidth`, one code point at a time, so it still counts `⚽︎` as two when it unwraps copied agent text. A row that ends right after such an emoji may not be joined back. - U+1F21A and U+1F22F are narrowed too. The `unicode-width` crate leaves the Enclosed Ideographic Supplement two cells wide, but herdr 0.9.5 and herdr-core 0.1.17 both measured `A🈚︎X` at column 4, and this follows the grid. - REP (`CSI b`) after `⚽︎`: herdr repeats the two-column base without its selector. xterm repeats the one-column cell, and libghostty lays out the repeats differently again. All three disagree, and none of them did before this change either. - A caller that asks xterm's unicode service for a string's width between a write that ends on a text-form emoji and the next write that starts with U+FE0E resets the provider's memory. The app has no such caller today. - libghostty misses a U+FE0E whose UTF-8 bytes are split between two byte-array writes. The app writes strings. - In insert mode (`CSI 4 h`), narrowing `⚽︎` leaves the freed column blank instead of shifting the text after it left. herdr's result for this was not measured. - herdr-core 0.1.14 (the version CI's live contract pins) still gives `⚽︎` two columns. The desktop app bundles 0.1.17.
Fixes #2934
Follow-up to #2908.
What was wrong
With Settings > Terminal > Draw with libghostty on, libghostty gave each code point of an emoji
sequence its own wide cell. For
ABC👨👩👧XYZit drewXYZat column 9, while xterm (cursor,selection, links, and herdr's grid via
terminalWidths.ts) has it at column 5. Skin tones, flags,keycaps, subdivision flags and
⚠️(emoji-presentation selector) drifted the same way.Fix
src/lib/ghostty/vt.ts,write():everything except emoji sequences as herdr and xterm do. Turning it on everywhere also clusters
what they keep apart: Bengali and Devanagari vowel signs, Indic conjuncts (
क्ष), Thai SARA AM. Review caughtthat.
it, VS16, skin tones, the keycap mark, tags and regional indicators. The scan is linear in the
bytes written: 1 MB of Hangul in 17 ms, 1 MB of ASCII in 10 ms.
CSI ? … 2027 … h, orrrestoring asaved one) is turned off again right after. A small CSI state machine finds it across writes,
through C0 controls or DEL inside the sequence, and across
reset().instead of being drawn as a blank.
Validation
bun test src/lib/ghostty/vt.test.ts src/lib/terminalWidths.test.ts: 39 pass. Withmain'svt.ts, 9 of the new tests fail.bun scripts/terminal-ghostty-demo-regression.ts: a new assertion on the real DOM checks that thefamily is one
.ghostty-widecell and that the text after it starts at xterm's column 5. It failson
main([ "👨", "👩", "👧" ]) and passes here.bun run check fast: pass (typecheck, build, unit tests)by eye. Before, the family showed as three emoji and the flag as
K R; after, each sequence is onetwo-column cell.
Not in this PR
⚽︎): herdr 0.9.5 and hc 0.1.17 give it one cell, while xterm's widths andthis PR give it two. Ghostty renderer: emoji with a text-presentation selector (U+FE0E) narrower than xterm #2954's lane handles it after this PR merges and will flip the
text-presentation selectortest invt.test.ts.Known remaining differences (LOW)
maineach is either the same or worse:👨 SGR 👩): libghostty still joins it, even withmode 2027 off.