fix(desktop): trim terminal copy whitespace

Merged
NR
2mo ago
+515-94

What & why

Normalizes terminal copying across the legacy and v2 terminals without stripping meaningful selection content. Normal multiline selections lose trailing ASCII row padding; Unicode whitespace, tabs, CRLF, indentation, and blank lines are preserved. Single logical lines, rectangular selections, whitespace-only selections, and partial final-line spaces retain xterm's selected text.

Keyboard copy, context-menu Copy, and copy-on-select share the policy. Clipboard writes fall back to Electron's native clipboard when the browser API is missing or rejects; failed writes remain retryable. Runtime disposal removes the copy handler, and pending copy-on-select completion cannot flash success after disposal.

Builds on the original contributor's implementation in this PR. Refs #2896. This addresses row padding; it does not reconstruct application-emitted hard line breaks into a single logical code-block line.

How I tested it

  • bun run lint: passed.
  • bun run typecheck: passed, 42 tasks successful.
  • Focused terminal copy, clipboard, runtime registry, and copy-on-select tests: 46 passed.
  • Real macOS desktop: keyboard and context-menu copy remove 107 padding spaces, preserve indentation/blank lines, Unicode spaces, rectangular rows, and whitespace-only selections; copying still works after renderer reload.
  • Forced browser clipboard rejection in the real renderer: native Electron fallback wrote the expected text, including CRLF and U+3000. This is diagnostic fault injection, not an actual Linux/Wayland test.
  • bun run test: fails in existing desktop Git/worktree and shell-environment tests. The original PR head also has the same 34 failing cases: baseline 3,995 pass / 34 fail; updated branch 4,018 pass / 34 fail. No new failure cases.

Before/after screenshots, raw clipboard captures, reference comparisons, and validation evidence (Superset organization access).

Selection-mode detection uses an isolated adapter for the pinned xterm internals; unknown modes preserve raw text. Full multiline rows cannot distinguish intentional trailing ASCII spaces from padding, so normal multiline copying trims them; single-line and rectangular copying preserve them.

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs

Summary by CodeRabbit

  • Bug Fixes

    • Improved terminal copying by removing trailing spaces while preserving indentation, line endings, wrapped lines, and intentional whitespace.
    • Standardized copy behavior across terminal views and selection modes.
    • Added clipboard fallback support when browser clipboard access is unavailable or fails.
    • Copy operations now handle failures safely and clean up when terminals close.
    • Prevented stale copy callbacks after disposal and ensured repeated identical selections can be copied.
  • Tests

    • Expanded coverage for selection normalization, clipboard fallbacks, error handling, and lifecycle cleanup.