diff options
| author | a-kenji <aks.kenji@protonmail.com> | 2021-03-12 22:05:41 +0100 |
|---|---|---|
| committer | a-kenji <aks.kenji@protonmail.com> | 2021-03-12 22:05:41 +0100 |
| commit | a86d8c216186030754a42968768f2af6a131d90d (patch) | |
| tree | 4e445340579663436ba6fb564a6b8403abd8fdb7 /src/common/input | |
| parent | Merge 'upstream/main' into config-file (diff) | |
| download | sl0ppij-a86d8c216186030754a42968768f2af6a131d90d.tar.gz sl0ppij-a86d8c216186030754a42968768f2af6a131d90d.tar.xz sl0ppij-a86d8c216186030754a42968768f2af6a131d90d.zip | |
Add example config file.
Diffstat (limited to 'src/common/input')
| -rw-r--r-- | src/common/input/config.rs | 124 | ||||
| -rw-r--r-- | src/common/input/handler.rs | 9 | ||||
| -rw-r--r-- | src/common/input/keybinds.rs | 6 | ||||
| -rw-r--r-- | src/common/input/mod.rs | 1 | ||||
| -rw-r--r-- | src/common/input/ut/config_test.rs | 31 | ||||
| -rw-r--r-- | src/common/input/ut/keybinds_test.rs | 75 |
6 files changed, 204 insertions, 42 deletions
diff --git a/src/common/input/config.rs b/src/common/input/config.rs new file mode 100644 index 00000000..b63ccef1 --- /dev/null +++ b/src/common/input/config.rs @@ -0,0 +1,124 @@ +//! Deserializes configuration options. +use std::error; +use std::fmt::{self, Display}; +use std::fs::File; +use std::io::{self, Read}; +use std::path::{Path, PathBuf}; + +use super::keybinds::{Keybinds, KeybindsFromYaml}; +use crate::utils::logging::*; + +use directories_next::ProjectDirs; +use serde::Deserialize; + +/// Intermediate deserialisation config struct +#[derive(Debug, Deserialize)] +pub struct ConfigFromYaml { + pub keybinds: Option<KeybindsFromYaml>, +} + +/// Main configuration. +#[derive(Debug, Clone, PartialEq)] +pub struct Config { + pub keybinds: Keybinds, +} + +#[derive(Debug)] +pub enum ConfigError { + // Deserialisation error + Serde(serde_yaml::Error), + // Io error + Io(io::Error), + // Io error with path context + IoPath(io::Error, PathBuf), +} + +impl Default for Config { + fn default() -> Self { + let keybinds = Keybinds::default(); + Config { keybinds } + } +} + +impl Config { + /// Uses defaults, but lets config override them. + pub fn from_yaml(yaml_config: &str) -> Result<Config, ConfigError> { + let config_from_yaml: ConfigFromYaml = serde_yaml::from_str(&yaml_config)?; + let keybinds = Keybinds::get_default_keybinds_with_config(config_from_yaml.keybinds); + Ok(Config { keybinds }) + } + + /// Deserializes from given path. + /// The allow is here, because rust assumes there is no + /// error handling when logging the error to the log file. + #[allow(unused_must_use)] + pub fn new(path: &Path) -> Result<Config, ConfigError> { + match File::open(path) { + Ok(mut file) => { + let mut yaml_config = String::new(); + file.read_to_string(&mut yaml_config) + .map_err(|e| ConfigError::IoPath(e, path.to_path_buf()))?; + Ok(Config::from_yaml(&yaml_config)?) + } + Err(e) => { + debug_log_to_file(format!( + "{}\nUsing the default configuration!", + ConfigError::IoPath(e, path.to_path_buf()) + )); + Ok(Config::default()) + } + } + } + + /// Deserializes the config from an optional path, or a platform specific path, + /// merges the default configuration - options take precedence. + pub fn from_option_or_default(option: Option<PathBuf>) -> Result<Config, ConfigError> { + if let Some(config_path) = option { + Ok(Config::new(&config_path)?) + } else { + let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); + let mut config_path: PathBuf = project_dirs.config_dir().to_owned(); + config_path.push("config.yaml"); + Ok(Config::new(&config_path)?) + } + } +} + +impl Display for ConfigError { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + match self { + ConfigError::Io(ref err) => write!(formatter, "IoError: {}", err), + ConfigError::IoPath(ref err, ref path) => { + write!(formatter, "IoError: {}, File: {}", err, path.display(),) + } + ConfigError::Serde(ref err) => write!(formatter, "Deserialisation error: {}", err), + } + } +} + +impl std::error::Error for ConfigError { + fn cause(&self) -> Option<&dyn error::Error> { + match *self { + ConfigError::Io(ref err) => Some(err), + ConfigError::IoPath(ref err, _) => Some(err), + ConfigError::Serde(ref err) => Some(err), + } + } +} + +impl From<io::Error> for ConfigError { + fn from(err: io::Error) -> ConfigError { + ConfigError::Io(err) + } +} + +impl From<serde_yaml::Error> for ConfigError { + fn from(err: serde_yaml::Error) -> ConfigError { + ConfigError::Serde(err) + } +} + +// The unit test location. +#[cfg(test)] +#[path = "./ut/config_test.rs"] +mod config_test; diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 862cc0d2..dc9fa382 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -2,7 +2,7 @@ use super::actions::Action; use super::keybinds::Keybinds; -use crate::common::config::Config; +use crate::common::input::config::Config; use crate::common::{update_state, AppInstruction, AppState, SenderWithContext, OPENCALLS}; use crate::errors::ContextType; use crate::os_input_output::OsApi; @@ -273,18 +273,25 @@ impl InputHandler { pub enum InputMode { /// In `Normal` mode, input is always written to the terminal, except for the shortcuts leading /// to other modes + #[serde(alias = "normal")] Normal, /// In `Locked` mode, input is always written to the terminal and all shortcuts are disabled /// except the one leading back to normal mode + #[serde(alias = "locked")] Locked, /// `Resize` mode allows resizing the different existing panes. + #[serde(alias = "resize")] Resize, /// `Pane` mode allows creating and closing panes, as well as moving between them. + #[serde(alias = "pane")] Pane, /// `Tab` mode allows creating and closing tabs, as well as moving between them. + #[serde(alias = "tab")] Tab, /// `Scroll` mode allows scrolling up and down within a pane. + #[serde(alias = "scroll")] Scroll, + #[serde(alias = "renametab")] RenameTab, } diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index d7f64652..0c37693c 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -56,7 +56,7 @@ impl Keybinds { fn merge_keybinds(&self, other: Keybinds) -> Keybinds { let mut keybinds = Keybinds::new(); - for mode in self.0.keys().chain(other.0.keys()) { + for mode in InputMode::iter() { let mut mode_keybinds = ModeKeybinds::new(); if let Some(keybind) = self.0.get(&mode) { mode_keybinds.0.extend(keybind.0.clone()); @@ -64,7 +64,9 @@ impl Keybinds { if let Some(keybind) = other.0.get(&mode) { mode_keybinds.0.extend(keybind.0.clone()); } - keybinds.0.insert(mode.clone(), mode_keybinds); + if !mode_keybinds.0.is_empty() { + keybinds.0.insert(mode, mode_keybinds); + } } keybinds } diff --git a/src/common/input/mod.rs b/src/common/input/mod.rs index a7dca7ed..6589cac2 100644 --- a/src/common/input/mod.rs +++ b/src/common/input/mod.rs @@ -1,5 +1,6 @@ //! The way terminal input is handled. pub mod actions; +pub mod config; pub mod handler; pub mod keybinds; diff --git a/src/common/input/ut/config_test.rs b/src/common/input/ut/config_test.rs index 17649d14..eb4bc960 100644 --- a/src/common/input/ut/config_test.rs +++ b/src/common/input/ut/config_test.rs @@ -1,12 +1,8 @@ //! For Configuration Options use super::super::config::*; -use crate::common::input::actions::*; -use crate::common::input::keybinds::*; use std::path::PathBuf; -use termion::event::Key; - #[test] fn no_config_file_equals_default_config() { let no_file = PathBuf::from(r"../fixtures/config/config.yamlll"); @@ -21,30 +17,3 @@ fn no_config_option_file_equals_default_config() { let default = Config::default(); assert_eq!(config, default); } - -//#[test] -//fn multiple_keys_mapped_to_one_action() { -//let options = r" -//--- -//keybindings: -//Normal: -//- ? - F: 6 -//- F: 7 -//- F: 8 -//: - {GoToTab: 5} -//"; - -//let config_options = Config::from_yaml(&options).unwrap(); - -//assert_eq!(config_options, config_options) -//} - -//#[test] -//fn merge_keybinds_merges(){ -//let mut self_keybinds = Keybinds::new(); -//let mut self_mode_keybinds = ModeKeybinds::new(); -//self_mode_keybinds.0.insert(Key::F(1), vec![Action::GoToTab(5)]); -//let mut other_keybinds = Keybinds::new(); -//let mut self_mode_keybinds = ModeKeybinds::new(); -//let mut expected = Keybinds::new(); -//} diff --git a/src/common/input/ut/keybinds_test.rs b/src/common/input/ut/keybinds_test.rs index 44472985..2b306bc3 100644 --- a/src/common/input/ut/keybinds_test.rs +++ b/src/common/input/ut/keybinds_test.rs @@ -60,17 +60,76 @@ fn merge_keybinds_merges() { .0 .insert(InputMode::Resize, mode_keybinds_other.clone()); let mut keybinds_expected = Keybinds::new(); - keybinds_expected.0.insert( - InputMode::Normal, - mode_keybinds_self - ); - keybinds_expected.0.insert( - InputMode::Resize, - mode_keybinds_other - ); + keybinds_expected + .0 + .insert(InputMode::Normal, mode_keybinds_self); + keybinds_expected + .0 + .insert(InputMode::Resize, mode_keybinds_other); assert_eq!( keybinds_expected, keybinds_self.merge_keybinds(keybinds_other) ) } + +#[test] +fn merge_keybinds_overwrites_same_keys() { + let mut mode_keybinds_self = ModeKeybinds::new(); + mode_keybinds_self.0.insert(Key::F(1), vec![Action::NoOp]); + mode_keybinds_self.0.insert(Key::F(2), vec![Action::NoOp]); + mode_keybinds_self.0.insert(Key::F(3), vec![Action::NoOp]); + let mut mode_keybinds_other = ModeKeybinds::new(); + mode_keybinds_other + .0 + .insert(Key::F(1), vec![Action::GoToTab(1)]); + mode_keybinds_other + .0 + .insert(Key::F(2), vec![Action::GoToTab(2)]); + mode_keybinds_other + .0 + .insert(Key::F(3), vec![Action::GoToTab(3)]); + let mut keybinds_self = Keybinds::new(); + keybinds_self + .0 + .insert(InputMode::Normal, mode_keybinds_self.clone()); + let mut keybinds_other = Keybinds::new(); + keybinds_other + .0 + .insert(InputMode::Normal, mode_keybinds_other.clone()); + let mut keybinds_expected = Keybinds::new(); + keybinds_expected + .0 + .insert(InputMode::Normal, mode_keybinds_other); + + assert_eq!( + keybinds_expected, + keybinds_self.merge_keybinds(keybinds_other) + ) +} + +#[test] +fn from_keyaction_from_yaml_to_mode_keybindings() { + let actions = vec![Action::NoOp, Action::GoToTab(1)]; + let keyaction = KeyActionFromYaml { + action: actions.clone(), + key: vec![Key::F(1), Key::Backspace, Key::Char('t')], + }; + + let mut expected = ModeKeybinds::new(); + expected.0.insert(Key::F(1), actions.clone()); + expected.0.insert(Key::Backspace, actions.clone()); + expected.0.insert(Key::Char('t'), actions); + + 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'), ], +//}; +//} |
