use key config in help component

pull/33/head
Takayuki Maeda 3 years ago
parent b84cdc1cb1
commit 19737074c9

@ -1,5 +1,6 @@
use super::{Component, DrawableComponent, EventState};
use crate::components::command::CommandInfo;
use crate::config::KeyConfig;
use crate::event::Key;
use anyhow::Result;
use itertools::Itertools;
@ -17,6 +18,7 @@ pub struct HelpComponent {
cmds: Vec<CommandInfo>,
visible: bool,
selection: u16,
key_config: KeyConfig,
}
impl DrawableComponent for HelpComponent {
@ -73,23 +75,18 @@ impl Component for HelpComponent {
fn event(&mut self, key: Key) -> Result<EventState> {
if self.visible {
match key {
Key::Esc => {
self.hide();
return Ok(EventState::Consumed);
}
Key::Char('j') => {
self.move_selection(true);
return Ok(EventState::Consumed);
}
Key::Char('k') => {
self.move_selection(false);
return Ok(EventState::Consumed);
}
_ => (),
if key == self.key_config.exit_popup {
self.hide();
return Ok(EventState::Consumed);
} else if key == self.key_config.move_down {
self.move_selection(true);
return Ok(EventState::Consumed);
} else if key == self.key_config.move_up {
self.move_selection(false);
return Ok(EventState::Consumed);
}
return Ok(EventState::NotConsumed);
} else if let Key::Char('?') = key {
} else if key == self.key_config.open_help {
self.show()?;
return Ok(EventState::Consumed);
}
@ -108,11 +105,12 @@ impl Component for HelpComponent {
}
impl HelpComponent {
pub const fn new() -> Self {
pub const fn new(key_config: KeyConfig) -> Self {
Self {
cmds: vec![],
visible: false,
selection: 0,
key_config,
}
}
@ -168,12 +166,12 @@ impl HelpComponent {
#[cfg(test)]
mod test {
use super::{Color, CommandInfo, HelpComponent, Modifier, Span, Spans, Style};
use super::{Color, CommandInfo, HelpComponent, KeyConfig, Modifier, Span, Spans, Style};
#[test]
fn test_get_text() {
let width = 3;
let mut component = HelpComponent::new();
let mut component = HelpComponent::new(KeyConfig::default());
component.set_cmds(vec![
CommandInfo::new(crate::components::command::move_left("h"), true, true),
CommandInfo::new(crate::components::command::move_right("l"), true, true),

Loading…
Cancel
Save