Browse Source

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 <granquet@baylibre.com>
Guillaume Ranquet 1 year ago
parent
commit
a37d5fc1

+ 17 - 0
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

+ 8 - 0
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<String>,
+}
+
 /// 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<CommandShortcut>,
             $(pub $fname : Key),*
         }
 
@@ -122,6 +129,7 @@ macro_rules! shortcut_key_values {
         impl Default for $name {
             fn default() -> Self {
                 Self {
+                    commands : vec![],
                     $($fname: $default),*
                 }
             }

+ 17 - 0
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
+                        })
+                }
                 _ => {}
             }
         }

+ 17 - 0
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

+ 19 - 0
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
+                            });
+                    }
                     _ => {}
                 }
             }

+ 17 - 0
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
+                        })
+                }
                 _ => {}
             }
         }

+ 17 - 0
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

+ 17 - 0
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

+ 11 - 0
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