5.4 KiB
Clipboard Copy via OSC52 — Design
Status: Approved, pending implementation plan
Date: 2026-07-15
Scope: src/app/state/misc.rs, src/controller/input.rs, src/main.rs,
src/ipc/protocol.rs
Context
There is no clipboard support anywhere in the TUI today, and mouse capture is enabled
(EnableMouseCapture in main.rs), which in most terminal emulators suppresses native
click-drag text selection unless the user holds a modifier — making an in-app copy action
more valuable than it would be in a plain scrollback. OSC52 is a terminal escape sequence
(\x1b]52;c;<base64>\x07) that asks the terminal emulator itself to set the system
clipboard; it needs no OS-level clipboard library (no X11/Wayland/win32 dependency) and
the base64 crate is already a dependency (used in service/oauth/pkce.rs), so no new
crate is needed for this feature.
Key architectural constraint discovered while designing this: controller::input::handle_key
runs on the daemon process in --daemon/--attach mode (main.rs:359, inside
handle_daemon_client), not on the process that owns the user's actual terminal. A raw
io::stdout() write inside handle_key would go to the headless daemon's stdout in that
mode, not the user's terminal. The copy action therefore can't write the escape sequence
directly from handle_key — it has to signal intent via state, and the terminal-owning
process (single-process run_loop_inner, or the attach client's loop) performs the actual
write.
Goals
Ctrl+Ycopies the most recentRole::Assistantmessage's raw text (not the rendered markdown spans) to the system clipboard via OSC52.- Works identically in single-process mode and in
--daemon/--attachmode. - No new dependency.
Non-goals
- No native clipboard fallback (e.g.
arboard) for terminals that don't honor OSC52 — unsupported terminals silently swallow the escape sequence; no error surfaces to the user beyond the optimistic "Copied to clipboard" toast (there's no ack mechanism in the OSC52 protocol to verify the terminal actually did it). - No copy-last-code-block variant — out of scope for this pass; the whole-message copy covers the common case and is simple to extend later if needed.
- No mouse-drag text selection — unrelated, much larger feature; not being built here.
State (misc.rs)
MiscStategainspub pending_clipboard_copy: Option<String>, initialized toNoneinMiscState::new().
input.rs
- New top-level arm alongside the existing
Ctrl+C/Ctrl+Dhandlers:KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL). It finds the last message instate.transcript_cache.messageswithrole == Role::Assistant:- If found:
state.misc.pending_clipboard_copy = Some(msg.content.clone()). - If not found: push an
Infotoast ("No assistant message to copy yet") and leavepending_clipboard_copyasNone. - Returns
Vec::new()— this is a direct state mutation insidehandle_key, matching the existingCtrl+Seditor-save precedent (main.rs's editor branch also mutates state/does I/O directly rather than going through anAction).
- If found:
OSC52 write helper (main.rs)
fn write_osc52(stdout: &mut impl Write, text: &str) -> io::Result<()> {
let b64 = base64::engine::general_purpose::STANDARD.encode(text);
write!(stdout, "\x1b]52;c;{b64}\x07")?;
stdout.flush()
}
Generic over impl Write so both the single-process loop (writing to io::stdout()) and
tests (writing to a Vec<u8> to assert the formatted sequence) can use it without a real
terminal.
Single-process mode (run_loop_inner)
After the existing for action in actions { apply_action(state, action); } block, add:
if let Some(text) = state.misc.pending_clipboard_copy.take() {
let _ = write_osc52(&mut io::stdout(), &text);
state.push_toast(Toast::new(ToastKind::Success, "Copied to clipboard".into()));
}
Daemon/attach mode
ipc/protocol.rs: addDaemonFrame::ClipboardCopy(String)(alongsideStateUpdate,StreamToken,SystemNote,Closed— sameSerialize/Deserializederive).handle_daemon_client(main.rs): after each branch that callshandle_key/apply_action(KeyPressandSubmit, the only two that can reach the input handler), before the existingsend_daemon_update(&mut conn, state)?;call, add:if let Some(text) = state.misc.pending_clipboard_copy.take() { conn.send(&DaemonFrame::ClipboardCopy(text))?; }- Attach-client loop (
main.rs, the function matching onDaemonFrame::StateUpdate/SystemNote/Closedaround line 573): add aDaemonFrame::ClipboardCopy(text) => { let _ = write_osc52(&mut io::stdout(), &text); client_state.push_toast(...); }arm, mirroring the existingSystemNotehandling but performing the actual terminal write since this process — not the daemon — owns the user's terminal.
Testing
Inline #[cfg(test)] mod tests per CLAUDE.md convention:
input.rs:Ctrl+Ywith a transcript containing multiple messages setspending_clipboard_copyto the last assistant message's content, ignoring later user/tool messages that might follow it; with no assistant message present, it pushes an info toast and leavespending_clipboard_copyasNone.main.rs:write_osc52writing into aVec<u8>buffer produces the exact expected\x1b]52;c;<base64>\x07byte sequence for a known input string.