eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Terminal capability detection for graphics protocols.

use std::env;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalType {
    /// iTerm2 on macOS - supports inline images
    ITerm2,
    /// Kitty terminal - supports Kitty graphics protocol
    Kitty,
    /// Wezterm - supports multiple protocols
    Wezterm,
    /// Fallback to cell-based rendering
    Generic,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphicsCapability {
    /// High-resolution inline images (iTerm2)
    ITerm2Inline,
    /// Kitty graphics protocol
    KittyGraphics,
    /// Cell-based color rendering (fallback)
    CellBased,
}

impl TerminalType {
    /// Detect the current terminal type from environment variables.
    pub fn detect() -> Self {
        // Check TERM_PROGRAM first (set by iTerm2, Wezterm, etc.)
        if let Ok(term_program) = env::var("TERM_PROGRAM") {
            match term_program.as_str() {
                "iTerm.app" => return Self::ITerm2,
                "WezTerm" => return Self::Wezterm,
                _ => {}
            }
        }

        // Check for Kitty
        if env::var("KITTY_WINDOW_ID").is_ok() {
            return Self::Kitty;
        }

        // Fallback
        Self::Generic
    }

    /// Get the best graphics capability for this terminal.
    pub fn graphics_capability(self) -> GraphicsCapability {
        match self {
            Self::ITerm2 => GraphicsCapability::ITerm2Inline,
            Self::Kitty => GraphicsCapability::KittyGraphics,
            Self::Wezterm => {
                // Wezterm supports both Kitty and iTerm2 protocols
                // Prefer Kitty as it's more feature-rich
                GraphicsCapability::KittyGraphics
            }
            Self::Generic => GraphicsCapability::CellBased,
        }
    }

    /// Human-readable name for logging.
    pub fn name(self) -> &'static str {
        match self {
            Self::ITerm2 => "iTerm2",
            Self::Kitty => "Kitty",
            Self::Wezterm => "Wezterm",
            Self::Generic => "Generic",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_terminal_detection() {
        // Just ensure it doesn't panic
        let term_type = TerminalType::detect();
        let capability = term_type.graphics_capability();
        println!("Detected: {:?}, Capability: {:?}", term_type, capability);
    }
}