diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2023-08-12 15:52:52 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2023-08-12 15:52:52 -0400 |
commit | 541f16850e33145ff657211e43a12f2327f188a6 (patch) | |
tree | 7f706b804c6e9d04a0425eadae628aef78108bd4 | |
parent | f9ebd1055b916532e307e1b64be296d74e760d0b (diff) |
add optional description to keymaps
-rw-r--r-- | src/commands/numbered_command.rs | 2 | ||||
-rw-r--r-- | src/commands/show_help.rs | 2 | ||||
-rw-r--r-- | src/commands/show_tasks.rs | 2 | ||||
-rw-r--r-- | src/config/keymap/keymapping.rs | 20 | ||||
-rw-r--r-- | src/event/process_event.rs | 4 | ||||
-rw-r--r-- | src/key_command/command_keybind.rs | 14 | ||||
-rw-r--r-- | src/run.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_help.rs | 4 |
8 files changed, 34 insertions, 16 deletions
diff --git a/src/commands/numbered_command.rs b/src/commands/numbered_command.rs index 5072e0a..266446a 100644 --- a/src/commands/numbered_command.rs +++ b/src/commands/numbered_command.rs @@ -52,7 +52,7 @@ pub fn numbered_command( prefix.push(c); } key => match keymap.default_view.get(&key) { - Some(CommandKeybind::SimpleKeybind(command)) => { + Some(CommandKeybind::SimpleKeybind { command, .. }) => { return command.numbered_execute(num_prefix, context, backend, keymap); } _ => { diff --git a/src/commands/show_help.rs b/src/commands/show_help.rs index fbf80a0..6702efd 100644 --- a/src/commands/show_help.rs +++ b/src/commands/show_help.rs @@ -48,7 +48,7 @@ pub fn help_loop( Event::Key(Key::Char('3')) => sort_by = 2, Event::Key(Key::Char('/')) => search_query.push('/'), event => { - if let Some(CommandKeybind::SimpleKeybind(command)) = + if let Some(CommandKeybind::SimpleKeybind { command, .. }) = keymap_t.help_view.get(&event) { match command { diff --git a/src/commands/show_tasks.rs b/src/commands/show_tasks.rs index ea5f310..e05edac 100644 --- a/src/commands/show_tasks.rs +++ b/src/commands/show_tasks.rs @@ -28,7 +28,7 @@ pub fn show_tasks( .message_queue_mut() .push_info(format!("Unmapped input: {}", key.to_string())); } - Some(CommandKeybind::SimpleKeybind(command)) => { + Some(CommandKeybind::SimpleKeybind { command, .. }) => { if let Command::ShowTasks = command { break; } diff --git a/src/config/keymap/keymapping.rs b/src/config/keymap/keymapping.rs index b81e4fc..6bccc2a 100644 --- a/src/config/keymap/keymapping.rs +++ b/src/config/keymap/keymapping.rs @@ -18,19 +18,20 @@ enum KeymapError { Conflict, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct CommandKeymap { pub command: String, + pub description: Option<String>, pub keys: Vec<String>, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct AppModeKeyMapping { #[serde(default)] pub keymap: Vec<CommandKeymap>, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct AppKeyMappingRaw { pub default_view: AppModeKeyMapping, pub task_view: AppModeKeyMapping, @@ -80,7 +81,8 @@ fn vec_to_map(vec: &[CommandKeymap]) -> HashMap<Event, CommandKeybind> { } let command_str = command.command(); - let result = insert_keycommand(&mut hashmap, command, &events); + let command_description = m.description.to_owned(); + let result = insert_keycommand(&mut hashmap, command, command_description, &events); match result { Ok(_) => {} Err(e) => match e { @@ -125,6 +127,7 @@ impl std::default::Default for AppKeyMapping { fn insert_keycommand( keymap: &mut KeyMapping, keycommand: Command, + description: Option<String>, events: &[Event], ) -> Result<(), KeymapError> { let num_events = events.len(); @@ -136,7 +139,10 @@ fn insert_keycommand( if num_events == 1 { match keymap.entry(event) { Entry::Occupied(_) => return Err(KeymapError::Conflict), - Entry::Vacant(entry) => entry.insert(CommandKeybind::SimpleKeybind(keycommand)), + Entry::Vacant(entry) => entry.insert(CommandKeybind::SimpleKeybind { + command: keycommand, + description: description, + }), }; return Ok(()); } @@ -144,13 +150,13 @@ fn insert_keycommand( match keymap.entry(event) { Entry::Occupied(mut entry) => match entry.get_mut() { CommandKeybind::CompositeKeybind(ref mut m) => { - insert_keycommand(m, keycommand, &events[1..]) + insert_keycommand(m, keycommand, description, &events[1..]) } _ => Err(KeymapError::Conflict), }, Entry::Vacant(entry) => { let mut new_map = KeyMapping::new(); - let result = insert_keycommand(&mut new_map, keycommand, &events[1..]); + let result = insert_keycommand(&mut new_map, keycommand, description, &events[1..]); if result.is_ok() { let composite_command = CommandKeybind::CompositeKeybind(new_map); entry.insert(composite_command); diff --git a/src/event/process_event.rs b/src/event/process_event.rs index 2b6b0ae..b7126bf 100644 --- a/src/event/process_event.rs +++ b/src/event/process_event.rs @@ -40,8 +40,8 @@ pub fn poll_event_until_simple_keybind<'a>( match event { Event::Key(Key::Esc) => return None, event => match keymap.get(&event) { - Some(CommandKeybind::SimpleKeybind(s)) => { - return Some(s); + Some(CommandKeybind::SimpleKeybind { command, .. }) => { + return Some(command); } Some(CommandKeybind::CompositeKeybind(m)) => { keymap = m; diff --git a/src/key_command/command_keybind.rs b/src/key_command/command_keybind.rs index bd58b18..97e5de3 100644 --- a/src/key_command/command_keybind.rs +++ b/src/key_command/command_keybind.rs @@ -3,14 +3,24 @@ use crate::key_command::Command; #[derive(Debug)] pub enum CommandKeybind { - SimpleKeybind(Command), + SimpleKeybind { + command: Command, + description: Option<String>, + }, CompositeKeybind(KeyMapping), } impl std::fmt::Display for CommandKeybind { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - CommandKeybind::SimpleKeybind(s) => write!(f, "{}", s), + CommandKeybind::SimpleKeybind { + command: _, + description: Some(desc), + } => write!(f, "{}", desc), + CommandKeybind::SimpleKeybind { + command: cmd, + description: None, + } => write!(f, "{}", cmd), CommandKeybind::CompositeKeybind(_) => write!(f, "..."), } } @@ -83,7 +83,7 @@ pub fn run_loop( .message_queue_mut() .push_info(format!("Unmapped input: {}", key.to_string())); } - Some(CommandKeybind::SimpleKeybind(command)) => { + Some(CommandKeybind::SimpleKeybind { command, .. }) => { if let Err(e) = command.execute(context, backend, &keymap_t) { context.message_queue_mut().push_error(e.to_string()); } diff --git a/src/ui/widgets/tui_help.rs b/src/ui/widgets/tui_help.rs index 5e62240..7616d09 100644 --- a/src/ui/widgets/tui_help.rs +++ b/src/ui/widgets/tui_help.rs @@ -124,7 +124,9 @@ pub fn get_raw_keymap_table<'a>( for (event, bind) in keymap.iter() { let key = key_event_to_string(event); let (command, comment) = match bind { - CommandKeybind::SimpleKeybind(command) => (format!("{}", command), command.comment()), + CommandKeybind::SimpleKeybind { command, .. } => { + (format!("{}", command), command.comment()) + } CommandKeybind::CompositeKeybind(sub_keymap) => { let mut sub_rows = get_raw_keymap_table(sub_keymap, "", sort_by); for _ in 0..sub_rows.len() { |