From a37d5fc1d19f5b195361ddb3ba045d848266285d Mon Sep 17 00:00:00 2001 From: Guillaume Ranquet Date: Fri, 5 Jan 2024 11:31:48 +0100 Subject: [PATCH] conf/shortcuts: implement a key to command mapping Permits users to map keys in their configuration file to an array of meli commands e.g: [shortcuts.listing] commands = [ { command = [ "tag remove trash", "flag unset trash" ], shortcut = "D" }, { command = [ "tag add trash", "flag set trash" ], shortcut = "d" } ] Signed-off-by: Guillaume Ranquet --- meli/docs/meli.conf.5 | 17 +++++++++++++++++ meli/src/conf/shortcuts.rs | 8 ++++++++ meli/src/contacts/list.rs | 17 +++++++++++++++++ meli/src/mail/compose.rs | 17 +++++++++++++++++ meli/src/mail/listing.rs | 19 +++++++++++++++++++ meli/src/mail/listing/conversations.rs | 17 +++++++++++++++++ meli/src/mail/view.rs | 17 +++++++++++++++++ meli/src/mail/view/thread.rs | 17 +++++++++++++++++ meli/src/utilities/pager.rs | 11 +++++++++++ 9 files changed, 140 insertions(+) diff --git a/meli/docs/meli.conf.5 b/meli/docs/meli.conf.5 index 72c14681..19aa6b16 100644 --- a/meli/docs/meli.conf.5 +++ b/meli/docs/meli.conf.5 @@ -1013,6 +1013,23 @@ exit_entry = 'i' .\" .sp .Pp +.Em commands +.sp +In addition, each shortcuts section supports a TOML array of commands to associate a key to an array of meli +.Em COMMAND +.sp +.\" +.\" +.\" +.Bd -literal +[shortcuts.listing] +commands = [ { command = [ "tag remove trash", "flag unset trash" ], shortcut = "D" }, + { command = [ "tag add trash", "flag set trash" ], shortcut = "d" } ] +.Ed +.\" +.\" +.\" +.Pp .Em general .Bl -tag -width 36n .It Ic toggle_help diff --git a/meli/src/conf/shortcuts.rs b/meli/src/conf/shortcuts.rs index 73c7f6fe..e5656291 100644 --- a/meli/src/conf/shortcuts.rs +++ b/meli/src/conf/shortcuts.rs @@ -88,6 +88,12 @@ impl DotAddressable for Shortcuts { } } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct CommandShortcut { + pub shortcut: Key, + pub command: Vec, +} + /// Create a struct holding all of a Component's shortcuts. #[macro_export] macro_rules! shortcut_key_values { @@ -100,6 +106,7 @@ macro_rules! shortcut_key_values { #[serde(default)] #[serde(rename = $cname)] pub struct $name { + pub commands: Vec, $(pub $fname : Key),* } @@ -122,6 +129,7 @@ macro_rules! shortcut_key_values { impl Default for $name { fn default() -> Self { Self { + commands : vec![], $($fname: $default),* } } diff --git a/meli/src/contacts/list.rs b/meli/src/contacts/list.rs index bc4eed07..094e1029 100644 --- a/meli/src/contacts/list.rs +++ b/meli/src/contacts/list.rs @@ -838,6 +838,23 @@ impl Component for ContactList { self.movement = Some(PageMovement::End); return true; } + UIEvent::Input(ref key) => { + return context + .settings + .shortcuts + .contact_list + .commands + .iter() + .any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context.replies.push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }) + } _ => {} } } diff --git a/meli/src/mail/compose.rs b/meli/src/mail/compose.rs index 261cf806..7e1e5b0b 100644 --- a/meli/src/mail/compose.rs +++ b/meli/src/mail/compose.rs @@ -1132,6 +1132,23 @@ impl Component for Composer { }; return true; } + UIEvent::Input(ref key) => { + return context + .settings + .shortcuts + .composing + .commands + .iter() + .any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context.replies.push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }) + } _ => {} } if self.cursor == Cursor::Headers diff --git a/meli/src/mail/listing.rs b/meli/src/mail/listing.rs index f5d1449e..cdd30680 100644 --- a/meli/src/mail/listing.rs +++ b/meli/src/mail/listing.rs @@ -1960,6 +1960,25 @@ impl Component for Listing { ))); return true; } + UIEvent::Input(ref key) => { + return context + .settings + .shortcuts + .listing + .commands + .iter() + .any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context + .replies + .push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }); + } _ => {} } } diff --git a/meli/src/mail/listing/conversations.rs b/meli/src/mail/listing/conversations.rs index c657c408..3a856259 100644 --- a/meli/src/mail/listing/conversations.rs +++ b/meli/src/mail/listing/conversations.rs @@ -1415,6 +1415,23 @@ impl Component for ConversationsListing { } _ => {} }, + UIEvent::Input(ref key) => { + return context + .settings + .shortcuts + .listing + .commands + .iter() + .any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context.replies.push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }) + } _ => {} } } diff --git a/meli/src/mail/view.rs b/meli/src/mail/view.rs index 5691f041..4c1288a6 100644 --- a/meli/src/mail/view.rs +++ b/meli/src/mail/view.rs @@ -789,6 +789,23 @@ impl Component for MailView { .push_back(UIEvent::Action(Tab(New(Some(Box::new(new_tab)))))); return true; } + UIEvent::Input(ref key) => { + return context + .settings + .shortcuts + .envelope_view + .commands + .iter() + .any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context.replies.push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }) + } _ => {} } false diff --git a/meli/src/mail/view/thread.rs b/meli/src/mail/view/thread.rs index 11c12ddb..2a3ec937 100644 --- a/meli/src/mail/view/thread.rs +++ b/meli/src/mail/view/thread.rs @@ -1103,6 +1103,23 @@ impl Component for ThreadView { } false } + UIEvent::Input(ref key) => { + return context + .settings + .shortcuts + .thread_view + .commands + .iter() + .any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context.replies.push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }) + } _ => { if self .entries diff --git a/meli/src/utilities/pager.rs b/meli/src/utilities/pager.rs index 70ac600c..d2530b9f 100644 --- a/meli/src/utilities/pager.rs +++ b/meli/src/utilities/pager.rs @@ -835,6 +835,17 @@ impl Component for Pager { String::new(), ))); } + UIEvent::Input(ref key) => { + return context.settings.shortcuts.pager.commands.iter().any(|cmd| { + if cmd.shortcut == *key { + for cmd in &cmd.command { + context.replies.push_back(UIEvent::Command(cmd.to_string())); + } + return true; + } + false + }) + } _ => {} } false