Skip to main content

limit_cli/tui/input/
clipboard.rs

1//! Clipboard operations handler
2//!
3//! Handles copy/paste operations with platform-specific support.
4
5use crate::clipboard::ClipboardManager;
6use crate::error::CliError;
7
8/// Clipboard handler for copy/paste operations
9pub struct ClipboardHandler {
10    clipboard: Option<ClipboardManager>,
11}
12
13impl ClipboardHandler {
14    /// Create a new clipboard handler
15    pub fn new() -> Self {
16        let clipboard = match ClipboardManager::new() {
17            Ok(cb) => {
18                tracing::info!("Clipboard initialized successfully");
19                Some(cb)
20            }
21            Err(e) => {
22                tracing::warn!("Clipboard unavailable: {}", e);
23                None
24            }
25        };
26        Self { clipboard }
27    }
28
29    /// Check if clipboard is available
30    pub fn is_available(&self) -> bool {
31        self.clipboard.is_some()
32    }
33
34    /// Copy text to clipboard
35    pub fn copy(&self, text: &str) -> Result<(), CliError> {
36        if let Some(ref clipboard) = self.clipboard {
37            clipboard.set_text(text).map_err(|e| {
38                CliError::ConfigError(format!("Failed to copy to clipboard: {}", e))
39            })?;
40            tracing::debug!("Copied {} chars to clipboard", text.len());
41            Ok(())
42        } else {
43            Err(CliError::ConfigError("Clipboard not available".to_string()))
44        }
45    }
46
47    /// Paste text from clipboard
48    pub fn paste(&self) -> Result<String, CliError> {
49        if let Some(ref clipboard) = self.clipboard {
50            let text = clipboard.get_text().map_err(|e| {
51                CliError::ConfigError(format!("Failed to read from clipboard: {}", e))
52            })?;
53            tracing::debug!("Read {} chars from clipboard", text.len());
54            Ok(text)
55        } else {
56            Err(CliError::ConfigError("Clipboard not available".to_string()))
57        }
58    }
59}
60
61impl Default for ClipboardHandler {
62    fn default() -> Self {
63        Self::new()
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_clipboard_handler_creation() {
73        let handler = ClipboardHandler::new();
74        // Clipboard may or may not be available in test environment
75        assert!(handler.is_available() || !handler.is_available());
76    }
77
78    #[test]
79    fn test_clipboard_handler_default() {
80        let handler = ClipboardHandler::default();
81        assert!(handler.is_available() || !handler.is_available());
82    }
83}