Skip to content

web: run the terminals on Ghostty's emulation core - #78

Open
SukharevAndrey wants to merge 4 commits into
pithings:mainfrom
SukharevAndrey:web/ghostty-terminals
Open

SukharevAndrey wants to merge 4 commits into
pithings:mainfrom
SukharevAndrey:web/ghostty-terminals

Conversation

@SukharevAndrey

@SukharevAndrey SukharevAndrey commented Nov 28, 2026 •

Copy link
Copy Markdown

This started with Claude Code looking wrong in the page's terminal: dark seams through its logo, and copy that did nothing. I got carried away from there. The page's terminals now run on Ghostty's terminal emulation (@wterm/ghostty, libghostty-vt compiled to wasm) instead of wterm's built-in core, because Ghostty implements what modern terminal programs use more completely: OSC 52 clipboard writes, Kitty graphics, colour and mode queries, soft-wrap tracking. wterm's DOM renderer still draws them. Along the way came a few features and fixes, listed below.

What was broken

  • Copying from a program did nothing. Claude Code, vim and tmux copy with OSC 52. wterm hands that to the host as onClipboardWrite, but the page never set it, so Claude Code said "copied" and the clipboard kept its old text.
  • A drag of a window's edge made full-screen programs stutter. Every step of the drag was a new grid size and a SIGWINCH, and a program that redraws its whole screen each time (Claude Code) fell behind.
  • The screen started at a slice of a history row when the terminal's height was not a whole number of rows.

What changed

The core (web/src/termcore.ts, new)

  • Each terminal gets its own GhosttyCore. The module and its wasm load and compile once. The console's starts loading while the preflight runs, and the console waits for it at most 10 s once the boot module is in.
  • If it does not load, the terminals use wterm's built-in core. The Kernel log says why, and once a terminal runs on it, a notice says that copy from programs and Kitty images are off. The notice waits until the page is visible and no other notice is up.
  • @wterm/dom and @wterm/ghostty 0.6.0, pinned exactly. ghostty-vt.wasm is 600 KB, 162 KB with brotli.

Terminals (web/src/wterms.ts, web/src/boot.ts)

  • OSC 52 copies go to the clipboard at once only from the focused terminal, within 3 s of a key or a click in it. Otherwise, or where the browser wants a click (Safari), a notice offers Copy. On resume, the console and reattached terminals redraw saved output with clipboard writes, bells and replies to terminal queries suppressed. For reattached terminals, the bundled guest program kwtty reports the replay byte count in its OPENED reply. Older guest bundles omit the count and can still show the stale copy prompt.
  • Sessions the page spawns get COLORTERM=truecolor (Ghostty draws 24-bit colour; Claude Code otherwise drops to 256 colours) and CLAUDE_CODE_NO_FLICKER=1, Claude Code's fullscreen interface, which a resize redraws cleanly. The console's login gets neither: kwtty passes it TERM only.
  • A window's terminals keep their size while its edge is dragged and take the new one when it is let go. Settings, Display and sound, has Resize live to follow the drag as before (off by default, applied at once). wm.ts dispatches kw-wm-resize per window around a drag, and a drag now ends when the page loses the focus.
  • The terminal's height is rounded down to whole rows in CSS (not on touch devices, where wterm insets the grid for the keyboard).
  • A tab whose core is still loading has no wterm yet: currentTerm() returns null for it, and a terminal that fails to start shows why as plain text.

Find (web/src/termfind.ts, new)

  • ⌘F on Apple devices, Ctrl-Shift-F, or the prefix's f opens a bar at the top right of the current terminal's window. It searches wterm's core, so it covers the whole history (the browser's find sees only the rows in view). Enter and ⇧Enter go to the next and the previous match, Esc closes it.
  • ⌘F and the chord act only while a terminal or the bar has the focus; elsewhere ⌘F stays the browser's. Commands got an optional when for this.

Smaller things

  • Bells: a terminal that rings while it is not in front of the user gets a dot on its tab until it is; a shown one's tab flashes once.
  • The cursor blinks as the program asks (DECSCUSR) instead of always.
  • Each terminal has an aria-label ("Terminal 0, the console", "Terminal N, name"), which wterm gives its input.
  • The mouse selection uses the theme's selection colour, and a background a program sets with OSC 11 shows in glass windows.
  • Settings, Keyboard, lists what the mouse does in a terminal (links, word and line selection, rectangles, Shift-drag past a program's mouse mode, select all, find).
  • The Claude Code app no longer writes ~/.claude/settings.json.
  • README, About, THIRD-PARTY.md (Ghostty 1.3.1, MIT, and Wuffs, Apache-2.0, in ghostty-vt.wasm) and .agents/toolchain.md updated.

Not in this PR

The seams through Claude Code's logo are wterm's: block cells drawn in the DOM sit next to cells drawn on its cell canvas, and at a fractional cell width the two round the shared edge to different device pixels. I reported it as vercel-labs/wterm#316 and sent a fix, vercel-labs/wterm#317. The page gets it with the wterm release that has it; until then the seams stay.

In the translucent Terminal windows, block elements still paint the part their glyph leaves empty in the terminal's opaque background, as on main. vercel-labs/wterm#319 (issue #318) adds --term-block-bg for that; once a release has it, one line in style.css lets the glass show through.

Testing

  • pnpm typecheck, pnpm typecheck:web, pnpm build:web and git diff --check: clean.
  • Headless Chromium against the built page, no guest boot: the find bar opens with ⌘F and the prefix's f (also from Settings), counts and steps through matches, closes with Esc; ⌘F in Settings does not open it; the terminal's height is a whole number of rows; with the ghostty-vt wasm request blocked, the console runs on the built-in core and the notice shows.
  • By hand in a Chromium-based browser on a Retina Mac, Claude Code in the page: its copy reaches the clipboard, its colours match a desktop terminal, and resizing a window no longer makes it stutter.
  • All six terminal scenarios passed in headless Chrome 158 with the rebuilt kwtty in the guest bundle. They cover scrollback search, both resize modes and the guest’s stty size, session environment and bells, OSC 52, suspend/resume, and startup with the Ghostty wasm blocked. All 25 SDK tty tests passed.
  • Rechecked suspend and resume in Chrome 158, Safari 27.2 and Firefox 161 on macOS. Both shells retained their state. Replaying the second terminal’s history produced no Copy prompt, and a fresh OSC 52 request after resume worked. The Chrome regression test puts the original request beyond the first 16 KiB of replay and sends the same text again after resume.
  • To reproduce with a local kernel build, refresh the guest bundle with kernel/build.sh base, rebuild the page with pnpm build:web, and run pnpm test:web --only term-find,term-resize,term-session,term-copy,term-resume,term-fallback.
  • All 29 Node tests in web/test/*.test.ts passed, as did web/test/net.mjs. The existing mobile, modfail and lowmem browser scenarios passed. Mobile coverage used Chrome touch emulation, including typing, keyboard controls, inertial scrolling and rotation. Physical phones remain untested.
  • Four existing checks also fail on the parent commit (df6d726) with the same guest image. The desktop paste check waits for execution without pressing Enter. nojspi expects an exact status colour. The simulated cross-origin fetch404 response lacks a CORS header. data.mjs waits for a marker at the start of a line after a command that prints no trailing newline.

The page's terminals use @wterm/ghostty (libghostty-vt in wasm) instead of
wterm's built-in core, which they fall back to, with a notice, if it does
not load. Programs' OSC 52 copies reach the clipboard (at once only from
the focused terminal right after input, else a notice offers Copy).
Sessions get COLORTERM=truecolor and CLAUDE_CODE_NO_FLICKER=1. A window's
terminals keep their size during a drag of its edge unless Resize live is
on. ⌘F / Ctrl-Shift-F finds in a terminal's history; a bell marks its tab;
the cursor blinks as the program asks; Settings, Keyboard lists the mouse
gestures. @wterm/dom and @wterm/ghostty 0.6.0.
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