use crate::theme::Theme;
use ratatui::{
Frame,
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Paragraph},
};
#[derive(Clone, Debug)]
pub struct HelpShortcut<'a> {
pub key: &'a str,
pub desc: &'a str,
}
impl<'a> HelpShortcut<'a> {
pub const fn new(key: &'a str, desc: &'a str) -> Self {
Self { key, desc }
}
}
pub struct HelpPageConfig<'a> {
pub title: &'a str,
pub title_icon: Option<&'a str>,
pub block_title: Option<&'a str>,
pub shortcuts: &'a [HelpShortcut<'a>],
pub footer_lines: Option<Vec<Line<'a>>>,
pub theme: &'a Theme,
}
pub fn draw_help_page(f: &mut Frame, area: Rect, config: &HelpPageConfig<'_>) {
let t = config.theme;
let mut lines: Vec<Line<'_>> = Vec::new();
let title_text = if let Some(icon) = config.title_icon {
format!(" {} {}", icon, config.title)
} else {
format!(" {}", config.title)
};
lines.push(Line::from(Span::styled(
title_text,
Style::default()
.fg(t.help_title)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
for shortcut in config.shortcuts {
lines.push(Line::from(vec![
Span::styled(shortcut.key.to_string(), Style::default().fg(t.help_key)),
Span::raw(shortcut.desc),
]));
}
if let Some(footer) = &config.footer_lines {
lines.extend(footer.iter().cloned());
}
let mut block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(t.help_title));
if let Some(bt) = config.block_title {
block = block.title(bt);
}
let help_widget = Paragraph::new(lines).block(block);
f.render_widget(help_widget, area);
}