eazygit 0.5.1

A fast TUI for Git with staging, conflicts, rebase, and palette-first UX
Documentation
//! Terminal-specific optimization hints.
//!
//! Provides helpful guidance to users about how to get the best
//! visual experience in their specific terminal emulator.

use super::terminal_detect::TerminalType;

/// Configuration for terminal hints.
#[derive(Debug, Clone)]
pub struct HintsConfig {
    /// Whether to show hints at all
    pub enabled: bool,
    /// Path to file tracking which hints have been shown
    pub shown_hints_file: Option<std::path::PathBuf>,
}

impl Default for HintsConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            shown_hints_file: dirs::config_dir().map(|p| p.join("eazygit").join(".shown_hints")),
        }
    }
}

/// Get optimization hint for a specific terminal type.
pub fn get_background_hint(terminal: TerminalType) -> Option<String> {
    match terminal {
        TerminalType::ITerm2 => Some(
            "💡 Pro Tip: For smooth high-resolution backgrounds in iTerm2:\n\
             1. Go to Preferences > Profiles > Window > Background Image\n\
             2. Choose your image and set blending to 20-30%\n\
             3. Disable 'background_image_path' in eazygit config\n\
             \n\
             This gives you full-resolution backgrounds instead of cell-based rendering."
                .to_string()
        ),
        TerminalType::Kitty => Some(
            "💡 Pro Tip: For smooth high-resolution backgrounds in Kitty:\n\
             1. Add to ~/.config/kitty/kitty.conf:\n\
                background_image /path/to/your/image.jpg\n\
                background_opacity 0.3\n\
             2. Disable 'background_image_path' in eazygit config\n\
             3. Restart Kitty\n\
             \n\
             This gives you full-resolution backgrounds instead of cell-based rendering."
                .to_string()
        ),
        TerminalType::Wezterm => Some(
            "💡 Pro Tip: For smooth high-resolution backgrounds in WezTerm:\n\
             1. Add to ~/.wezterm.lua:\n\
                config.window_background_opacity = 0.9\n\
                config.window_background_image = '/path/to/image.jpg'\n\
             2. Disable 'background_image_path' in eazygit config\n\
             \n\
             This gives you full-resolution backgrounds instead of cell-based rendering."
                .to_string()
        ),
        TerminalType::Generic => None,
    }
}

/// Check if a hint has been shown before.
pub fn has_shown_hint(hint_id: &str, config: &HintsConfig) -> bool {
    if !config.enabled {
        return true; // Treat as "already shown" if hints disabled
    }
    
    let Some(path) = &config.shown_hints_file else {
        return false;
    };
    
    if !path.exists() {
        return false;
    }
    
    std::fs::read_to_string(path)
        .ok()
        .map(|content| content.lines().any(|line| line == hint_id))
        .unwrap_or(false)
}

/// Mark a hint as shown.
pub fn mark_hint_shown(hint_id: &str, config: &HintsConfig) -> std::io::Result<()> {
    if !config.enabled {
        return Ok(());
    }
    
    let Some(path) = &config.shown_hints_file else {
        return Ok(());
    };
    
    // Create parent directory if needed
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    
    // Append hint ID
    use std::io::Write;
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)?;
    
    writeln!(file, "{}", hint_id)?;
    Ok(())
}

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

    #[test]
    fn test_iterm2_hint_exists() {
        let hint = get_background_hint(TerminalType::ITerm2);
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("iTerm2"));
    }

    #[test]
    fn test_generic_has_no_hint() {
        let hint = get_background_hint(TerminalType::Generic);
        assert!(hint.is_none());
    }
}