From 48ab6eac21d6ebff36582f3ad99289a57ab0127c Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Fri, 5 Nov 2021 17:33:33 +0530 Subject: [PATCH] Add on_function key handler This adds `on_function` handler to handle F1-F12 keys. This also fixes issues with previously added handlers and adds a checklist for future additions. --- docs/en/src/configure-key-bindings.md | 9 ++++ docs/en/src/debug-key-bindings.md | 15 ++++--- src/app.rs | 10 +++-- src/config.rs | 64 ++++++++++++++++++++++++++- src/input.rs | 23 ++++++++-- 5 files changed, 106 insertions(+), 15 deletions(-) diff --git a/docs/en/src/configure-key-bindings.md b/docs/en/src/configure-key-bindings.md index 96d5f68..2caa9aa 100644 --- a/docs/en/src/configure-key-bindings.md +++ b/docs/en/src/configure-key-bindings.md @@ -16,6 +16,7 @@ Key bindings contains the following information: - [on_special_character][13] - [on_character][33] - [on_navigation][34] +- [on_function][35] - [default][14] ### on_key @@ -67,6 +68,13 @@ Type: nullable [Action][16] An action to perform if the keyboard input is a navigation key and is not mapped via the [on_key][10] field. +### on_function + +Type: nullable [Action][16] + +An action to perform if the keyboard input is a function key and is not mapped +via the [on_key][10] field. + ### default Type: nullable [Action][16] @@ -221,3 +229,4 @@ Visit [Awesome Plugins][27] for more [integration][28] options. [32]: #on_alphanumeric [33]: #on_character [34]: #on_navigation +[34]: #on_function diff --git a/docs/en/src/debug-key-bindings.md b/docs/en/src/debug-key-bindings.md index cccf864..a64319b 100644 --- a/docs/en/src/debug-key-bindings.md +++ b/docs/en/src/debug-key-bindings.md @@ -32,11 +32,6 @@ xplr.config.modes.custom.test = { { LogInfo = "on_key called" }, }, }, - f1 = { - messages = { - { LogInfo = "on_key called" }, - }, - }, tab = { messages = { { LogInfo = "on_key called" }, @@ -52,6 +47,11 @@ xplr.config.modes.custom.test = { "Terminate", }, }, + f1 = { + messages = { + { LogInfo = "on_key called" }, + }, + }, }, on_alphabet = { messages = { @@ -83,6 +83,11 @@ xplr.config.modes.custom.test = { { LogInfo = "on_navigation called" }, }, }, + on_function = { + messages = { + { LogInfo = "on_function called" }, + }, + }, default = { messages = { { LogInfo = "default called" }, diff --git a/src/app.rs b/src/app.rs index dd08b26..241704c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1642,6 +1642,12 @@ impl App { kb.on_alphabet.as_ref().map(|a| a.messages.clone()) } else if key.is_number() { kb.on_number.as_ref().map(|a| a.messages.clone()) + } else if key.is_special_character() { + kb.on_special_character.as_ref().map(|a| a.messages.clone()) + } else if key.is_navigation() { + kb.on_navigation.as_ref().map(|a| a.messages.clone()) + } else if key.is_function() { + kb.on_function.as_ref().map(|a| a.messages.clone()) } else { None } @@ -1649,8 +1655,6 @@ impl App { .or_else(|| { if key.is_alphanumeric() { kb.on_alphanumeric.as_ref().map(|a| a.messages.clone()) - } else if key.is_special_character() { - kb.on_special_character.as_ref().map(|a| a.messages.clone()) } else { None } @@ -1658,8 +1662,6 @@ impl App { .or_else(|| { if key.is_character() { kb.on_character.as_ref().map(|a| a.messages.clone()) - } else if key.is_navigation() { - kb.on_navigation.as_ref().map(|a| a.messages.clone()) } else { None } diff --git a/src/config.rs b/src/config.rs index ded5f5f..b5308b1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -291,8 +291,17 @@ pub struct KeyBindings { #[serde(default)] pub on_navigation: Option, + #[serde(default)] + pub on_function: Option, + #[serde(default)] pub default: Option, + // Checklist for adding field: + // - [ ] Update App::handle_key + // - [ ] Update KeyBindings::sanitized + // - [ ] Update Mode::help_menu + // - [ ] Update configure-key-bindings.md + // - [ ] Update debug-key-bindings.md } impl KeyBindings { @@ -308,12 +317,19 @@ impl KeyBindings { self.on_alphabet.and_then(|a| a.sanitized(read_only)); self.on_number = self.on_number.and_then(|a| a.sanitized(read_only)); + self.on_alphanumeric = + self.on_alphanumeric.and_then(|a| a.sanitized(read_only)); self.on_special_character = self .on_special_character .and_then(|a| a.sanitized(read_only)); + self.on_character = + self.on_character.and_then(|a| a.sanitized(read_only)); + self.on_navigation = + self.on_navigation.and_then(|a| a.sanitized(read_only)); + self.on_function = + self.on_function.and_then(|a| a.sanitized(read_only)); self.default = self.default.and_then(|a| a.sanitized(read_only)); }; - self } } @@ -404,11 +420,55 @@ impl Mode { }) }), ) + .chain( + self.key_bindings + .on_alphanumeric + .iter() + .map(|a| ("[0-Z]", a.help.clone())) + .filter_map(|(k, mh)| { + mh.map(|h| { + HelpMenuLine::KeyMap(k.into(), vec![], h) + }) + }), + ) .chain( self.key_bindings .on_special_character .iter() - .map(|a| ("[spcl chars]", a.help.clone())) + .map(|a| ("[^0-Z]", a.help.clone())) + .filter_map(|(k, mh)| { + mh.map(|h| { + HelpMenuLine::KeyMap(k.into(), vec![], h) + }) + }), + ) + .chain( + self.key_bindings + .on_character + .iter() + .map(|a| ("[*]", a.help.clone())) + .filter_map(|(k, mh)| { + mh.map(|h| { + HelpMenuLine::KeyMap(k.into(), vec![], h) + }) + }), + ) + .chain( + self.key_bindings + .on_navigation + .iter() + .map(|a| ("[nav]", a.help.clone())) + .filter_map(|(k, mh)| { + mh.map(|h| { + HelpMenuLine::KeyMap(k.into(), vec![], h) + }) + }), + ) + .chain( + self.key_bindings + .on_function + .iter() + .map(|a| ("[f1-f12]", a.help.clone())) .filter_map(|(k, mh)| { mh.map(|h| { HelpMenuLine::KeyMap(k.into(), vec![], h) diff --git a/src/input.rs b/src/input.rs index 444fe7e..c907212 100644 --- a/src/input.rs +++ b/src/input.rs @@ -449,9 +449,27 @@ impl Key { self.is_alphanumeric() || self.is_special_character() } + pub fn is_function(&self) -> bool { + matches!( + self, + Self::F1 + | Self::F2 + | Self::F3 + | Self::F4 + | Self::F5 + | Self::F6 + | Self::F7 + | Self::F8 + | Self::F9 + | Self::F10 + | Self::F11 + | Self::F12 + ) + } + pub fn is_navigation(&self) -> bool { matches!( - &self, + self, Self::Backspace | Self::Left | Self::Right @@ -464,9 +482,6 @@ impl Key { | Self::BackTab | Self::Delete | Self::Insert - | Self::Enter - | Self::Space - | Self::Tab | Self::Esc | Self::CtrlA | Self::CtrlB