haj 0.3.5

the beautiful, cross-distro package manager and tui.
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph, Wrap},
};

pub fn render_popup(f: &mut Frame) {
    let area = centered_rect(60, 80, f.area());

    f.render_widget(Clear, area);

    let block = Block::default()
        .title(" haj keybinds reference ")
        .title_alignment(Alignment::Center)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));

    let help_text = vec![
        Line::from(Span::styled(
            "dashboard",
            Style::default()
                .fg(Color::Magenta)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled(
            "─────────",
            Style::default().fg(Color::DarkGray),
        )),
        format_bind("tab / enter", "switch widget / open browser or news"),
        format_bind("/ / f", "open package browser"),
        format_bind("u", "system upgrade (sync + upgrade)"),
        format_bind("n / t / h / g", "news / stats / history / groups"),
        format_bind("o / c", "sweep orphans / clean cache"),
        format_bind("b / r", "toggle blahaj / refresh data"),
        Line::from(""),
        Line::from(Span::styled(
            "package browser",
            Style::default()
                .fg(Color::Magenta)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled(
            "───────────────",
            Style::default().fg(Color::DarkGray),
        )),
        format_bind("j / k / g / G", "move cursor / top / bottom"),
        format_bind("/ / esc", "search repos & aur / exit search"),
        format_bind("tab / S", "cycle filter / cycle sort"),
        format_bind("space / a / A", "toggle select / all visible / clear"),
        format_bind("i / r", "install / remove selected"),
        format_bind("1 - 4", "overview / deps / files / queue"),
        format_bind("ctrl+u / ctrl+d", "scroll details pane"),
        format_bind("R", "refresh package index"),
        Line::from(""),
        Line::from(Span::styled(
            "global",
            Style::default()
                .fg(Color::Magenta)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled("──────", Style::default().fg(Color::DarkGray))),
        format_bind("esc / ? / q", "back to dashboard / toggle help / quit"),
    ];

    let paragraph = Paragraph::new(help_text)
        .block(block)
        .wrap(Wrap { trim: false });

    f.render_widget(paragraph, area);
}

fn format_bind(key: &str, desc: &str) -> Line<'static> {
    Line::from(vec![
        Span::styled(format!("{key:<14}"), Style::default().fg(Color::Cyan)),
        Span::styled(desc.to_string(), Style::default().fg(Color::White)),
    ])
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints(
            [
                Constraint::Percentage((100 - percent_y) / 2),
                Constraint::Percentage(percent_y),
                Constraint::Percentage((100 - percent_y) / 2),
            ]
            .as_ref(),
        )
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints(
            [
                Constraint::Percentage((100 - percent_x) / 2),
                Constraint::Percentage(percent_x),
                Constraint::Percentage((100 - percent_x) / 2),
            ]
            .as_ref(),
        )
        .split(popup_layout[1])[1]
}