aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authora-kenji <aks.kenji@protonmail.com>2021-04-17 15:21:12 +0200
committerGitHub <noreply@github.com>2021-04-17 15:21:12 +0200
commitd5be5e3a849e5bb67c664c0dac35686a24fb6b13 (patch)
tree48f61f1329a5990b8ca021789e58fd16a981ba40 /src
parentMerge branch 'main' of github.com:zellij-org/zellij (diff)
parentAdd Unbind for Keybindings (#241) (diff)
downloadsl0ppij-d5be5e3a849e5bb67c664c0dac35686a24fb6b13.tar.gz
sl0ppij-d5be5e3a849e5bb67c664c0dac35686a24fb6b13.tar.xz
sl0ppij-d5be5e3a849e5bb67c664c0dac35686a24fb6b13.zip
Merge pull request #275 from a-kenji/unbind-default-keys
Unbind default keys
Diffstat (limited to 'src')
-rw-r--r--src/common/input/keybinds.rs64
-rw-r--r--src/common/input/unit/keybinds_test.rs31
2 files changed, 81 insertions, 14 deletions
diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs
index 8a064970..6239475e 100644
--- a/src/common/input/keybinds.rs
+++ b/src/common/input/keybinds.rs
@@ -7,14 +7,30 @@ use serde::Deserialize;
use strum::IntoEnumIterator;
use zellij_tile::data::*;
+/// Used in the config struct
#[derive(Clone, Debug, PartialEq)]
pub struct Keybinds(HashMap<InputMode, ModeKeybinds>);
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ModeKeybinds(HashMap<Key, Vec<Action>>);
/// Intermediate struct used for deserialisation
+/// Used in the config file.
#[derive(Clone, Debug, PartialEq, Deserialize)]
-pub struct KeybindsFromYaml(HashMap<InputMode, Vec<KeyActionFromYaml>>);
+pub struct KeybindsFromYaml {
+ #[serde(flatten)]
+ keybinds: HashMap<InputMode, Vec<KeyActionUnbind>>,
+ #[serde(default)]
+ unbind: Unbind,
+}
+
+/// Intermediate enum used for deserialisation
+#[derive(Clone, Debug, PartialEq, Deserialize)]
+#[serde(untagged)]
+enum KeyActionUnbind {
+ KeyAction(KeyActionFromYaml),
+ // TODO: use the enum
+ //Unbind(UnbindFromYaml),
+}
/// Intermediate struct used for deserialisation
#[derive(Clone, Debug, PartialEq, Deserialize)]
@@ -23,6 +39,22 @@ pub struct KeyActionFromYaml {
key: Vec<Key>,
}
+/// Intermediate struct used for deserialisation
+#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
+struct UnbindFromYaml {
+ unbind: Unbind,
+}
+
+/// List of keys, for which to disable their respective default actions
+/// `All` is a catch all, and will disable the default actions for all keys.
+#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
+#[serde(untagged)]
+enum Unbind {
+ All(bool),
+ // TODO@a-kenji: use the enum
+ //Keys(Vec<Key>),
+}
+
impl Default for Keybinds {
fn default() -> Keybinds {
let mut defaults = Keybinds::new();
@@ -41,9 +73,16 @@ impl Keybinds {
Keybinds(HashMap::<InputMode, ModeKeybinds>::new())
}
- pub fn get_default_keybinds_with_config(keybinds: Option<KeybindsFromYaml>) -> Keybinds {
- let default_keybinds = Keybinds::default();
- if let Some(keybinds) = keybinds {
+ pub fn get_default_keybinds_with_config(from_yaml: Option<KeybindsFromYaml>) -> Keybinds {
+ let default_keybinds = match from_yaml.clone() {
+ Some(keybinds) => match keybinds.unbind {
+ Unbind::All(true) => Keybinds::new(),
+ Unbind::All(false) => Keybinds::default(),
+ },
+ None => Keybinds::default(),
+ };
+
+ if let Some(keybinds) = from_yaml {
default_keybinds.merge_keybinds(Keybinds::from(keybinds))
} else {
default_keybinds
@@ -383,7 +422,7 @@ impl From<KeybindsFromYaml> for Keybinds {
for mode in InputMode::iter() {
let mut mode_keybinds = ModeKeybinds::new();
- for key_action in keybinds_from_yaml.0.get(&mode).iter() {
+ for key_action in keybinds_from_yaml.keybinds.get(&mode).iter() {
for keybind in key_action.iter() {
mode_keybinds = mode_keybinds.merge(ModeKeybinds::from(keybind.clone()));
}
@@ -409,6 +448,21 @@ impl From<KeyActionFromYaml> for ModeKeybinds {
}
}
+// Currently an enum for future use
+impl From<KeyActionUnbind> for ModeKeybinds {
+ fn from(key_action_unbind: KeyActionUnbind) -> ModeKeybinds {
+ match key_action_unbind {
+ KeyActionUnbind::KeyAction(key_action) => ModeKeybinds::from(key_action),
+ }
+ }
+}
+
+impl Default for Unbind {
+ fn default() -> Unbind {
+ Unbind::All(false)
+ }
+}
+
// The unit test location.
#[cfg(test)]
#[path = "./unit/keybinds_test.rs"]
diff --git a/src/common/input/unit/keybinds_test.rs b/src/common/input/unit/keybinds_test.rs
index 6411d017..e0d78b62 100644
--- a/src/common/input/unit/keybinds_test.rs
+++ b/src/common/input/unit/keybinds_test.rs
@@ -124,12 +124,25 @@ fn from_keyaction_from_yaml_to_mode_keybindings() {
assert_eq!(expected, ModeKeybinds::from(keyaction));
}
-//#[test]
-//fn from_keybinds_from_yaml_to_keybinds(){
-//let mut keybinds_from_yaml = KeybindsFromYaml(HashMap<InputMode, Vec<KeyActionFromYaml>>);
-//let actions = vec![Action::NoOp, Action::GoToTab(1), ];
-//let keyaction = KeyActionFromYaml {
-//action : actions.clone(),
-//key : vec![ Key::F(1), Key::Backspace , Key::Char('t'), ],
-//};
-//}
+#[test]
+fn toplevel_unbind_unbinds_all() {
+ let from_yaml = KeybindsFromYaml {
+ unbind: Unbind::All(true),
+ keybinds: HashMap::new(),
+ };
+
+ let keybinds_from_yaml = Keybinds::get_default_keybinds_with_config(Some(from_yaml));
+
+ assert_eq!(keybinds_from_yaml, Keybinds::new());
+}
+
+fn no_unbind_unbinds_none() {
+ let from_yaml = KeybindsFromYaml {
+ unbind: Unbind::All(false),
+ keybinds: HashMap::new(),
+ };
+
+ let keybinds_from_yaml = Keybinds::get_default_keybinds_with_config(Some(from_yaml));
+
+ assert_eq!(keybinds_from_yaml, Keybinds::new());
+}