Skip to main content

limit_cli/
clipboard.rs

1//! Clipboard operations for the TUI
2
3use arboard::Clipboard;
4use std::sync::Mutex;
5
6/// Thread-safe clipboard wrapper
7pub struct ClipboardManager {
8    clipboard: Mutex<Clipboard>,
9}
10
11impl ClipboardManager {
12    /// Create a new clipboard manager
13    pub fn new() -> Result<Self, arboard::Error> {
14        let clipboard = Clipboard::new()?;
15        Ok(Self {
16            clipboard: Mutex::new(clipboard),
17        })
18    }
19
20    /// Copy text to clipboard
21    pub fn set_text(&self, text: &str) -> Result<(), arboard::Error> {
22        let mut clipboard = self
23            .clipboard
24            .lock()
25            .map_err(|_| arboard::Error::ClipboardNotSupported)?;
26        clipboard.set_text(text.to_string())?;
27        Ok(())
28    }
29
30    /// Get text from clipboard
31    pub fn get_text(&self) -> Result<String, arboard::Error> {
32        let mut clipboard = self
33            .clipboard
34            .lock()
35            .map_err(|_| arboard::Error::ClipboardNotSupported)?;
36        clipboard.get_text()
37    }
38}
39
40impl Default for ClipboardManager {
41    fn default() -> Self {
42        Self::new().expect("Failed to initialize clipboard")
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_clipboard_manager_new() {
52        // This test may fail in CI without clipboard access
53        if let Ok(manager) = ClipboardManager::new() {
54            assert!(manager.set_text("test").is_ok());
55            assert_eq!(manager.get_text().unwrap(), "test");
56        }
57    }
58}