term39 1.5.1

A modern, retro-styled terminal multiplexer with a classic MS-DOS aesthetic
#[cfg(feature = "clipboard")]
use arboard::Clipboard;

/// Manages clipboard operations with system clipboard integration
pub struct ClipboardManager {
    #[cfg(feature = "clipboard")]
    clipboard: Option<Clipboard>,
    last_copied: Option<String>,
}

impl ClipboardManager {
    /// Create a new clipboard manager
    pub fn new() -> Self {
        #[cfg(feature = "clipboard")]
        let clipboard_instance = Clipboard::new().ok();

        Self {
            #[cfg(feature = "clipboard")]
            clipboard: clipboard_instance,
            last_copied: None,
        }
    }

    /// Copy text to clipboard
    pub fn copy(&mut self, text: String) -> Result<(), String> {
        if text.is_empty() {
            return Err("Cannot copy empty text".to_string());
        }

        // Store in internal buffer
        self.last_copied = Some(text.clone());

        // Try to copy to system clipboard if available
        #[cfg(feature = "clipboard")]
        if let Some(clipboard) = &mut self.clipboard {
            clipboard
                .set_text(text)
                .map_err(|e| format!("Failed to copy to system clipboard: {}", e))?;
        }

        Ok(())
    }

    /// Get text from clipboard (system or internal)
    pub fn paste(&mut self) -> Result<String, String> {
        // Try system clipboard first if available
        #[cfg(feature = "clipboard")]
        if let Some(clipboard) = &mut self.clipboard {
            if let Ok(text) = clipboard.get_text() {
                return Ok(text);
            }
        }

        // Fall back to internal buffer
        self.last_copied
            .clone()
            .ok_or_else(|| "Clipboard is empty".to_string())
    }

    /// Check if clipboard has content
    pub fn has_content(&self) -> bool {
        self.last_copied.is_some()
    }

    /// Clear clipboard
    pub fn clear(&mut self) {
        self.last_copied = None;
        #[cfg(feature = "clipboard")]
        if let Some(clipboard) = &mut self.clipboard {
            let _ = clipboard.clear();
        }
    }

    /// Get last copied text (internal buffer only)
    #[allow(dead_code)]
    pub fn last_copied(&self) -> Option<&str> {
        self.last_copied.as_deref()
    }
}

impl Default for ClipboardManager {
    fn default() -> Self {
        Self::new()
    }
}