aboutsummaryrefslogtreecommitdiff
path: root/src/keys/key_commands.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/keys/key_commands.rs')
-rw-r--r--src/keys/key_commands.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/keys/key_commands.rs b/src/keys/key_commands.rs
new file mode 100644
index 0000000..6124560
--- /dev/null
+++ b/src/keys/key_commands.rs
@@ -0,0 +1,56 @@
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
+
+use crate::app_action::AppAction;
+
+#[derive(Default, Clone)]
+pub struct KeyCommand {
+ pub key_code: String,
+ pub description: String,
+ pub action: Option<AppAction>,
+}
+
+impl std::fmt::Display for KeyCommand {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}\t{}", self.key_code, self.description)
+ }
+}
+
+pub fn serialize_key_event(event: KeyEvent) -> String {
+ let mut modifiers = Vec::with_capacity(3);
+ if event.modifiers.intersects(KeyModifiers::CONTROL) {
+ modifiers.push("ctrl");
+ }
+ if event.modifiers.intersects(KeyModifiers::SUPER)
+ || event.modifiers.intersects(KeyModifiers::HYPER)
+ || event.modifiers.intersects(KeyModifiers::META)
+ {
+ modifiers.push("super");
+ }
+ if event.modifiers.intersects(KeyModifiers::ALT) {
+ modifiers.push("alt");
+ }
+
+ let char;
+ let key = match event.code {
+ KeyCode::Backspace => "del",
+ KeyCode::Enter => "enter",
+ KeyCode::Left => "left",
+ KeyCode::Right => "right",
+ KeyCode::Up => "up",
+ KeyCode::Down => "down",
+ KeyCode::Tab => "tab",
+ KeyCode::Delete => "del",
+ KeyCode::Char(c) if c == ' ' => "space",
+ KeyCode::Char(c) => {
+ char = c.to_string();
+ &char
+ }
+ KeyCode::Esc => "esc",
+ _ => "",
+ };
+ let separator = if modifiers.len() > 0 { "-" } else { "" };
+ let serialized_event =
+ format!("{}{}{}", modifiers.join("-"), separator, key);
+
+ serialized_event
+}