Skip to content

fix(os): support OSC 52 clipboard sequences and Wayland wl-copy fallback - #45

Merged
Blankeos merged 1 commit into
Blankeos:mainfrom
felipecm-br:fix/clipboard-wayland-osc52
Nov 5, 2026
Merged

Blankeos merged 1 commit into
Blankeos:mainfrom
felipecm-br:fix/clipboard-wayland-osc52

Conversation

@felipecm-br

Copy link
Copy Markdown
Contributor

Summary

Enhances clipboard copy functionality across modern terminal environments and Linux display servers:

  • OSC 52 Support: Emits ANSI OSC 52 escape sequences (\x1b]52;c;<base64>\x07, with tmux passthrough wrapping when TMUX is set). This enables clipboard copying over SSH, remote tmux sessions, and modern terminals (Alacritty, Kitty, WezTerm, iTerm2, foot, ghostty, etc.) without requiring local clipboard binaries.
  • Wayland Support (wl-copy): On Linux, tries wl-copy first before falling back to xclip and xsel.
  • Non-blocking Spawn: Dispatches external clipboard utility calls in a background thread so terminal responsiveness is never blocked by clipboard processes.
  • Stdin Flush & Drop: Explicitly drops child stdin after writing in CmdBuilder::run_with_stdin, sending an immediate EOF so clipboard processes do not hang waiting for standard input.
  • Unit Tests: Added test suite for base64 encoding in src/os/platform.rs.

Verification

  • cargo fmt checked.
  • cargo test --bin lazygitrs (372 passed).

@Blankeos

Blankeos commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

fix/clipboard-wayland-osc52 → main
Model: claude-sonnet-5-5, effort unknown

Confidence: 4/5

Small, self-contained change to one function plus a stdin fix. I merged it with current main, built it and ran it on macOS (Ghostty) and in a Linux VM. The remaining risk is the lost error reporting and untested tmux/SSH paths.
Checks run: merges cleanly with current main; cargo fmt --check clean; cargo test on the merged result: 380 passed, 0 failed (includes the new base64 test); cargo clippy shows no new warnings in the changed code. OSC 52: ran the release build in Ghostty with pbcopy removed from PATH (only OSC 52 could set the clipboard) after setting the clipboard to a sentinel; copying a file's relative path from the Files copy menu put f1.txt in the clipboard, and the screen showed no corruption. Wayland fallback: ran a Linux build in a VM with a stub wl-copy first on PATH and no xclip/xsel; the stub received f1.txt on stdin and exited, so the stdin-drop EOF fix works.
Confidence bumps: make clipboard failures visible again (see below) → 4.5/5. Not covered: tmux passthrough (needs allow-passthrough), SSH, a very large "copy diff" payload, and a real Wayland session.

Summary

Thanks Felipe, this is a nice improvement. OSC 52 plus wl-copy fixes real gaps (SSH, Wayland), and dropping stdin after the write is the right fix so the child sees EOF.

A few things to look at:

  • copy_to_clipboard now always returns Ok(()), because the external command runs in a detached thread and its result is discarded. Callers like src/gui/mod.rs (~2895 and ~4213) and commits.rs (? on every copy) used to surface failures. Now a missing clipboard tool silently shows "copied". Could the thread report failure, or could you only treat it as success if OSC 52 or the command worked?
  • OSC 52 is written to stdout on every copy, including huge payloads like "copy diff", and also fires on local macOS where pbcopy already works. Consider a size cap for OSC 52 (some terminals and tmux cap the payload) and skipping it when the external tool succeeds.
  • The raw stdout write while ratatui owns the terminal did not glitch in my Ghostty run (one write plus flush).
  • In cmd.rs, let _ = stdin.write_all(...) now swallows a broken pipe (good for fast-exiting tools) but also hides real write errors. run_with_stdin is shared, so check other users.
  • Nit: a hand-rolled base64 avoids a dependency and is tested; a test for the OSC 52 string wrapping (including the tmux form) would be nice.

Map

flowchart LR
  GUI[gui/mod.rs + controller/*] --> CP[Platform::copy_to_clipboard]
  CP --> OSC[write_osc52_clipboard -> stdout]
  CP --> T[spawned thread]
  T --> CB[CmdBuilder::run]
  CB --> RS[run: stdin write + drop]
  T --> TOOLS[pbcopy / clip / wl-copy / xclip / xsel]
  OSC --> B64[base64_encode + tests]
Loading

Regressions

  • Error reporting lost: copy_to_clipboard always returns Ok, so the if let Err(e) and ? at its callers (gui/mod.rs ~2895, ~4213, ~4701, commits.rs ~926-1007) never fire for clipboard failures.
  • OSC 52 is emitted unconditionally. Terminals that don't support it may ignore it, but very large payloads can be truncated or cause glitches.
  • Direct stdout writes while the TUI is active could interleave with rendering.
  • The copy_to_clipboard_cmd user config (user_config.rs:243) is still not honoured, as before.
  • wl-copy forks a background process, and piped stdout/stderr in CmdBuilder::run may keep wait_with_output waiting. It is now in a thread, so the UI is not blocked, but threads could linger.

Migrations

None. No config, keybinding or format changes.

Before merging

  • Decide on error propagation (or document that failures are silent now)
  • Cap the OSC 52 payload size, or skip it when a local tool succeeded
  • Test on Wayland (wl-copy only), X11, macOS, and in tmux and SSH
  • Verify no screen corruption when copying while the TUI renders
  • Add a test for the OSC 52 string and tmux wrapping
  • Check other run() stdin users aren't affected by the swallowed write error

@Blankeos
Blankeos merged commit 73356c8 into Blankeos:main Nov 5, 2026
6 checks passed
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.

2 participants