Skip to content

fix(terminal): lay out emoji sequences in libghostty as xterm does - #2948

Merged
devswha merged 10 commits into
mainfrom
fix/ghostty-grapheme-widths
Nov 5, 2026
Merged

devswha merged 10 commits into
mainfrom
fix/ghostty-grapheme-widths

Conversation

@devswha

@devswha devswha commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

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👨‍👩‍👧XYZ it drew XYZ at 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():

  • libghostty's grapheme clustering (DEC mode 2027) stays off. Without it libghostty lays out
    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 caught
    that.
  • Each code point that joins an emoji is printed with mode 2027 on: ZWJ and the pictograph after
    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.
  • xterm ignores mode 2027, so a program's own 2027 on (CSI ? … 2027 … h, or r restoring a
    saved 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().
  • A grapheme longer than the 120-byte read buffer is read again with the size libghostty asks for,
    instead of being drawn as a blank.

Validation

  • bun test src/lib/ghostty/vt.test.ts src/lib/terminalWidths.test.ts: 39 pass. With main's
    vt.ts, 9 of the new tests fail.
  • bun scripts/terminal-ghostty-demo-regression.ts: a new assertion on the real DOM checks that the
    family is one .ghostty-wide cell and that the text after it starts at xterm's column 5. It fails
    on main ([ "👨‍", "👩‍", "👧" ]) and passes here.
  • bun run check fast: pass (typecheck, build, unit tests)
  • Screenshots (demo fixture, herdr-core, libghostty on; 390 and 1280 px, light and dark) were checked
    by eye. Before, the family showed as three emoji and the flag as K R; after, each sequence is one
    two-column cell.
  • No changelog fragment: this fixes the unreleased feat(terminal): draw herdr-core panes with libghostty, Kitty graphics pictures included, behind a Settings toggle #2908 feature, whose own fragment covers it.

Not in this PR

Known remaining differences (LOW)

  • Each case below was found in review, and on main each is either the same or worse:
    • A style change inside an emoji sequence (👨 SGR ‍👩): libghostty still joins it, even with
      mode 2027 off.
    • A lone regional indicator.
    • A ZWJ sequence past libghostty's grapheme storage limit (about 34 people).
  • Byte-array writes that split a joiner's UTF-8 between two calls. The app writes strings.

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
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

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

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 09a5939 and 8bb4564.

Files selected for processing: 3

  • scripts/terminal-ghostty-demo-regression.ts
  • src/lib/ghostty/vt.test.ts
  • src/lib/ghostty/vt.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

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

devswha commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner Author

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 src/lib/ghostty/vt.test.ts (45 cases, run against xterm with matchHerdrWidths):

  • A program's own 2027 on (h, or r restoring it): caught across writes, through C0 controls or DEL inside the sequence, and across reset().
  • Linear scan. The first version was quadratic on numeric output (128 KB of 2027 in 1.3 s). 1 MB of Hangul now takes 17 ms.
  • A ZWJ sequence over 120 bytes is read again with the size libghostty asks for, instead of drawing as a blank.
  • Spacing marks and Indic conjuncts stay apart as in xterm. U+FE0E is left to Ghostty renderer: emoji with a text-presentation selector (U+FE0E) narrower than xterm #2954's lane, since herdr gives it one cell, not xterm's two.
  • No join across an escape sequence (a style change, an OSC title), as in xterm.

Codex's last pass at 5b3bc39b has no HIGH. These remain, all rare in real output:

  • MEDIUM, new: a C0 control between an emoji and its skin tone (👍 BEL 🏽, 👍 BS 🏽) still joins, where xterm splits. xterm does join a flag's halves across BEL, so the rule depends on the joiner.
  • MEDIUM, new: a 3-byte escape such as ESC ( B between an emoji and its skin tone ends the scanner's escape one byte early, so the skin tone still joins.
  • LOW: CAN or SUB inside a non-private CSI leaves the scanner in it until the next final byte.
  • LOW, also on main: a joiner whose UTF-8 is split between two Uint8Array writes is missed. The app writes strings.
  • LOW: ⚠ + combining acute + VS16 loses the selector in libghostty's cell. The column matches main.
  • LOW, also on main: a lone regional indicator, and a ZWJ sequence past libghostty's grapheme storage limit.

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Local merge gate (owner-approved): bun run check fast passed at this head, omo CLEAR at this exact head (Codex: no HIGH), threads resolved; main moved 3 commits since, none touching ghostty or terminal widths, and it merges cleanly. 9 vt tests and the browser regression fail on main and pass here. FE0E width is handled separately (#2954). Merged without waiting for GitHub CI.

@devswha
devswha merged commit 2a2c494 into main Nov 5, 2026
15 checks passed
@devswha
devswha deleted the fix/ghostty-grapheme-widths branch November 5, 2026 11:18
devswha added a commit that referenced this pull request Nov 5, 2026
…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.
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.

terminal: ZWJ emoji sequences are wider under Draw with libghostty than in xterm

1 participant