Skip to content

feat(tui/clipboard): multi-backend copy with OSC 52 fallback (#65)#188

Open
quangdang46 wants to merge 1 commit into
masterfrom
feat/clipboard-osc52-fallback
Open

feat(tui/clipboard): multi-backend copy with OSC 52 fallback (#65)#188
quangdang46 wants to merge 1 commit into
masterfrom
feat/clipboard-osc52-fallback

Conversation

@quangdang46
Copy link
Copy Markdown
Owner

What

copy_to_clipboard previously tried only wl-copy then arboard, dropping silently on every other path. SSH sessions, machines without those tools, and X11 systems with xclip/xsel were all broken.

This addresses issue #65: #65

Changes

src/tui/app/helpers.rs — replace with a layered resolver:

  1. SSH detection: if SSH_CONNECTION or SSH_TTY is set, skip native helpers and write OSC 52 directly so the clipboard lands on the user's local machine.
  2. Platform-native (in order):
    • macOS → pbcopy
    • Linux Wayland → wl-copy --type text/plain;charset=utf-8 (only if WAYLAND_DISPLAY is set)
    • Linux X11 → xclip -selection clipboard -in then xsel --clipboard --input (only if DISPLAY is set)
    • Windows → arboard
  3. OSC 52 fallback (BEL-terminated, base64-encoded) — kitty, alacritty, foot, wezterm, iTerm2, Ghostty, and recent xterm honor it.

The pure helpers (native_clipboard_commands_for_env, write_osc52_clipboard_to, should_prefer_osc52_for_env) are pub(super) so the test module can exercise them without touching real env / stdout.

src/tui/app/helpers_tests.rs — 4 new tests:

  • native_clipboard_commands_prefer_wayland_before_x11
  • native_clipboard_commands_are_empty_without_display_env
  • osc52_clipboard_writer_emits_base64_bel_sequence — pins the exact \x1b]52;c;<base64>\x07 byte sequence.
  • ssh_sessions_prefer_osc52_clipboard

Tests

All 4 new tests pass; all pre-existing helpers tests still pass.

Notes

Ports upstream PR 1jehuang#68.

The previous copy_to_clipboard tried wl-copy first, then arboard,
and dropped silently on every other path. SSH sessions, machines
without wl-copy/arboard, and X11 systems with xclip/xsel were all
broken.

Replace with a layered resolver:
1. If SSH_CONNECTION or SSH_TTY is set, skip native helpers and emit
   OSC 52 directly so the clipboard ends up on the user's local
   machine instead of the remote host.
2. Otherwise try platform-native helpers in order:
   - macOS: pbcopy
   - Linux Wayland: wl-copy --type text/plain;charset=utf-8 (only if
     WAYLAND_DISPLAY is set)
   - Linux X11: xclip then xsel (only if DISPLAY is set)
   - Windows: arboard
3. Final fallback: emit OSC 52 (BEL-terminated, base64-encoded) which
   modern terminals (kitty, alacritty, foot, wezterm, iTerm2,
   Ghostty, recent xterm) honor.

Helpers like native_clipboard_commands_for_env / write_osc52_clipboard_to /
should_prefer_osc52_for_env are pub(super) so the test module can
exercise them deterministically without touching real env / stdout.

Tests added in src/tui/app/helpers_tests.rs:
- native_clipboard_commands_prefer_wayland_before_x11
- native_clipboard_commands_are_empty_without_display_env
- osc52_clipboard_writer_emits_base64_bel_sequence
  (asserts \x1b]52;c;<base64>\x07)
- ssh_sessions_prefer_osc52_clipboard

Ports upstream PR 1jehuang#68.
Closes #65
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