diff options
Diffstat (limited to 'pkgs/by-name/ri/river-mk-keymap/src/key_map/mod.rs')
-rw-r--r-- | pkgs/by-name/ri/river-mk-keymap/src/key_map/mod.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/pkgs/by-name/ri/river-mk-keymap/src/key_map/mod.rs b/pkgs/by-name/ri/river-mk-keymap/src/key_map/mod.rs new file mode 100644 index 00000000..84a16c9d --- /dev/null +++ b/pkgs/by-name/ri/river-mk-keymap/src/key_map/mod.rs @@ -0,0 +1,79 @@ +use std::{collections::HashMap, fmt::Display, ops::Deref, str::FromStr}; + +use anyhow::Context; +use keymaps::{key_repr::Key, map_tree::MapTrie}; +use serde::{Deserialize, Serialize}; + +pub mod commands; + +#[derive(Deserialize, Serialize, Debug)] +#[allow(clippy::module_name_repetitions)] +pub struct RawKeyMap(HashMap<Key, KeyConfig>); + +#[derive(Clone, Deserialize, Serialize, Debug, PartialEq, PartialOrd)] +/// What values to use for: `riverctl <map_mode> <mode> <mods> <key> <command..>` +pub struct KeyConfig { + command: Vec<String>, + + #[serde(default = "default_mode")] + modes: Vec<String>, + + #[serde(default = "MapMode::default")] + map_mode: MapMode, +} + +impl FromStr for KeyMap { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + let raw: RawKeyMap = + serde_json::from_str(s).context("Failed to parse the keymap config file as json.")?; + let mut out = MapTrie::<KeyConfig>::new(); + for (key, value) in raw.0 { + out.insert(&[key], value.clone()) + .with_context(|| format!("Failed to insert mapping {key} -> {value}"))?; + } + + Ok(Self(out)) + } +} +impl Display for KeyConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.command.join(" ").as_str()) + } +} + +fn default_mode() -> Vec<String> { + vec!["normal".to_owned()] +} + +#[derive(Copy, Deserialize, Serialize, Debug, Clone, Default, PartialEq, PartialOrd)] +enum MapMode { + #[default] + Map, + MapMouse, + Unmap, +} + +impl Display for MapMode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + <Self as std::fmt::Debug>::fmt(self, f) + } +} + +#[derive(Debug)] +pub struct KeyMap(MapTrie<KeyConfig>); + +impl Display for KeyMap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +impl Deref for KeyMap { + type Target = MapTrie<KeyConfig>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} |