src/config/keybindings.rs
57 lines · 1842 bytes
1use super::ConfigTrait;
2use crate::global::keyaction::KeyAction;
3use crossterm::event::KeyCode;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use typemap::Key;
7
8// The outer HashMap<KeyCode, T> gets a value for whatever code is being pressed
9// The inner HashMap<u8, KeyAction> gets the action using a u8 representing a key modifier
10// The u8 for each modifier key can be found in https://docs.rs/crossterm/latest/src/crossterm/event.rs.html#587-603
11// The sum of u8 for each key modifier would mean having all of those modifiers at the same time
12#[derive(Serialize, Deserialize, Clone)]
13pub struct KeyBindingsConfig(pub HashMap<KeyCode, HashMap<u8, KeyAction>>);
14
15impl ConfigTrait for KeyBindingsConfig {
16 const LABEL: &'static str = "keybindings";
17}
18
19impl Key for KeyBindingsConfig {
20 type Value = Self;
21}
22
23impl Default for KeyBindingsConfig {
24 fn default() -> Self {
25 let mut map = HashMap::new();
26
27 let mut left = HashMap::new();
28 left.insert(0, KeyAction::MoveLeft);
29 map.insert(KeyCode::Left, left);
30
31 let mut right = HashMap::new();
32 right.insert(0, KeyAction::MoveRight);
33 map.insert(KeyCode::Right, right);
34
35 let mut up = HashMap::new();
36 up.insert(0, KeyAction::MoveUp);
37 map.insert(KeyCode::Up, up);
38
39 let mut down = HashMap::new();
40 down.insert(0, KeyAction::MoveDown);
41 map.insert(KeyCode::Down, down);
42
43 let mut enter = HashMap::new();
44 enter.insert(0, KeyAction::Select);
45 map.insert(KeyCode::Enter, enter);
46
47 let mut escape = HashMap::new();
48 escape.insert(0, KeyAction::Deselect);
49 map.insert(KeyCode::Esc, escape);
50
51 let mut q = HashMap::new();
52 q.insert(0, KeyAction::Exit);
53 map.insert(KeyCode::Char('q'), q);
54
55 Self(map)
56 }
57}