Skip to main content

md_tui/boxes/
help_box.rs

1use ratatui::{
2    buffer::Buffer,
3    layout::Rect,
4    style::Stylize,
5    text::Text,
6    widgets::{Row, Table, Widget},
7};
8
9use crate::util::{Mode, colors::color_config, keys::KEY_CONFIG};
10
11#[derive(Debug, Clone, Copy, Default)]
12pub struct HelpBox {
13    mode: Mode,
14    expanded: bool,
15}
16
17impl HelpBox {
18    pub fn close(&mut self) {
19        self.expanded = false;
20    }
21
22    pub fn toggle(&mut self) {
23        self.expanded = !self.expanded;
24    }
25
26    pub fn set_mode(&mut self, mode: Mode) {
27        self.mode = mode;
28    }
29
30    #[must_use]
31    pub fn expanded(&self) -> bool {
32        self.expanded
33    }
34}
35
36impl Widget for HelpBox {
37    fn render(self, area: Rect, buf: &mut Buffer)
38    where
39        Self: Sized,
40    {
41        match self.mode {
42            Mode::View => render_markdown_help(self.expanded, area, buf),
43            Mode::FileTree => render_file_tree_help(self.expanded, area, buf),
44        }
45    }
46}
47
48fn render_file_tree_help(expanded: bool, area: Rect, buf: &mut Buffer) {
49    if !expanded {
50        let text = Text::from("? - Help")
51            .fg(color_config().help_title_color)
52            .bold();
53        text.render(area, buf);
54        return;
55    }
56
57    let header = Row::new(vec!["Key", "Action"]);
58
59    let key_actions = [
60        Row::new(vec![
61            format!("{} or \u{2193}", KEY_CONFIG.down),
62            "Move down".to_string(),
63        ]),
64        Row::new(vec![
65            format!("{} or \u{2191}", KEY_CONFIG.up),
66            "Move up".to_string(),
67        ]),
68        Row::new(vec![
69            format!("{} or \u{2190}", KEY_CONFIG.page_up),
70            "Go to previous page".to_string(),
71        ]),
72        Row::new(vec![
73            format!("{} or \u{2192}", KEY_CONFIG.page_down),
74            "Go to next page".to_string(),
75        ]),
76        Row::new(vec![
77            format!("{}", KEY_CONFIG.top),
78            "Move to first file".to_string(),
79        ]),
80        Row::new(vec![
81            format!("{}", KEY_CONFIG.bottom),
82            "Move to last file".to_string(),
83        ]),
84        Row::new(vec![
85            format!("/ or {}", KEY_CONFIG.search),
86            "Search".to_string(),
87        ]),
88        Row::new(vec!["\u{21b5}", "Open file"]),
89        Row::new(vec!["q", "Quit"]),
90    ];
91
92    let widths = [12, 20];
93
94    let table = Table::new(key_actions, widths)
95        .header(header.fg(color_config().table_header_fg_color))
96        .fg(color_config().help_fg_color);
97    table.render(area, buf);
98}
99
100fn render_markdown_help(expandend: bool, area: Rect, buf: &mut Buffer) {
101    if !expandend {
102        let text = Text::from("? - Help")
103            .fg(color_config().help_title_color)
104            .bold();
105        text.render(area, buf);
106        return;
107    }
108
109    let header = Row::new(vec!["Key", "Action"]);
110
111    let key_actions = [
112        Row::new(vec![
113            format!("{} or \u{2193}", KEY_CONFIG.down),
114            "Move down".to_string(),
115        ]),
116        Row::new(vec![
117            format!("{} or \u{2191}", KEY_CONFIG.up),
118            "Move up".to_string(),
119        ]),
120        Row::new(vec![
121            format!("{}", KEY_CONFIG.half_page_down),
122            "Move half page down".to_string(),
123        ]),
124        Row::new(vec![
125            format!("{}", KEY_CONFIG.half_page_up),
126            "Move half page up".to_string(),
127        ]),
128        Row::new(vec![
129            format!("{} or \u{2192}", KEY_CONFIG.page_down),
130            "Move full page down".to_string(),
131        ]),
132        Row::new(vec![
133            format!("{} or \u{2190}", KEY_CONFIG.page_up),
134            "Move full page up".to_string(),
135        ]),
136        Row::new(vec![
137            format!("{}", KEY_CONFIG.bottom),
138            "Move to bottom".to_string(),
139        ]),
140        Row::new(vec![
141            format!("{}", KEY_CONFIG.top),
142            "Move to top".to_string(),
143        ]),
144        Row::new(vec![
145            format!("/ or {}", KEY_CONFIG.search),
146            "Search".to_string(),
147        ]),
148        Row::new(vec![
149            format!("{}", KEY_CONFIG.back),
150            "Go back to previous file".to_string(),
151        ]),
152        Row::new(vec![
153            format!("{}", KEY_CONFIG.file_tree),
154            "To file tree".to_string(),
155        ]),
156        Row::new(vec![
157            format!("{}", KEY_CONFIG.select_link),
158            "Enter select mode".to_string(),
159        ]),
160        Row::new(vec!["\u{21b5}", "Open link/file"]),
161        Row::new(vec![
162            format!("{}", KEY_CONFIG.edit),
163            "Edit file".to_string(),
164        ]),
165        Row::new(vec!["q", "Quit"]),
166    ];
167
168    let widths = [12, 25];
169
170    let table = Table::new(key_actions, widths)
171        .header(header.fg(color_config().table_header_fg_color))
172        .fg(color_config().help_fg_color);
173
174    table.render(area, buf);
175}