Skip to content

feat(tui): fall back to wl-clipboard/xclip when the native clipboard is unavailable - #67307

Draft
MBanucu wants to merge 2 commits into
anomalyco:devfrom
MBanucu:feat/tui-linux-clipboard-fallback
Draft

MBanucu wants to merge 2 commits into
anomalyco:devfrom
MBanucu:feat/tui-linux-clipboard-fallback

Conversation

@MBanucu

@MBanucu MBanucu commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Refs #66351

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

On Linux, OpenTUI reads and writes the host clipboard through Wayland or X11
natively. When neither backend is usable it reports unsupported, and the TUI
shows Clipboard read is not supported on this system. On Nix this happens on
GNOME/Mutter: there is no Wayland data-control protocol, and OpenTUI's X11
fallback cannot dlopen libxcb.so.1 because the wrapper does not expose it
(#66351). The same unsupported status reaches users on hosts where the X11
clipboard library is missing, even though wl-paste/xclip can still reach the
desktop clipboard.

This adds a last-resort backend to the TUI clipboard adapter. It only runs on
Linux and only after the native read reports unsupported, or after a host
write reports unsupported/failed:

  • reads try wl-paste when WAYLAND_DISPLAY is set and xclip when DISPLAY
    is set, in the caller's MIME preference order, with a 1.5 s timeout and an
    8 MiB cap;
  • writes try wl-copy and xclip, serialised so a burst of copies keeps its
    order;
  • a write that fails through every helper reports the helper's stderr instead
    of claiming success when OSC 52 support is unconfirmed (TUI: "Copied to clipboard" toast shown even when nothing was copied (OSC52 "attempted" treated as success) #59664);
  • a missing tool, a timeout, or an empty clipboard falls back to the previous
    behaviour, so nothing changes on systems where the native backend works.

The fallback is injected through createClipboardAdapter and only enabled by
createTuiClipboard, so other OpenTUI consumers and the existing adapter tests
keep the native-only behaviour. It does not change OpenTUI itself, whose
documentation intentionally says it does not invoke wl-copy, wl-paste,
xclip, or xsel.

The second commit (cc15c65, authored by @SeifHediya in
MBanucu#1) covers the failure case: ClipboardTools.write returns
false when no helper is installed and rejects with the helper's stderr when
one runs but fails. The adapter only swallows that failure when the terminal
reported OSC 52 support, so an unconfirmed OSC 52 attempt no longer hides a
broken wl-copy.

A packaging fix for the Nix case is proposed separately in
#67304 and
NixOS/nixpkgs#595482. This PR is the broader fallback for hosts where the
native library cannot be loaded at all.

I have opened this as a draft because #66351 is a feature request and the
CONTRIBUTING guide asks for design approval before new functionality.

How did you verify your code works?

  • Unit tests cover the new paths with an injected backend: image read, empty
    clipboard, write fallback, failed tool write, no helper installed, and both
    OSC 52 capability cases. bun test packages/tui/test/clipboard.test.ts passes
    (12 tests).
  • The module typechecks with tsc against @opentui/core 0.9.9.
  • On NixOS, GNOME 50 (Mutter, Wayland), the real tool backend was exercised
    directly: read(["image/png", "text/plain"]) returned a PNG placed with
    wl-copy byte-for-byte, write() put text on the clipboard, and the same
    held with WAYLAND_DISPLAY unset, which forces the xclip candidates.
  • Built the merged branch (opencode v2.4.3+cc15c65) and ran it in a PTY with a
    PNG on the clipboard. Its wrapper has no libxcb, so the native backend
    reports unsupported; Ctrl+V still inserted [Image 1]. With
    WAYLAND_DISPLAY and DISPLAY unset, the same key inserted nothing and
    showed no error, which is the intended empty fallback.
  • Copy path, same build: with DISPLAY unset and failing wl-copy/xclip
    shims first on PATH, the "Copy session ID" action showed
    Failed to copy session ID; with the real helpers it showed
    Session ID copied to clipboard! and emitted the OSC 52 sequence. A fake
    wl-copy that fails while xclip succeeds still completes the copy.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@MBanucu
MBanucu force-pushed the feat/tui-linux-clipboard-fallback branch from 9ae7907 to 91376b7 Compare November 30, 2026 09:56
@abrown abrown mentioned this pull request Nov 30, 2026
1 task done
@SeifHediya

Copy link
Copy Markdown

I opened MBanucu/opencode#1 against this branch. It shows the helper error instead of “Copied to clipboard” when the write fails and OSC 52 support is unconfirmed. I tested it in Apple Terminal with clipboard.textCopy both off and on.

Preserve helper diagnostics when no other clipboard route can report success.

Related to anomalyco#59664.
@MBanucu

MBanucu commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — I merged MBanucu#1 into this branch via rebase as cc15c654, so it is part of this PR now.

Re-verified on Linux after the merge:

  • 12 clipboard tests pass, strict tsc and Prettier clean
  • real helper code: OSC 52 unknown surfaces the wl-copy/xclip error, supported still succeeds, and a failing wl-copy still falls through to a working xclip
  • built opencode v2.4.3+cc15c65: [Image 1] still pastes, and with DISPLAY unset plus failing helper shims the copy action reports Failed to copy session ID instead of the success toast

The PR description now covers the follow-up commit.

@SeifHediya

Copy link
Copy Markdown

Thanks for merging my follow-up. Could a maintainer weigh in on scope? Should this PR keep the Linux copy and paste fallbacks together, or would it make sense to restore v1’s text-copy fallback first and then review the paste fallback separately?

@MBanucu

MBanucu commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Happy to split this if that is what maintainers prefer — here is the breakdown so the scope call is easier.

v1's helper was text-copy only on the write side (wl-copy, then xclip, then xsel, gated on Bun.which), plus image reads through wl-paste/xclip. v2's native backend replaced both, which is how #66351 (image paste) and #59664 (false copy success) ended up in the same gap.

In this PR the two halves are already separable after cc15c65:

So "text copy first" would be a small, low-risk PR, but it would not close #66351 — that issue is entirely in the read/image path. My preference is to keep them together because the policy is one decision ("when the native backend reports unsupported/failed, try the desktop helpers"), the read half is what motivated the feature request, and the extra code is ~60 lines per side. If maintainers would rather review copy first, I can open the write half as its own PR against dev, keep this one read-only, and rebase whichever lands second.

Either way this stays draft until a maintainer signs off on the approach.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants