* chore(config): default kdl keybindings config * tests * work * refactor(config): move stuff around * work * tab merge layout * work * work * layouts working * work * layout tests * work * work * feat(parsing): kdl layouts without config * refactor(kdl): move stuff around * work * tests(layout): add cases and fix bugs * work * fix(kdl): various bugs * chore(layouts): move all layouts to kdl * feat(kdl): shared keybidns * fix(layout): do not count fixed panes toward percentile * fix(keybinds): missing keybinds and actions * fix(config): adjust default tips * refactor(config): move stuff around * fix(tests): make e2e tests pass * fix(kdl): add verbose parsing errors * fix(kdl): focused tab * fix(layout): corret default_tab_template behavior * style(code): fix compile warnings * feat(cli): send actions through the cli * fix(cli): exit only when action is done * fix(cli): open embedded pane from floating pane * fix(cli): send actions to other sessions * feat(cli): command alias * feat(converter): convert old config * feat(converter): convert old layout and theme files * feat(kdl): pretty errors * feat(client): convert old YAML files on startup * fix: various bugs and styling issues * fix: e2e tests * fix(screen): propagate errors after merge * style(clippy): lower clippy level * fix(tests): own session_name variable * style(fmt): rustfmt * fix(cli): various action fixes * style(fmt): rustfmt * fix(themes): loading of theme files * style(fmt): rustfmt * fix(tests): theme fixtures * fix(layouts): better errors on unknown nodes * fix(kdl): clarify valid node terminator error * fix(e2e): adjust close tab test * fix(e2e): adjust close tab test again * style(code): cleanup some comments
73 lines
2.5 KiB
Rust
73 lines
2.5 KiB
Rust
use std::collections::{BTreeMap, HashMap};
|
|
|
|
use super::actions::Action;
|
|
use crate::data::{InputMode, Key, KeybindsVec};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use std::fmt;
|
|
|
|
/// Used in the config struct
|
|
#[derive(Clone, PartialEq, Deserialize, Serialize, Default)]
|
|
pub struct Keybinds(pub HashMap<InputMode, HashMap<Key, Vec<Action>>>);
|
|
|
|
impl fmt::Debug for Keybinds {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
let mut stable_sorted = BTreeMap::new();
|
|
for (mode, keybinds) in self.0.iter() {
|
|
let mut stable_sorted_mode_keybinds = BTreeMap::new();
|
|
for (key, actions) in keybinds {
|
|
stable_sorted_mode_keybinds.insert(key, actions);
|
|
}
|
|
stable_sorted.insert(mode, stable_sorted_mode_keybinds);
|
|
}
|
|
write!(f, "{:#?}", stable_sorted)
|
|
}
|
|
}
|
|
|
|
impl Keybinds {
|
|
pub fn get_actions_for_key_in_mode(&self, mode: &InputMode, key: &Key) -> Option<&Vec<Action>> {
|
|
self.0
|
|
.get(mode)
|
|
.and_then(|normal_mode_keybindings| normal_mode_keybindings.get(key))
|
|
}
|
|
pub fn get_actions_for_key_in_mode_or_default_action(
|
|
&self,
|
|
mode: &InputMode,
|
|
key: &Key,
|
|
raw_bytes: Vec<u8>,
|
|
) -> Vec<Action> {
|
|
self.0
|
|
.get(mode)
|
|
.and_then(|normal_mode_keybindings| normal_mode_keybindings.get(key))
|
|
.cloned()
|
|
.unwrap_or_else(|| vec![self.default_action_for_mode(mode, raw_bytes)])
|
|
}
|
|
pub fn get_input_mode_mut(&mut self, input_mode: &InputMode) -> &mut HashMap<Key, Vec<Action>> {
|
|
self.0.entry(*input_mode).or_insert_with(HashMap::new)
|
|
}
|
|
pub fn default_action_for_mode(&self, mode: &InputMode, raw_bytes: Vec<u8>) -> Action {
|
|
match *mode {
|
|
InputMode::Normal | InputMode::Locked => Action::Write(raw_bytes),
|
|
InputMode::RenameTab => Action::TabNameInput(raw_bytes),
|
|
InputMode::RenamePane => Action::PaneNameInput(raw_bytes),
|
|
InputMode::EnterSearch => Action::SearchInput(raw_bytes),
|
|
_ => Action::NoOp,
|
|
}
|
|
}
|
|
pub fn to_keybinds_vec(&self) -> KeybindsVec {
|
|
let mut ret = vec![];
|
|
for (mode, mode_binds) in &self.0 {
|
|
let mut mode_binds_vec: Vec<(Key, Vec<Action>)> = vec![];
|
|
for (key, actions) in mode_binds {
|
|
mode_binds_vec.push((key.clone(), actions.clone()));
|
|
}
|
|
ret.push((*mode, mode_binds_vec))
|
|
}
|
|
ret
|
|
}
|
|
}
|
|
|
|
// The unit test location.
|
|
#[cfg(test)]
|
|
#[path = "./unit/keybinds_test.rs"]
|
|
mod keybinds_test;
|