Skip to content

feat(web): terminal citation previews render with the terminal's own renderer - #360

Merged
adampeterhiggins merged 4 commits into
mainfrom
ah/terminal-citation-preview
Oct 20, 2026
Merged

adampeterhiggins merged 4 commits into
mainfrom
ah/terminal-citation-preview

Conversation

@adampeterhiggins

@adampeterhiggins adampeterhiggins commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Hovering a terminal citation re-laid the excerpt out as HTML text, so anything wider than the card was rewrapped: powerline prompts split into ragged boxes, words broke mid-token, and continuation lines lost their alignment. The preview now uses the terminal's own renderer, read-only, so it looks and selects exactly like the terminal drawer.

Before After

What changes for users

  • Lines keep their captured width. Wide lines scroll sideways instead of rewrapping; tall excerpts show 16 rows and scroll, opening at the top.
  • Selection works like the terminal: drag, double/triple-click, Cmd+A, Cmd+C, Edit → Copy, right-click Copy / Select all, and a Copy button in the card header (copies the selection, or the whole excerpt).
  • Dragging a selection past the card's edge scrolls it sideways, and the card stays open once you've pressed inside it, so the selection survives until you copy.
  • The card stays out of the way: app shortcuts, Escape, and Tab still work while it's focused, paste never touches the clipboard, and closing it hands focus back to the composer.
  • Touch: a long press selects a word.
  • Always shows something: plain text while the renderer loads (loading starts on hover intent) or if it fails.

How

  • The card mounts a GhosttyTerminalSurface with a new readOnly option, sized to the excerpt with sizeToGrid (columns from the widest line, wide characters counted as two cells, capped at 400). It's created when the card opens and disposed when it closes.
  • readOnly is enforced in one place each: an allowlist at the top of the key handler, so key handling added later for the drawer skips viewers by default, and a single sendInput that every PTY-bound byte passes, including the terminal's own replies.
  • ContextChipPopover gains onIntent (preload) and stayOpenAfterPress (pin plus focus hand-back).
  • The HTML SGR parser (terminalStyledText.ts) is removed; its remaining helper moves into terminalContext.ts.

Mobile has no hover preview, so it's unaffected. Old excerpts without styled text render as plain text through the same surface.

Known, not changed here: after clicking inside any chip hover card (also on main), clicking another control such as the sidebar search moves focus to the chip button. That's Base UI focus handling shared by every ContextChipPopover, so it belongs in its own fix.

Screenshots

Drag selection (copied 日本語 wide 🎉 ok)
Scrolled sideways
Right-click menu
Plain-text fallback (top) and touch long press (bottom)
Drag past the right edge, then back past the left
31-line excerpt, at the top and after scrolling

Testing

  • Unit: 130 passed across terminalContext.test.ts, ghostty/core.test.ts, and ghostty/surface.test.ts. New tests cover the read-only mode (keys, paste, input, composition, middle click, alternate-screen wheel, status replies, focus on creation), touch long press, drag extension, and that a styled selection replays into a fresh terminal with identical cells. tsc and lint are clean on the changed files.
  • Real browser, on composer chips and on a sent message carrying the original broken excerpt:
    • rendering, drag-select + Cmd+C, double-click, sideways and vertical scrolling
    • Edit → Copy, via the editor copy command Electron's menu role uses
    • Ctrl+K, Cmd+V, and typing inside the card aren't swallowed; no clipboard reads; Escape closes and returns focus to the composer
    • Copy button, right-click menu, drag past either edge, release outside then copy
    • WASM blocked → plain-text fallback; touch tap, long press, and Copy

Built with Claude Opus 5.5 in Claude Code (T3 Code).

🤖 Generated with Claude Code

adampeterhiggins and others added 4 commits October 20, 2026 17:33
…renderer

The hover preview re-laid captured output as HTML text, which rewrapped wide
lines to the card's width and broke powerline prompts apart. It now mounts a
read-only Ghostty surface sized to the excerpt, so lines keep their captured
width and scroll sideways, and selection, copy, and scrolling are the terminal
drawer's own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The preview's terminal swallowed app shortcuts, could trigger a clipboard
permission prompt on paste, showed an empty box if the renderer failed to
load, had no right-click Copy or touch selection, stranded focus when the
card closed, and closed mid-selection when a drag ended outside it.

The surface gains a readOnly mode: only copy, select-all, and scrollback
keys are handled, nothing is pasted, and a touch long press selects a word.
The card shows plain text until the renderer is ready (or if it fails),
starts loading on hover intent, offers Copy and Select all on right-click and
a Copy button in its header, stays open once pressed inside, and hands focus
back to the composer when it closes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… it sideways

The terminal autoscrolls a selection drag vertically through its scrollback,
but the preview's sideways scroller sat still, so a drag past the card's left
or right edge selected columns out of view. The card now scrolls while the
pointer is past an edge and extends the selection through the surface's new
extendSelectionDrag.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…place

A read-only surface opted out of key handling part-way down the handler, so a
key branch added above that point for the drawer would have reached previews
too. Read-only is now an allowlist checked first (selection, copy, and
scrollback keys), and every byte bound for the PTY, including the terminal's
own replies, passes one sendInput that drops it for a viewer. The read-only
test now covers keys, paste, input, composition, middle click, wheel on the
alternate screen, status replies, and focus on creation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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