diff options
author | Shav Kinderlehrer <[email protected]> | 2024-03-06 14:14:07 -0500 |
---|---|---|
committer | Shav Kinderlehrer <[email protected]> | 2024-03-06 14:14:07 -0500 |
commit | c2acb446e7868b67e5743d54cee5c64c469e5a18 (patch) | |
tree | 0a0243b2d064fe2d1d6acf2a21ea93a40ad9a02a /src/components/global_keys.rs | |
parent | a5dbccee4f22de991e33449ae1d1835269c6075d (diff) | |
download | molehole-c2acb446e7868b67e5743d54cee5c64c469e5a18.tar.gz molehole-c2acb446e7868b67e5743d54cee5c64c469e5a18.zip |
Add keyboard commands
Diffstat (limited to 'src/components/global_keys.rs')
-rw-r--r-- | src/components/global_keys.rs | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/src/components/global_keys.rs b/src/components/global_keys.rs index e99e2e0..67b1b6a 100644 --- a/src/components/global_keys.rs +++ b/src/components/global_keys.rs @@ -4,41 +4,69 @@ use ratatui::widgets::*; use crate::app_action::AppAction; use crate::component::Component; +use crate::keys::key_commands::*; -#[derive(Default, Clone, Copy)] +#[derive(Default)] pub struct GlobalKeys { - should_show: bool, + pub key_commands: Vec<KeyCommand>, + + pub should_show: bool, + pub scroll: u16, } impl Component for GlobalKeys { + fn init(&mut self) -> eyre::Result<()> { + self.key_commands.push(KeyCommand { + key_code: "?".to_string(), + description: "Show help menu".to_string(), + action: None, + }); + + Ok(()) + } + fn handle_key_event( &mut self, key: KeyEvent, ) -> eyre::Result<Option<AppAction>> { if key.kind == KeyEventKind::Press { - return match key.code { - KeyCode::Char('q') => Ok(Some(AppAction::Quit)), - KeyCode::Char('?') => { - self.should_show = !self.should_show; - Ok(None) + for key_command in self.key_commands.iter_mut() { + if key_command.key_code == serialize_key_event(key) { + if serialize_key_event(key) == "?" { + self.should_show = !self.should_show; + } + + return Ok(key_command.action); } - _ => Ok(None), - }; + } } Ok(None) } fn render(&mut self, frame: &mut Frame, rect: Rect) -> eyre::Result<()> { - let horizontal_center = Layout::default() - .direction(Direction::Horizontal); - let block = Block::default() .title("Keyboard shortcuts") .borders(Borders::ALL); + let mut lines: Vec<Line> = vec![]; + for key_command in self.key_commands.iter_mut() { + let command = Span::from(key_command.key_code.clone()); + let description = + Span::from(key_command.description.clone()).italic(); + let spacer = Span::from(" "); + + let line = Line::from(vec![command, spacer, description]); + lines.push(line); + } + + let commands = Paragraph::new(lines) + .block(block) + .wrap(Wrap { trim: true }) + .scroll((self.scroll, 0)); + if self.should_show { - frame.render_widget(block, rect); + frame.render_widget(commands, rect); } Ok(()) |