1use arboard::Clipboard;
4use std::sync::Mutex;
5
6pub struct ClipboardManager {
8 clipboard: Mutex<Clipboard>,
9}
10
11impl ClipboardManager {
12 pub fn new() -> Result<Self, arboard::Error> {
14 let clipboard = Clipboard::new()?;
15 Ok(Self {
16 clipboard: Mutex::new(clipboard),
17 })
18 }
19
20 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 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 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}