pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
Documentation
use crossterm::{
    event::{DisableMouseCapture, EnableMouseCapture},
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};

/// Result type alias for terminal operations.
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

/// What: Enter raw mode and switch to the alternate screen with mouse capture enabled.
///
/// Inputs:
/// - None
///
/// Output:
/// - `Ok(())` if the terminal was prepared; `Err` on I/O or terminal backend failure.
///
/// # Errors
///
/// Returns an error if raw mode cannot be enabled or the alternate screen cannot be entered.
pub fn setup_terminal() -> Result<()> {
    if std::env::var("PACSEA_TEST_HEADLESS").ok().as_deref() == Some("1") {
        // Skip raw TTY setup in headless/test mode
        // Explicitly disable mouse reporting to prevent mouse position escape sequences
        // from appearing in test output when mouse moves over terminal
        let _ = execute!(std::io::stdout(), DisableMouseCapture);
        return Ok(());
    }
    enable_raw_mode()?;
    execute!(std::io::stdout(), EnterAlternateScreen, EnableMouseCapture)?;
    Ok(())
}

/// What: Restore terminal to normal mode, leave the alternate screen, and disable mouse capture.
///
/// Inputs:
/// - None
///
/// Output:
/// - `Ok(())` when restoration succeeds; `Err` if underlying terminal operations fail.
///
/// # Errors
///
/// Returns an error if raw mode cannot be disabled or the alternate screen cannot be left.
pub fn restore_terminal() -> Result<()> {
    if std::env::var("PACSEA_TEST_HEADLESS").ok().as_deref() == Some("1") {
        // Skip terminal restore in headless/test mode
        return Ok(());
    }
    disable_raw_mode()?;
    execute!(std::io::stdout(), DisableMouseCapture, LeaveAlternateScreen)?;
    Ok(())
}