aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/input
diff options
context:
space:
mode:
authordenis <denismaximov98@gmail.com>2021-04-22 11:45:29 +0300
committerdenis <denismaximov98@gmail.com>2021-04-22 11:45:29 +0300
commit8d98ca7da0bc278a832e4e7de23ff63a84deb529 (patch)
treee7fdfaa6c1ae25e7737dbb36bd1dbcb94d62ab47 /src/common/input
parentwip: fixing the first arrow (diff)
parentfix(cargo): reduce crate size by 4.8MB using `cargo diet`, to 77kB (diff)
downloadsl0ppij-8d98ca7da0bc278a832e4e7de23ff63a84deb529.tar.gz
sl0ppij-8d98ca7da0bc278a832e4e7de23ff63a84deb529.tar.xz
sl0ppij-8d98ca7da0bc278a832e4e7de23ff63a84deb529.zip
wip: merge main in
Diffstat (limited to 'src/common/input')
-rw-r--r--src/common/input/actions.rs4
-rw-r--r--src/common/input/config.rs26
-rw-r--r--src/common/input/handler.rs14
-rw-r--r--src/common/input/keybinds.rs69
-rw-r--r--src/common/input/unit/keybinds_test.rs31
5 files changed, 113 insertions, 31 deletions
diff --git a/src/common/input/actions.rs b/src/common/input/actions.rs
index 47a5f763..3bcadf15 100644
--- a/src/common/input/actions.rs
+++ b/src/common/input/actions.rs
@@ -33,6 +33,10 @@ pub enum Action {
ScrollUp,
/// Scroll down in focus pane.
ScrollDown,
+ /// Scroll up one page in focus pane.
+ PageScrollUp,
+ /// Scroll down one page in focus pane.
+ PageScrollDown,
/// Toggle between fullscreen focus pane and normal layout.
ToggleFocusFullscreen,
/// Open a new pane in the specified direction (relative to focus).
diff --git a/src/common/input/config.rs b/src/common/input/config.rs
index 4bb9e10f..542116d9 100644
--- a/src/common/input/config.rs
+++ b/src/common/input/config.rs
@@ -51,7 +51,6 @@ impl Config {
}
/// Deserializes from given path.
- #[allow(unused_must_use)]
pub fn new(path: &Path) -> ConfigResult {
match File::open(path) {
Ok(mut file) => {
@@ -80,20 +79,23 @@ impl Config {
/// Entry point of the configuration
#[cfg(not(test))]
- pub fn from_cli_config(cli_config: Option<ConfigCli>) -> ConfigResult {
+ pub fn from_cli_config(
+ location: Option<PathBuf>,
+ cli_config: Option<ConfigCli>,
+ ) -> ConfigResult {
+ if let Some(path) = location {
+ return Config::new(&path);
+ }
+
match cli_config {
Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()),
- Some(ConfigCli::Config { path, .. }) if path.is_some() => {
- Ok(Config::new(&path.unwrap())?)
- }
Some(_) | None => Ok(Config::from_default_path()?),
}
}
- //#[allow(unused_must_use)]
/// In order not to mess up tests from changing configurations
#[cfg(test)]
- pub fn from_cli_config(_: Option<ConfigCli>) -> ConfigResult {
+ pub fn from_cli_config(_: Option<PathBuf>, _: Option<ConfigCli>) -> ConfigResult {
Ok(Config::default())
}
}
@@ -139,19 +141,15 @@ mod config_test {
#[test]
fn clean_option_equals_default_config() {
- let no_file = PathBuf::from(r"../fixtures/config/config.yamlll");
- let cli_config = ConfigCli::Config {
- path: Some(no_file),
- clean: true,
- };
- let config = Config::from_cli_config(Some(cli_config)).unwrap();
+ let cli_config = ConfigCli::Config { clean: true };
+ let config = Config::from_cli_config(None, Some(cli_config)).unwrap();
let default = Config::default();
assert_eq!(config, default);
}
#[test]
fn no_config_option_file_equals_default_config() {
- let config = Config::from_cli_config(None).unwrap();
+ let config = Config::from_cli_config(None, None).unwrap();
let default = Config::default();
assert_eq!(config, default);
}
diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs
index 2484f08c..bbb594a3 100644
--- a/src/common/input/handler.rs
+++ b/src/common/input/handler.rs
@@ -59,9 +59,6 @@ impl InputHandler {
fn handle_input(&mut self) {
let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
err_ctx.add_call(ContextType::StdinHandler);
- self.send_pty_instructions.update(err_ctx);
- self.send_app_instructions.update(err_ctx);
- self.send_screen_instructions.update(err_ctx);
let keybinds = self.config.keybinds.clone();
let alt_left_bracket = vec![27, 91];
loop {
@@ -188,6 +185,16 @@ impl InputHandler {
.send(ScreenInstruction::ScrollDown)
.unwrap();
}
+ Action::PageScrollUp => {
+ self.send_screen_instructions
+ .send(ScreenInstruction::PageScrollUp)
+ .unwrap();
+ }
+ Action::PageScrollDown => {
+ self.send_screen_instructions
+ .send(ScreenInstruction::PageScrollDown)
+ .unwrap();
+ }
Action::ToggleFocusFullscreen => {
self.send_screen_instructions
.send(ScreenInstruction::ToggleActiveTerminalFullscreen)
@@ -298,6 +305,7 @@ pub fn get_mode_info(mode: InputMode) -> ModeInfo {
}
InputMode::Scroll => {
keybinds.push(("↓↑".to_string(), "Scroll".to_string()));
+ keybinds.push(("PgUp/PgDn".to_string(), "Scroll Page".to_string()));
}
InputMode::RenameTab => {
keybinds.push(("Enter".to_string(), "when done".to_string()));
diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs
index 8a064970..8e04c44a 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
@@ -301,6 +340,11 @@ impl Keybinds {
defaults.insert(Key::Char('j'), vec![Action::ScrollDown]);
defaults.insert(Key::Char('k'), vec![Action::ScrollUp]);
+ defaults.insert(Key::Ctrl('f'), vec![Action::PageScrollDown]);
+ defaults.insert(Key::Ctrl('b'), vec![Action::PageScrollUp]);
+ defaults.insert(Key::PageDown, vec![Action::PageScrollDown]);
+ defaults.insert(Key::PageUp, vec![Action::PageScrollUp]);
+
defaults.insert(Key::Down, vec![Action::ScrollDown]);
defaults.insert(Key::Up, vec![Action::ScrollUp]);
@@ -383,7 +427,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 +453,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());
+}