Skip to content

feat(tui): OSC 52 clipboard for SSH/headless terminals - #87

Merged
lance0 merged 1 commit into
masterfrom
feat/tui-osc52-clipboard
Jun 25, 2026
Merged

lance0 merged 1 commit into
masterfrom
feat/tui-osc52-clipboard

Conversation

@lance0

@lance0 lance0 commented Jun 25, 2026

Copy link
Copy Markdown
Owner

What

y-copy used arboard, which on Unix only speaks X11/Wayland. Over SSH with no display forwarding it waits out the X11 connection timeout and then fails with a cryptic error — nothing reaches a clipboard. This adds an OSC 52 path so copy works over SSH and on headless terminals.

Behavior by platform

Platform Primary Fallback
Linux / non-macOS Unix, with $DISPLAY/$WAYLAND_DISPLAY arboard (native) OSC 52 on error
Linux / non-macOS Unix, no display (SSH/headless) OSC 52 (skips arboard + its X11 timeout) —
macOS / Windows arboard (native pasteboard; DISPLAY-agnostic) OSC 52 on error
--no-default-features (no clipboard) OSC 52 —

OSC 52 (ESC ]52;c;<base64>BEL) is written to the terminal stream, so the local terminal — not a remote X server — writes the local clipboard.

Correctness: no stdout race

OSC 52 bytes are written only on the event-loop thread, never from a spawned task:

  • the headless skip writes synchronously inside dispatch (loop thread);
  • the native-error fallback round-trips through a new internal AppEvent::CopyViaTerminal, intercepted in the loop before handle_event.

So the escape is serialized with terminal.draw and the two can't interleave on stdout (crossterm flushes the frame; write_osc52 flushes after). OSC 52 doesn't move the cursor or draw cells, so ratatui's next diff is unaffected.

Notes

  • Status stays honest: copied: <v> (native) vs copied via terminal: <v> (OSC 52).
  • Known edge: detection keys on DISPLAY/WAYLAND_DISPLAY (an X11/Wayland concept), so SSH-into-a-Mac can't be distinguished — arboard succeeds to the remote Mac's pasteboard rather than falling back to OSC 52. Deliberate (Terminal.app's OSC 52 is unreliable; SSH-into-Mac-TUI is rare); SSH_CONNECTION could close it later if wanted.
  • base64 moves dev-dep → runtime dep (now used in non-test code).

Verification

  • Platform-conditional logic is unit-tested cross-platform via a cfg(any(feature = "clipboard", test)) gate: display detection, the skip decision, OSC 52 encoding, and the buffered write. 5 new tests, present in both feature modes.
  • fmt clean; clippy --all-targets clean on --all-features and --no-default-features; 1139 all-features / 1044 no-default tests pass, 0 failed.

Docs

README (feature table + keymap), CHANGELOG (Added), ROADMAP (item ☑), and a docs/TROUBLESHOOTING.md entry covering the tmux set -g set-clipboard on caveat.

`y`-copy used `arboard`, which only speaks X11/Wayland on Unix — over SSH with
no display forwarding it waits out the X11 connection timeout, then fails with a
cryptic error and nothing reaches a clipboard.

- On non-macOS Unix with no `$DISPLAY`/`$WAYLAND_DISPLAY`, skip `arboard` (and its
  X11 timeout) and emit an OSC 52 escape (`ESC ]52;c;<base64>BEL`) so the *local*
  terminal writes the *local* clipboard over the SSH stream. macOS and Windows
  keep their native desktop backends first (DISPLAY-agnostic; Terminal.app's OSC
  52 is unreliable), with OSC 52 as a fallback if the native call errors.
- OSC 52 bytes are written on the event-loop thread, never from a spawned task:
  the headless path writes synchronously in `dispatch`, and the native-error path
  round-trips through a new internal `AppEvent::CopyViaTerminal` intercepted in
  the loop before `handle_event`. This serializes the escape with `terminal.draw`
  so the two never interleave on stdout.
- The lean `--no-default-features` build now copies via OSC 52 too, instead of
  reporting "clipboard not built in".
- Status stays honest: "copied: <v>" for the native path, "copied via terminal:
  <v>" for OSC 52.

Platform-conditional logic is unit-tested cross-platform via a `cfg(any(feature,
test))` gate (display detection, the skip decision, the OSC 52 encoding/write).
Docs: README feature/keymap, CHANGELOG, ROADMAP (☑), and a TROUBLESHOOTING entry
covering the tmux `set-clipboard on` caveat. `base64` moves dev → runtime dep.
@lance0
lance0 merged commit 11b200f into master Jun 25, 2026
4 checks passed
@lance0
lance0 deleted the feat/tui-osc52-clipboard branch June 25, 2026 15:59
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