|
| 1 | +use base64::{engine::general_purpose::STANDARD, Engine}; |
| 2 | +use std::io::{self, Write}; |
| 3 | + |
| 4 | +/// Trait for clipboard implementations |
| 5 | +trait ClipboardBackend { |
| 6 | + fn set_text(&mut self, text: &str) -> Result<(), ClipboardError>; |
| 7 | +} |
| 8 | + |
| 9 | +/// Arboard-based clipboard implementation (system clipboard) |
| 10 | +struct ArboardClipboard { |
| 11 | + clipboard: arboard::Clipboard, |
| 12 | +} |
| 13 | + |
| 14 | +impl ArboardClipboard { |
| 15 | + fn new() -> Result<Self, arboard::Error> { |
| 16 | + Ok(Self { |
| 17 | + clipboard: arboard::Clipboard::new()?, |
| 18 | + }) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl ClipboardBackend for ArboardClipboard { |
| 23 | + fn set_text(&mut self, text: &str) -> Result<(), ClipboardError> { |
| 24 | + self.clipboard |
| 25 | + .set_text(text) |
| 26 | + .map_err(|e| ClipboardError::Arboard(e)) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// OSC 52-based clipboard implementation (terminal escape sequences) |
| 31 | +struct Osc52Clipboard; |
| 32 | + |
| 33 | +impl Osc52Clipboard { |
| 34 | + fn new() -> Self { |
| 35 | + Self |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl ClipboardBackend for Osc52Clipboard { |
| 40 | + fn set_text(&mut self, text: &str) -> Result<(), ClipboardError> { |
| 41 | + let encoded = STANDARD.encode(text); |
| 42 | + let osc52 = format!("\x1b]52;c;{}\x07", encoded); |
| 43 | + |
| 44 | + // Write directly to stderr to avoid interfering with UI |
| 45 | + io::stderr().write_all(osc52.as_bytes())?; |
| 46 | + io::stderr().flush()?; |
| 47 | + |
| 48 | + Ok(()) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// Main clipboard wrapper that manages different backend implementations |
| 53 | +pub(crate) struct Clipboard { |
| 54 | + backend: Box<dyn ClipboardBackend>, |
| 55 | +} |
| 56 | + |
| 57 | +impl Clipboard { |
| 58 | + /// Creates a new clipboard instance with the preferred backend. |
| 59 | + /// If use_osc52 is true, uses OSC 52 with arboard as fallback. |
| 60 | + /// Otherwise, uses only arboard. |
| 61 | + pub fn new(use_osc52: bool) -> Option<Self> { |
| 62 | + if use_osc52 { |
| 63 | + // Prefer OSC 52, fallback to arboard |
| 64 | + Some(Self { |
| 65 | + backend: Box::new(Osc52Clipboard::new()), |
| 66 | + }) |
| 67 | + } else { |
| 68 | + // Try arboard only |
| 69 | + ArboardClipboard::new() |
| 70 | + .inspect_err(|e| log::warn!("Couldn't initialize arboard clipboard: {e}")) |
| 71 | + .ok() |
| 72 | + .map(|cb| Self { |
| 73 | + backend: Box::new(cb), |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Sets text to clipboard using the configured backend. |
| 79 | + pub fn set_text(&mut self, text: String) -> Result<(), ClipboardError> { |
| 80 | + self.backend.set_text(&text) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[derive(Debug)] |
| 85 | +pub enum ClipboardError { |
| 86 | + Io(io::Error), |
| 87 | + Arboard(arboard::Error), |
| 88 | +} |
| 89 | + |
| 90 | +impl From<io::Error> for ClipboardError { |
| 91 | + fn from(err: io::Error) -> Self { |
| 92 | + ClipboardError::Io(err) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl std::fmt::Display for ClipboardError { |
| 97 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 98 | + match self { |
| 99 | + ClipboardError::Io(err) => write!(f, "IO error: {}", err), |
| 100 | + ClipboardError::Arboard(err) => write!(f, "Clipboard error: {}", err), |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl std::error::Error for ClipboardError {} |
0 commit comments