fix(desktop): copy terminal selections with Ghostty's whitespace rules
Merged
KI
2mo ago+386+386+012345678901234567890123456789-66-66-01234567890123456789
Summary
- Terminal copy (Cmd+C, copy-on-select, runtime
getSelection) now reads the selected buffer cells and applies Ghostty's default formatter rules, instead of post-processing xterm's selection string. - 27 regression cases ported from Ghostty's
selectionStringtests (ghosttye88c6c099,src/terminal/Screen.zig) run against real@xterm/headlessbuffers.
Why / Context
We had custom exceptions that diverged from Ghostty: trailing spaces were kept for single-line, partial, rectangular, and whitespace-only selections. Worse, trimming ran on xterm's already-formatted string, which had dropped spaces at soft-wrap boundaries and converted NBSP, so no string-level fix could reach parity. The old tests encoded our assumptions rather than Ghostty's behavior.
How It Works
getTerminalSelectionForCopywalks rows fromgetSelectionPosition()(linear or rectangular via xterm's private_activeSelectionMode), collecting cell chars.- Matches Ghostty's formatter defaults: trim trailing blanks on rows with text, always drop trailing blank rows, keep blank rows between text, unwrap soft wraps (keeping interior spaces across the wrap), skip wide-char tail cells, and include a wrapped wide char when the selection ends on its spacer head.
- Falls back to trimming the raw string when bounds or mode are unavailable.
- A selection that trims to nothing now writes
""to the clipboard (Ghostty behavior) rather than being treated as "no selection" — both in the copy handler and copy-on-select.
Manual QA Checklist
- Select
abcon a line → pastedabc - Select across a soft-wrapped line with spaces at the wrap → interior spaces preserved
- Alt/Option rectangular select over ragged lines → trailing spaces trimmed per row
- Select only spaces → clipboard cleared
- Wide chars / emoji at a wrap boundary copy intact
Testing
bun teston terminal-copy, terminal-copy.ghostty, terminal-clipboard, terminal-runtime-registry, copyOnSelect — 76 passtsc --noEmit -p apps/desktop/tsconfig.jsonbiome check- Not run: live UI verification in the running desktop app (the checklist above is unchecked).
Known Limitations
- Parity target is Ghostty's defaults only (
clipboard-trim-trailing-spaces = true, noclipboard-codepoint-map). - Rectangular detection depends on xterm's private
_core._selectionService._activeSelectionMode; if that changes, we fall back to trimming the raw string.
Summary by cubic
Terminal copy (Cmd+C, copy-on-select, runtime getSelection) now reads selected buffer cells and applies Ghostty's default formatter rules instead of post-processing xterm's formatted string.
Behavior
- Trailing whitespace is trimmed on every selected row, including single-line, partial, rectangular, and whitespace-only selections.
- Soft-wrapped lines preserve interior spaces across the wrap and match Ghostty's wrap flags.
- A selection that trims to nothing writes
""to the clipboard rather than being treated as no selection.
Testing
- 27 regression cases ported from Ghostty's
selectionStringtests run against real@xterm/headlessbuffers. bun test,tsc --noEmit, andbiome checkpass; live UI verification was not run.
Summary by CodeRabbit
- Bug Fixes
- Terminal selections now copy more consistently, including rectangular selections, wrapped lines, tabs, wide characters, and selections spanning scrollback.
- Trailing spaces and line endings are trimmed from copied text, while meaningful spaces and blank lines within a selection are preserved.
- Selections containing only whitespace or empty cells now copy as an empty string without preventing a valid selection from being copied.