Skip to main content

codetether_agent/tui/
clipboard_text.rs

1//! Text clipboard helpers: system clipboard + OSC52 fallback.
2
3/// Copy text to clipboard via `arboard`, falling back to OSC52.
4/// Returns the method name on success.
5pub fn copy_text(text: &str) -> Result<&'static str, String> {
6    if text.trim().is_empty() {
7        return Err("empty text".to_string());
8    }
9    match arboard::Clipboard::new().and_then(|mut cb| cb.set_text(text.to_string())) {
10        Ok(()) => return Ok("system clipboard"),
11        Err(e) => {
12            tracing::debug!(error = %e, "arboard unavailable, trying OSC52");
13        }
14    }
15    osc52_copy(text).map_err(|e| format!("osc52: {e}"))?;
16    Ok("OSC52")
17}
18
19fn osc52_copy(text: &str) -> std::io::Result<()> {
20    use base64::Engine;
21    use std::io::Write;
22    let b64 = base64::engine::general_purpose::STANDARD.encode(text.as_bytes());
23    let seq = format!("\u{1b}]52;c;{b64}\u{07}");
24    let mut stdout = std::io::stdout();
25    crossterm::execute!(stdout, crossterm::style::Print(seq))?;
26    stdout.flush()
27}