From 3e0174cc4a973b9f3f6324af66b1b9ed9b6a7361 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Thu, 4 Mar 2021 15:00:23 +0100 Subject: [PATCH 01/23] poke at creating Macros - create config struct - create config errors - create macro struct TODO create --config option for the file --- src/common/config.rs | 129 +++++++++++++++++++++++++++++++++++ src/common/input/actions.rs | 6 +- src/common/input/keybinds.rs | 2 +- src/common/input/macros.rs | 12 ++++ src/common/input/mod.rs | 1 + src/common/mod.rs | 3 + 6 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 src/common/config.rs create mode 100644 src/common/input/macros.rs diff --git a/src/common/config.rs b/src/common/config.rs new file mode 100644 index 00000000..2ce25bff --- /dev/null +++ b/src/common/config.rs @@ -0,0 +1,129 @@ +//! Deserializes configuration options. +use std; +use std::collections::HashMap; +use std::error; +use std::fmt::{self, Display}; +use std::fs::File; +use std::io::{self,Read}; +use std::path::PathBuf; + +use super::input::{keybinds,handler}; + +use serde::Deserialize; + +/// Intermediate struct +//pub struct KeybingsFromYaml { + +//} + + +/// Intermediate struct +//#[derive(Debug, Deserialize)] +pub struct ConfigFromYaml { + keybinds: HashMap>, +} + +///// Deserialized config state +#[derive(Debug, Clone, Default, Deserialize)] +pub struct Config { + keybinds: Vec, +} + +#[derive(Debug)] +pub enum ConfigError { + // from the serde documentation + // https://serde.rs/error-handling.html + // One or more variants that can be created by data structures through the + // `ser::Error` and `de::Error` traits. For example the Serialize impl for + // Mutex might return an error because the mutex is poisoned, or the + // Deserialize impl for a struct may return an error because a required + // field is missing. + //Message(String), + // serde_yaml error + Serde(serde_yaml::Error), + //Eof, + // io::Error + Io(io::Error) +} + +impl Config { + /// Deserializes from given path + pub fn new(path: &PathBuf) -> Result { + let config_deserialized: Config; + let mut config_string = String::new(); + + match File::open(path) { + Ok(mut file) => { + file.read_to_string(&mut config_string)?; + config_deserialized = serde_yaml::from_str(&config_string)?; + } + Err(_) => { + // TODO logging, if a file is not found + // at an expected position - should not + // panic @a-kenji + config_deserialized = Config::default(); + } + } + Ok(config_deserialized) + } +} + +//impl de::Error for ConfigError { + //fn custom(msg: T) -> Self { + //ConfigError::Message(msg.to_string()) + //} +//} + +impl Display for ConfigError { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + match self { + //ConfigError::Message(msg) => formatter.write_str(msg), + //ConfigError::Eof => formatter.write_str("unexpected end of input"), + // + ConfigError::Io(ref err) => write!(formatter, "Io error: {}", err), + ConfigError::Serde(ref err) => write!(formatter, "Serde error: {}", err), + /* and so forth */ + } + } +} + +impl std::error::Error for ConfigError { + fn description(&self) -> &str { + match *self { + //ConfigError::Message(ref err) => err, + ConfigError::Io(ref err) => err.to_string().as_str(), + ConfigError::Serde(ref err) => err.to_string().as_str(), + } + } + + fn cause(&self) -> Option<&dyn error::Error> { + match *self { + // N.B. Both of these implicitly cast `err` from their concrete + // types (either `&io::Error` or `&num::ParseIntError`) + // to a trait object `&Error`. This works because both error types + // implement `Error`. + ConfigError::Io(ref err) => Some(err), + //ConfigError::Message(ref err) => Some(err), + ConfigError::Serde(ref err) => Some(err), + } +} +} + +impl From for ConfigError { + fn from(err: io::Error) -> ConfigError { + ConfigError::Io(err) + } +} + +impl From for ConfigError { + fn from(err: serde_yaml::Error) -> ConfigError { + ConfigError::Serde(err) + } +} + +//impl From for ConfigError { + //fn from(err: de::Error::Message) -> ConfigError { + //ConfigError::Message(err) + //} +//} + diff --git a/src/common/input/actions.rs b/src/common/input/actions.rs index fe5e2dad..d03ca859 100644 --- a/src/common/input/actions.rs +++ b/src/common/input/actions.rs @@ -1,9 +1,11 @@ //! Definition of the actions that can be bound to keys. use super::handler; +//use super::macros; +use serde::Deserialize; /// The four directions (left, right, up, down). -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] pub enum Direction { Left, Right, @@ -12,7 +14,7 @@ pub enum Direction { } /// Actions that can be bound to keys. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize)] pub enum Action { /// Quit Zellij. Quit, diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index e56488b4..0ea0695f 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -8,7 +8,7 @@ use std::collections::HashMap; use strum::IntoEnumIterator; use termion::event::Key; -type Keybinds = HashMap; +pub type Keybinds = HashMap; type ModeKeybinds = HashMap>; /// Populates the default hashmap of keybinds. diff --git a/src/common/input/macros.rs b/src/common/input/macros.rs new file mode 100644 index 00000000..cbd14332 --- /dev/null +++ b/src/common/input/macros.rs @@ -0,0 +1,12 @@ +//! Use a list of commands and execute them in a +//! defined predictable order. + +use super::actions::Action; + +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize)] +pub struct Macro { + name: Option, + sequence: Vec +} diff --git a/src/common/input/mod.rs b/src/common/input/mod.rs index 20c30dae..438df224 100644 --- a/src/common/input/mod.rs +++ b/src/common/input/mod.rs @@ -3,3 +3,4 @@ pub mod actions; pub mod handler; pub mod keybinds; +pub mod macros; diff --git a/src/common/mod.rs b/src/common/mod.rs index f78303ea..5ba2e15d 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,3 +1,4 @@ +pub mod config; pub mod command_is_executing; pub mod errors; pub mod input; @@ -26,6 +27,7 @@ use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; use wasmer_wasi::{Pipe, WasiState}; use crate::cli::CliArgs; +use crate::common::config::Config; use crate::layout::Layout; use command_is_executing::CommandIsExecuting; use errors::{AppContext, ContextType, ErrorContext, PluginContext, PtyContext, ScreenContext}; @@ -47,6 +49,7 @@ pub enum ApiCommand { #[derive(Debug, Clone, Default)] pub struct AppState { pub input_mode: InputMode, + pub config : Config, } // FIXME: Make this a method on the big `Communication` struct, so that app_tx can be extracted From c644a1c2437ac439425eaa185d4f69911cf5ee45 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 5 Mar 2021 19:23:06 +0100 Subject: [PATCH 02/23] poking --- assets/completions/_zellij | 2 + assets/completions/zellij.bash | 10 +++- assets/completions/zellij.fish | 1 + config.yaml | 8 ++++ src/cli.rs | 4 ++ src/common/config.rs | 85 +++++++++++++++++----------------- src/common/input/handler.rs | 19 ++++++-- src/common/input/keybinds.rs | 2 +- src/common/input/macros.rs | 4 +- src/common/mod.rs | 20 ++++++-- 10 files changed, 103 insertions(+), 52 deletions(-) create mode 100644 config.yaml diff --git a/assets/completions/_zellij b/assets/completions/_zellij index 64bd538c..cd8dce56 100644 --- a/assets/completions/_zellij +++ b/assets/completions/_zellij @@ -22,6 +22,8 @@ _zellij() { '--max-panes=[Maximum panes on screen, caution: opening more panes will close old ones]' \ '-l+[Path to a layout yaml file]' \ '--layout=[Path to a layout yaml file]' \ +'-c+[Path to the configuration yaml file]' \ +'--config=[Path to the configuration yaml file]' \ '-m[Send "move focused pane" to active zellij session]' \ '--move-focus[Send "move focused pane" to active zellij session]' \ '-d[]' \ diff --git a/assets/completions/zellij.bash b/assets/completions/zellij.bash index a921e7c9..0ede5cce 100644 --- a/assets/completions/zellij.bash +++ b/assets/completions/zellij.bash @@ -20,7 +20,7 @@ _zellij() { case "${cmd}" in zellij) - opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --layout " + opts=" -m -d -h -V -s -o -l -c --move-focus --debug --help --version --split --open-file --max-panes --layout --config " if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -55,6 +55,14 @@ _zellij() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + --config) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -c) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; *) COMPREPLY=() ;; diff --git a/assets/completions/zellij.fish b/assets/completions/zellij.fish index e902823f..eaba9fa4 100644 --- a/assets/completions/zellij.fish +++ b/assets/completions/zellij.fish @@ -2,6 +2,7 @@ complete -c zellij -n "__fish_use_subcommand" -s s -l split -d 'Send "split (dir complete -c zellij -n "__fish_use_subcommand" -s o -l open-file -d 'Send "open file in new pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -l max-panes -d 'Maximum panes on screen, caution: opening more panes will close old ones' complete -c zellij -n "__fish_use_subcommand" -s l -l layout -d 'Path to a layout yaml file' +complete -c zellij -n "__fish_use_subcommand" -s c -l config -d 'Path to the configuration yaml file' complete -c zellij -n "__fish_use_subcommand" -s m -l move-focus -d 'Send "move focused pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -s d -l debug complete -c zellij -n "__fish_use_subcommand" -s h -l help -d 'Prints help information' diff --git a/config.yaml b/config.yaml new file mode 100644 index 00000000..b1657775 --- /dev/null +++ b/config.yaml @@ -0,0 +1,8 @@ +--- +macro: + {name:"closePane", sequence: [NewPane: Right,]} + +keybinds: + Normal: + Backspace: [NewPane:, NewPane:,] + {F: 1}: [NewPane:,] diff --git a/src/cli.rs b/src/cli.rs index 6e384e16..67a92196 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -24,6 +24,10 @@ pub struct CliArgs { #[structopt(short, long)] pub layout: Option, + /// Path to the configuration yaml file + #[structopt(short, long)] + pub config: Option, + #[structopt(short, long)] pub debug: bool, } diff --git a/src/common/config.rs b/src/common/config.rs index 2ce25bff..705c1592 100644 --- a/src/common/config.rs +++ b/src/common/config.rs @@ -1,14 +1,15 @@ //! Deserializes configuration options. use std; -use std::collections::HashMap; +//use std::collections::HashMap; use std::error; use std::fmt::{self, Display}; use std::fs::File; -use std::io::{self,Read}; +use std::io::{self, Read}; use std::path::PathBuf; -use super::input::{keybinds,handler}; +use super::input::{keybinds, macros}; +use directories_next::ProjectDirs; use serde::Deserialize; /// Intermediate struct @@ -16,17 +17,17 @@ use serde::Deserialize; //} - /// Intermediate struct -//#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize)] pub struct ConfigFromYaml { - keybinds: HashMap>, + keybinds: Option, + macros: Option>, } ///// Deserialized config state #[derive(Debug, Clone, Default, Deserialize)] pub struct Config { - keybinds: Vec, + pub keybinds: keybinds::Keybinds, } #[derive(Debug)] @@ -43,37 +44,52 @@ pub enum ConfigError { Serde(serde_yaml::Error), //Eof, // io::Error - Io(io::Error) + Io(io::Error), } impl Config { /// Deserializes from given path - pub fn new(path: &PathBuf) -> Result { - let config_deserialized: Config; + pub fn new(path: &PathBuf) -> Result { + let config: Config; + let config_deserialized: ConfigFromYaml; let mut config_string = String::new(); + // TODO fix this unwrap match File::open(path) { Ok(mut file) => { file.read_to_string(&mut config_string)?; config_deserialized = serde_yaml::from_str(&config_string)?; + config = Config { + keybinds: config_deserialized + .keybinds + .unwrap_or_else(|| keybinds::get_default_keybinds().unwrap()), + } } - Err(_) => { + Err(e) => { // TODO logging, if a file is not found // at an expected position - should not // panic @a-kenji - config_deserialized = Config::default(); + eprintln!("{}", e); + config = Config::default(); } } - Ok(config_deserialized) + Ok(config) + } + + pub fn from_option_or_default(option: Option) -> Result { + let config; + if let Some(config_path) = option { + config = Config::new(&config_path)?; + } else { + let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); + //let config_path = PathBuf::from(project_dirs.config_dir().as_os_str()); + let config_path = project_dirs.config_dir().to_owned().into(); + config = Config::new(&config_path)?; + } + return Ok(config); } } -//impl de::Error for ConfigError { - //fn custom(msg: T) -> Self { - //ConfigError::Message(msg.to_string()) - //} -//} - impl Display for ConfigError { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match self { @@ -88,25 +104,17 @@ impl Display for ConfigError { } impl std::error::Error for ConfigError { - fn description(&self) -> &str { + fn cause(&self) -> Option<&dyn error::Error> { match *self { - //ConfigError::Message(ref err) => err, - ConfigError::Io(ref err) => err.to_string().as_str(), - ConfigError::Serde(ref err) => err.to_string().as_str(), + // N.B. Both of these implicitly cast `err` from their concrete + // types (either `&io::Error` or `&num::ParseIntError`) + // to a trait object `&Error`. This works because both error types + // implement `Error`. + ConfigError::Io(ref err) => Some(err), + //ConfigError::Message(ref err) => Some(err), + ConfigError::Serde(ref err) => Some(err), } } - - fn cause(&self) -> Option<&dyn error::Error> { - match *self { - // N.B. Both of these implicitly cast `err` from their concrete - // types (either `&io::Error` or `&num::ParseIntError`) - // to a trait object `&Error`. This works because both error types - // implement `Error`. - ConfigError::Io(ref err) => Some(err), - //ConfigError::Message(ref err) => Some(err), - ConfigError::Serde(ref err) => Some(err), - } -} } impl From for ConfigError { @@ -120,10 +128,3 @@ impl From for ConfigError { ConfigError::Serde(err) } } - -//impl From for ConfigError { - //fn from(err: de::Error::Message) -> ConfigError { - //ConfigError::Message(err) - //} -//} - diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 5c4f1971..9d8b2cf2 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -2,6 +2,7 @@ use super::actions::Action; use super::keybinds::get_default_keybinds; +use crate::common::config::Config; use crate::common::{update_state, AppInstruction, AppState, SenderWithContext, OPENCALLS}; use crate::errors::ContextType; use crate::os_input_output::OsApi; @@ -22,6 +23,7 @@ struct InputHandler { /// The current input mode mode: InputMode, os_input: Box, + config: Config, command_is_executing: CommandIsExecuting, send_screen_instructions: SenderWithContext, send_pty_instructions: SenderWithContext, @@ -34,6 +36,7 @@ impl InputHandler { fn new( os_input: Box, command_is_executing: CommandIsExecuting, + config: Config, send_screen_instructions: SenderWithContext, send_pty_instructions: SenderWithContext, send_plugin_instructions: SenderWithContext, @@ -42,6 +45,7 @@ impl InputHandler { InputHandler { mode: InputMode::Normal, os_input, + config, command_is_executing, send_screen_instructions, send_pty_instructions, @@ -59,6 +63,8 @@ impl InputHandler { self.send_app_instructions.update(err_ctx); self.send_screen_instructions.update(err_ctx); if let Ok(keybinds) = get_default_keybinds() { + let mut merged_keybinds = keybinds; + merged_keybinds.extend(self.config.keybinds.clone().into_iter()); 'input_loop: loop { //@@@ I think this should actually just iterate over stdin directly let stdin_buffer = self.os_input.read_from_stdin(); @@ -76,9 +82,14 @@ impl InputHandler { // been revised. Sorry about this (@categorille) if { let mut should_break = false; - for action in - key_to_actions(&key, raw_bytes, &self.mode, &keybinds) - { + // Hacked on way to have a means of testing Macros, needs to + // get properly integrated + for action in key_to_actions( + &key, + raw_bytes, + &self.mode, + &merged_keybinds, + ) { should_break |= self.dispatch_action(action); } should_break @@ -324,6 +335,7 @@ pub fn get_help(mode: InputMode) -> Help { /// its [`InputHandler::handle_input()`] loop. pub fn input_loop( os_input: Box, + config: Config, command_is_executing: CommandIsExecuting, send_screen_instructions: SenderWithContext, send_pty_instructions: SenderWithContext, @@ -333,6 +345,7 @@ pub fn input_loop( let _handler = InputHandler::new( os_input, command_is_executing, + config, send_screen_instructions, send_pty_instructions, send_plugin_instructions, diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 0ea0695f..8799649d 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -23,7 +23,7 @@ pub fn get_default_keybinds() -> Result { Ok(defaults) } -/// Returns the default keybinds for a givent [`InputMode`]. +/// Returns the default keybinds for a given [`InputMode`]. fn get_defaults_for_mode(mode: &InputMode) -> Result { let mut defaults = ModeKeybinds::new(); diff --git a/src/common/input/macros.rs b/src/common/input/macros.rs index cbd14332..4b7c6df1 100644 --- a/src/common/input/macros.rs +++ b/src/common/input/macros.rs @@ -2,11 +2,11 @@ //! defined predictable order. use super::actions::Action; - + use serde::Deserialize; #[derive(Debug, Clone, Deserialize)] pub struct Macro { name: Option, - sequence: Vec + sequence: Vec, } diff --git a/src/common/mod.rs b/src/common/mod.rs index 5ba2e15d..c8b2ec98 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,5 +1,5 @@ -pub mod config; pub mod command_is_executing; +pub mod config; pub mod errors; pub mod input; pub mod install; @@ -46,10 +46,17 @@ pub enum ApiCommand { MoveFocus, } // FIXME: It would be good to add some more things to this over time -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct AppState { pub input_mode: InputMode, - pub config : Config, +} + +impl Default for AppState { + fn default() -> Self { + AppState { + input_mode: InputMode::default(), + } + } } // FIXME: Make this a method on the big `Communication` struct, so that app_tx can be extracted @@ -158,8 +165,13 @@ pub fn start(mut os_input: Box, opts: CliArgs) { .get_stdout_writer() .write(take_snapshot.as_bytes()) .unwrap(); + let mut app_state = AppState::default(); + let config = Config::from_option_or_default(opts.config) + .map_err(|e| eprintln!{"Config Error: {}", e}) + .unwrap(); + let command_is_executing = CommandIsExecuting::new(); let full_screen_ws = os_input.get_terminal_size_using_fd(0); @@ -638,9 +650,11 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let send_pty_instructions = send_pty_instructions.clone(); let send_plugin_instructions = send_plugin_instructions.clone(); let os_input = os_input.clone(); + let config = config.clone(); move || { input_loop( os_input, + config, command_is_executing, send_screen_instructions, send_pty_instructions, From c97c553870380956904ab7d2daa36e5ce0aef010 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 5 Mar 2021 22:40:42 +0100 Subject: [PATCH 03/23] feature(input): add config file * use a simple platform dependent config location `ProjectDir` * deserialise from yaml iterate more on config format, maybe be more verbose? * make keybinds a tuple struct same size as newtype, but makes impls easier to add * merge optionally multiple keys for one keybinding easier configuration --- config.yaml | 14 +- src/common/config.rs | 116 +++-- src/common/input/actions.rs | 6 +- src/common/input/handler.rs | 16 +- src/common/input/keybinds.rs | 450 +++++++++++------- src/common/input/macros.rs | 12 - src/common/input/mod.rs | 3 +- src/common/input/ut/config_test.rs | 51 ++ src/common/input/ut/keybinds_test.rs | 0 src/common/mod.rs | 8 +- .../config/functioning_simple_config.yaml | 0 .../config/multiple_keys_for_one_action.yaml | 8 + 12 files changed, 430 insertions(+), 254 deletions(-) delete mode 100644 src/common/input/macros.rs create mode 100644 src/common/input/ut/config_test.rs create mode 100644 src/common/input/ut/keybinds_test.rs create mode 100644 src/tests/fixtures/config/functioning_simple_config.yaml create mode 100644 src/tests/fixtures/config/multiple_keys_for_one_action.yaml diff --git a/config.yaml b/config.yaml index b1657775..719d15a2 100644 --- a/config.yaml +++ b/config.yaml @@ -1,8 +1,12 @@ --- -macro: - {name:"closePane", sequence: [NewPane: Right,]} - keybinds: Normal: - Backspace: [NewPane:, NewPane:,] - {F: 1}: [NewPane:,] + - {F: 1}: [GoToTab: 1,] + - {F: 2}: [GoToTab: 2,] + - {F: 3}: [GoToTab: 3,] + - {F: 4}: [GoToTab: 4,] + - {F: 5}: [NewTab: ,] + - ? - F: 6 + - F: 7 + : - {GoToTab: 5} + - Backspace: [NewPane: Right, NewPane: Right,] diff --git a/src/common/config.rs b/src/common/config.rs index 705c1592..bf8a9a9f 100644 --- a/src/common/config.rs +++ b/src/common/config.rs @@ -1,104 +1,101 @@ //! Deserializes configuration options. use std; -//use std::collections::HashMap; use std::error; use std::fmt::{self, Display}; use std::fs::File; use std::io::{self, Read}; use std::path::PathBuf; -use super::input::{keybinds, macros}; +use super::input::keybinds::{Keybinds, MultipleKeybinds}; +use crate::utils::logging::*; use directories_next::ProjectDirs; use serde::Deserialize; -/// Intermediate struct -//pub struct KeybingsFromYaml { - -//} - -/// Intermediate struct +/// Intermediate deserialisation config struct #[derive(Debug, Deserialize)] pub struct ConfigFromYaml { - keybinds: Option, - macros: Option>, + pub keybinds: Option, } -///// Deserialized config state -#[derive(Debug, Clone, Default, Deserialize)] +/// Main configuration. +#[derive(Debug, Clone, PartialEq)] pub struct Config { - pub keybinds: keybinds::Keybinds, + pub keybinds: Keybinds, } #[derive(Debug)] pub enum ConfigError { - // from the serde documentation - // https://serde.rs/error-handling.html - // One or more variants that can be created by data structures through the - // `ser::Error` and `de::Error` traits. For example the Serialize impl for - // Mutex might return an error because the mutex is poisoned, or the - // Deserialize impl for a struct may return an error because a required - // field is missing. - //Message(String), - // serde_yaml error + // Deserialisation error Serde(serde_yaml::Error), - //Eof, - // io::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 { - /// Deserializes from given path - pub fn new(path: &PathBuf) -> Result { - let config: Config; - let config_deserialized: ConfigFromYaml; - let mut config_string = String::new(); + /// Uses defaults, but lets config override them. + pub fn from_yaml(yaml_config: &str) -> Result { + 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 }) + } - // TODO fix this unwrap + /// 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: &PathBuf) -> Result { match File::open(path) { Ok(mut file) => { - file.read_to_string(&mut config_string)?; - config_deserialized = serde_yaml::from_str(&config_string)?; - config = Config { - keybinds: config_deserialized - .keybinds - .unwrap_or_else(|| keybinds::get_default_keybinds().unwrap()), - } + 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) => { // TODO logging, if a file is not found // at an expected position - should not - // panic @a-kenji - eprintln!("{}", e); - config = Config::default(); + // panic, but log to file @a-kenji + debug_log_to_file(format!( + "{}\nUsing the default configuration!", + ConfigError::IoPath(e, path.to_path_buf()) + )); + Ok(Config::default()) } } - Ok(config) } + /// 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) -> Result { - let config; if let Some(config_path) = option { - config = Config::new(&config_path)?; + Ok(Config::new(&config_path)?) } else { - let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); - //let config_path = PathBuf::from(project_dirs.config_dir().as_os_str()); - let config_path = project_dirs.config_dir().to_owned().into(); - config = Config::new(&config_path)?; + let project_dirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); + let mut config_path: PathBuf = project_dirs.config_dir().to_owned().into(); + config_path.push("config.yaml"); + Ok(Config::new(&config_path)?) } - return Ok(config); } } impl Display for ConfigError { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match self { - //ConfigError::Message(msg) => formatter.write_str(msg), - //ConfigError::Eof => formatter.write_str("unexpected end of input"), - // - ConfigError::Io(ref err) => write!(formatter, "Io error: {}", err), - ConfigError::Serde(ref err) => write!(formatter, "Serde error: {}", err), - /* and so forth */ + 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), } } } @@ -106,12 +103,8 @@ impl Display for ConfigError { impl std::error::Error for ConfigError { fn cause(&self) -> Option<&dyn error::Error> { match *self { - // N.B. Both of these implicitly cast `err` from their concrete - // types (either `&io::Error` or `&num::ParseIntError`) - // to a trait object `&Error`. This works because both error types - // implement `Error`. ConfigError::Io(ref err) => Some(err), - //ConfigError::Message(ref err) => Some(err), + ConfigError::IoPath(ref err, _) => Some(err), ConfigError::Serde(ref err) => Some(err), } } @@ -128,3 +121,8 @@ impl From for ConfigError { ConfigError::Serde(err) } } + +// The unit test location. +#[cfg(test)] +#[path = "./input/ut/config_test.rs"] +mod config_test; diff --git a/src/common/input/actions.rs b/src/common/input/actions.rs index d03ca859..6d5c5892 100644 --- a/src/common/input/actions.rs +++ b/src/common/input/actions.rs @@ -2,10 +2,10 @@ use super::handler; //use super::macros; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; /// The four directions (left, right, up, down). -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub enum Direction { Left, Right, @@ -14,7 +14,7 @@ pub enum Direction { } /// Actions that can be bound to keys. -#[derive(Clone, Debug, Deserialize)] +#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] pub enum Action { /// Quit Zellij. Quit, diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 9d8b2cf2..9b08491b 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -1,7 +1,7 @@ //! Main input logic. use super::actions::Action; -use super::keybinds::get_default_keybinds; +use super::keybinds::Keybinds; use crate::common::config::Config; use crate::common::{update_state, AppInstruction, AppState, SenderWithContext, OPENCALLS}; use crate::errors::ContextType; @@ -15,8 +15,6 @@ use serde::{Deserialize, Serialize}; use strum_macros::EnumIter; use termion::input::TermReadEventsAndRaw; -use super::keybinds::key_to_actions; - /// Handles the dispatching of [`Action`]s according to the current /// [`InputMode`], and keep tracks of the current [`InputMode`]. struct InputHandler { @@ -62,9 +60,8 @@ impl InputHandler { self.send_pty_instructions.update(err_ctx); self.send_app_instructions.update(err_ctx); self.send_screen_instructions.update(err_ctx); - if let Ok(keybinds) = get_default_keybinds() { - let mut merged_keybinds = keybinds; - merged_keybinds.extend(self.config.keybinds.clone().into_iter()); + if let Ok(_keybinds) = Keybinds::get_default_keybinds() { + let keybinds = self.config.keybinds.clone(); 'input_loop: loop { //@@@ I think this should actually just iterate over stdin directly let stdin_buffer = self.os_input.read_from_stdin(); @@ -84,11 +81,8 @@ impl InputHandler { let mut should_break = false; // Hacked on way to have a means of testing Macros, needs to // get properly integrated - for action in key_to_actions( - &key, - raw_bytes, - &self.mode, - &merged_keybinds, + for action in Keybinds::key_to_actions( + &key, raw_bytes, &self.mode, &keybinds, ) { should_break |= self.dispatch_action(action); } diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 8799649d..8b089ecf 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -1,183 +1,311 @@ //! Mapping of inputs to sequences of actions. +use std::collections::HashMap; use super::actions::{Action, Direction}; use super::handler::InputMode; -use std::collections::HashMap; - +use serde::Deserialize; use strum::IntoEnumIterator; use termion::event::Key; -pub type Keybinds = HashMap; -type ModeKeybinds = HashMap>; +#[derive(Clone, Debug, PartialEq, Deserialize)] +pub struct Keybinds(HashMap); +#[derive(Clone, Debug, Default, PartialEq, Deserialize)] +pub struct ModeKeybinds(HashMap>); -/// Populates the default hashmap of keybinds. -/// @@@khs26 What about an input config file? -pub fn get_default_keybinds() -> Result { - let mut defaults = Keybinds::new(); +/// Intermediate struct for configuration. +#[derive(Clone, Debug, Default, PartialEq, Deserialize)] +pub struct MultipleKeybinds(HashMap>); +/// Intermediate struct for configuration. +#[derive(Clone, Debug, Default, PartialEq, Deserialize)] +struct MultipleModeKeybinds(HashMap, Vec>); - for mode in InputMode::iter() { - defaults.insert(mode, get_defaults_for_mode(&mode)?); - } - - Ok(defaults) +/// Intermediate enum for configuration. +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(untagged)] +enum ModeKeybind { + ModeKeybinds(ModeKeybinds), + MultipleModeKeybinds(MultipleModeKeybinds), } -/// Returns the default keybinds for a given [`InputMode`]. -fn get_defaults_for_mode(mode: &InputMode) -> Result { - let mut defaults = ModeKeybinds::new(); +impl Default for Keybinds { + fn default() -> Keybinds { + Keybinds::get_default_keybinds().expect("Something is wrong with the default Keybinds") + } +} - match *mode { - InputMode::Normal => { - defaults.insert( - Key::Ctrl('g'), - vec![Action::SwitchToMode(InputMode::Command)], - ); +impl Keybinds { + pub fn new() -> Keybinds { + Keybinds::from(HashMap::::new()) + } + + pub fn get_default_keybinds() -> Result { + let mut defaults = Keybinds::new(); + + for mode in InputMode::iter() { + defaults + .0 + .insert(mode, Keybinds::get_defaults_for_mode(&mode)?); } - InputMode::Command => { - defaults.insert( - Key::Char('r'), - vec![Action::SwitchToMode(InputMode::Resize)], - ); - defaults.insert(Key::Char('p'), vec![Action::SwitchToMode(InputMode::Pane)]); - defaults.insert(Key::Char('t'), vec![Action::SwitchToMode(InputMode::Tab)]); - defaults.insert( - Key::Char('s'), - vec![Action::SwitchToMode(InputMode::Scroll)], - ); - defaults.insert( - Key::Ctrl('g'), - vec![Action::SwitchToMode(InputMode::Normal)], - ); - defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Normal)]); - defaults.insert(Key::Char('q'), vec![Action::Quit]); + + Ok(defaults) + } + + pub fn get_default_keybinds_with_config(keybinds: Option) -> Keybinds { + let default_keybinds = Keybinds::default(); + if let Some(keybinds) = keybinds { + default_keybinds.merge_keybinds(Keybinds::from(keybinds)) + } else { + default_keybinds } - InputMode::Resize => { - defaults.insert(Key::Char('h'), vec![Action::Resize(Direction::Left)]); - defaults.insert(Key::Char('j'), vec![Action::Resize(Direction::Down)]); - defaults.insert(Key::Char('k'), vec![Action::Resize(Direction::Up)]); - defaults.insert(Key::Char('l'), vec![Action::Resize(Direction::Right)]); + } - defaults.insert(Key::Left, vec![Action::Resize(Direction::Left)]); - defaults.insert(Key::Down, vec![Action::Resize(Direction::Down)]); - defaults.insert(Key::Up, vec![Action::Resize(Direction::Up)]); - defaults.insert(Key::Right, vec![Action::Resize(Direction::Right)]); + /// Merges two Keybinds structs into one Keybinds struct + /// `other` overrides the ModeKeybinds of `self`. + fn merge_keybinds(&self, other: Keybinds) -> Keybinds { + let mut keybinds = Keybinds::default(); - defaults.insert(Key::Ctrl('b'), vec![Action::Resize(Direction::Left)]); - defaults.insert(Key::Ctrl('n'), vec![Action::Resize(Direction::Down)]); - defaults.insert(Key::Ctrl('p'), vec![Action::Resize(Direction::Up)]); - defaults.insert(Key::Ctrl('f'), vec![Action::Resize(Direction::Right)]); - - defaults.insert(Key::Char('q'), vec![Action::Quit]); - defaults.insert( - Key::Ctrl('g'), - vec![Action::SwitchToMode(InputMode::Normal)], - ); - defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); - } - InputMode::Pane => { - defaults.insert(Key::Char('h'), vec![Action::MoveFocus(Direction::Left)]); - defaults.insert(Key::Char('j'), vec![Action::MoveFocus(Direction::Down)]); - defaults.insert(Key::Char('k'), vec![Action::MoveFocus(Direction::Up)]); - defaults.insert(Key::Char('l'), vec![Action::MoveFocus(Direction::Right)]); - - defaults.insert(Key::Left, vec![Action::MoveFocus(Direction::Left)]); - defaults.insert(Key::Down, vec![Action::MoveFocus(Direction::Down)]); - defaults.insert(Key::Up, vec![Action::MoveFocus(Direction::Up)]); - defaults.insert(Key::Right, vec![Action::MoveFocus(Direction::Right)]); - - defaults.insert(Key::Ctrl('b'), vec![Action::MoveFocus(Direction::Left)]); - defaults.insert(Key::Ctrl('n'), vec![Action::MoveFocus(Direction::Down)]); - defaults.insert(Key::Ctrl('p'), vec![Action::MoveFocus(Direction::Up)]); - defaults.insert(Key::Ctrl('f'), vec![Action::MoveFocus(Direction::Right)]); - - defaults.insert(Key::Char('p'), vec![Action::SwitchFocus(Direction::Right)]); - defaults.insert(Key::Char('n'), vec![Action::NewPane(None)]); - defaults.insert(Key::Char('d'), vec![Action::NewPane(Some(Direction::Down))]); - defaults.insert( - Key::Char('r'), - vec![Action::NewPane(Some(Direction::Right))], - ); - defaults.insert(Key::Char('x'), vec![Action::CloseFocus]); - - defaults.insert(Key::Char('f'), vec![Action::ToggleFocusFullscreen]); - - defaults.insert(Key::Char('q'), vec![Action::Quit]); - defaults.insert( - Key::Ctrl('g'), - vec![Action::SwitchToMode(InputMode::Normal)], - ); - defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); - } - InputMode::Tab => { - defaults.insert(Key::Char('h'), vec![Action::GoToPreviousTab]); - defaults.insert(Key::Char('j'), vec![Action::GoToNextTab]); - defaults.insert(Key::Char('k'), vec![Action::GoToPreviousTab]); - defaults.insert(Key::Char('l'), vec![Action::GoToNextTab]); - - defaults.insert(Key::Left, vec![Action::GoToPreviousTab]); - defaults.insert(Key::Down, vec![Action::GoToNextTab]); - defaults.insert(Key::Up, vec![Action::GoToPreviousTab]); - defaults.insert(Key::Right, vec![Action::GoToNextTab]); - - defaults.insert(Key::Ctrl('b'), vec![Action::GoToPreviousTab]); - defaults.insert(Key::Ctrl('n'), vec![Action::GoToNextTab]); - defaults.insert(Key::Ctrl('p'), vec![Action::GoToPreviousTab]); - defaults.insert(Key::Ctrl('f'), vec![Action::GoToNextTab]); - - defaults.insert(Key::Char('n'), vec![Action::NewTab]); - defaults.insert(Key::Char('x'), vec![Action::CloseTab]); - - defaults.insert(Key::Char('q'), vec![Action::Quit]); - defaults.insert( - Key::Ctrl('g'), - vec![Action::SwitchToMode(InputMode::Normal)], - ); - for i in '1'..='9' { - defaults.insert(Key::Char(i), vec![Action::GoToTab(i.to_digit(10).unwrap())]); + for mode in InputMode::iter() { + let mut mode_keybinds: ModeKeybinds = if let Some(keybind) = self.0.get(&mode) { + keybind.clone() + } else { + ModeKeybinds::default() + }; + if let Some(keybind) = other.0.get(&mode) { + mode_keybinds.0.extend(keybind.0.clone()); } - defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); + keybinds.0.insert(mode, mode_keybinds); } - InputMode::Scroll => { - defaults.insert(Key::Char('j'), vec![Action::ScrollDown]); - defaults.insert(Key::Char('k'), vec![Action::ScrollUp]); - - defaults.insert(Key::Down, vec![Action::ScrollDown]); - defaults.insert(Key::Up, vec![Action::ScrollUp]); - - defaults.insert(Key::Ctrl('n'), vec![Action::ScrollDown]); - defaults.insert(Key::Ctrl('p'), vec![Action::ScrollUp]); - - defaults.insert(Key::Char('q'), vec![Action::Quit]); - defaults.insert( - Key::Ctrl('g'), - vec![Action::SwitchToMode(InputMode::Normal)], - ); - defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); - } - } - - Ok(defaults) -} - -/// Converts a [`Key`] terminal event to a sequence of [`Action`]s according to the current -/// [`InputMode`] and [`Keybinds`]. -pub fn key_to_actions( - key: &Key, - input: Vec, - mode: &InputMode, - keybinds: &Keybinds, -) -> Vec { - let mode_keybind_or_action = |action: Action| { keybinds - .get(mode) - .unwrap_or_else(|| unreachable!("Unrecognized mode: {:?}", mode)) - .get(key) - .cloned() - .unwrap_or_else(|| vec![action]) - }; - match *mode { - InputMode::Normal => mode_keybind_or_action(Action::Write(input)), - _ => mode_keybind_or_action(Action::NoOp), + } + + /// Returns the default keybinds for a given [`InputMode`]. + fn get_defaults_for_mode(mode: &InputMode) -> Result { + let mut defaults = HashMap::new(); + + match *mode { + InputMode::Normal => { + defaults.insert( + Key::Ctrl('g'), + vec![Action::SwitchToMode(InputMode::Command)], + ); + } + InputMode::Command => { + defaults.insert( + Key::Char('r'), + vec![Action::SwitchToMode(InputMode::Resize)], + ); + defaults.insert(Key::Char('p'), vec![Action::SwitchToMode(InputMode::Pane)]); + defaults.insert(Key::Char('t'), vec![Action::SwitchToMode(InputMode::Tab)]); + defaults.insert( + Key::Char('s'), + vec![Action::SwitchToMode(InputMode::Scroll)], + ); + defaults.insert( + Key::Ctrl('g'), + vec![Action::SwitchToMode(InputMode::Normal)], + ); + defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Normal)]); + defaults.insert(Key::Char('q'), vec![Action::Quit]); + } + InputMode::Resize => { + defaults.insert(Key::Char('h'), vec![Action::Resize(Direction::Left)]); + defaults.insert(Key::Char('j'), vec![Action::Resize(Direction::Down)]); + defaults.insert(Key::Char('k'), vec![Action::Resize(Direction::Up)]); + defaults.insert(Key::Char('l'), vec![Action::Resize(Direction::Right)]); + + defaults.insert(Key::Left, vec![Action::Resize(Direction::Left)]); + defaults.insert(Key::Down, vec![Action::Resize(Direction::Down)]); + defaults.insert(Key::Up, vec![Action::Resize(Direction::Up)]); + defaults.insert(Key::Right, vec![Action::Resize(Direction::Right)]); + + defaults.insert(Key::Ctrl('b'), vec![Action::Resize(Direction::Left)]); + defaults.insert(Key::Ctrl('n'), vec![Action::Resize(Direction::Down)]); + defaults.insert(Key::Ctrl('p'), vec![Action::Resize(Direction::Up)]); + defaults.insert(Key::Ctrl('f'), vec![Action::Resize(Direction::Right)]); + + defaults.insert(Key::Char('q'), vec![Action::Quit]); + defaults.insert( + Key::Ctrl('g'), + vec![Action::SwitchToMode(InputMode::Normal)], + ); + defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); + } + InputMode::Pane => { + defaults.insert(Key::Char('h'), vec![Action::MoveFocus(Direction::Left)]); + defaults.insert(Key::Char('j'), vec![Action::MoveFocus(Direction::Down)]); + defaults.insert(Key::Char('k'), vec![Action::MoveFocus(Direction::Up)]); + defaults.insert(Key::Char('l'), vec![Action::MoveFocus(Direction::Right)]); + + defaults.insert(Key::Left, vec![Action::MoveFocus(Direction::Left)]); + defaults.insert(Key::Down, vec![Action::MoveFocus(Direction::Down)]); + defaults.insert(Key::Up, vec![Action::MoveFocus(Direction::Up)]); + defaults.insert(Key::Right, vec![Action::MoveFocus(Direction::Right)]); + + defaults.insert(Key::Ctrl('b'), vec![Action::MoveFocus(Direction::Left)]); + defaults.insert(Key::Ctrl('n'), vec![Action::MoveFocus(Direction::Down)]); + defaults.insert(Key::Ctrl('p'), vec![Action::MoveFocus(Direction::Up)]); + defaults.insert(Key::Ctrl('f'), vec![Action::MoveFocus(Direction::Right)]); + + defaults.insert(Key::Char('p'), vec![Action::SwitchFocus(Direction::Right)]); + defaults.insert(Key::Char('n'), vec![Action::NewPane(None)]); + defaults.insert(Key::Char('d'), vec![Action::NewPane(Some(Direction::Down))]); + defaults.insert( + Key::Char('r'), + vec![Action::NewPane(Some(Direction::Right))], + ); + defaults.insert(Key::Char('x'), vec![Action::CloseFocus]); + + defaults.insert(Key::Char('f'), vec![Action::ToggleFocusFullscreen]); + + defaults.insert(Key::Char('q'), vec![Action::Quit]); + defaults.insert( + Key::Ctrl('g'), + vec![Action::SwitchToMode(InputMode::Normal)], + ); + defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); + } + InputMode::Tab => { + defaults.insert(Key::Char('h'), vec![Action::GoToPreviousTab]); + defaults.insert(Key::Char('j'), vec![Action::GoToNextTab]); + defaults.insert(Key::Char('k'), vec![Action::GoToPreviousTab]); + defaults.insert(Key::Char('l'), vec![Action::GoToNextTab]); + + defaults.insert(Key::Left, vec![Action::GoToPreviousTab]); + defaults.insert(Key::Down, vec![Action::GoToNextTab]); + defaults.insert(Key::Up, vec![Action::GoToPreviousTab]); + defaults.insert(Key::Right, vec![Action::GoToNextTab]); + + defaults.insert(Key::Ctrl('b'), vec![Action::GoToPreviousTab]); + defaults.insert(Key::Ctrl('n'), vec![Action::GoToNextTab]); + defaults.insert(Key::Ctrl('p'), vec![Action::GoToPreviousTab]); + defaults.insert(Key::Ctrl('f'), vec![Action::GoToNextTab]); + + defaults.insert(Key::Char('n'), vec![Action::NewTab]); + defaults.insert(Key::Char('x'), vec![Action::CloseTab]); + + defaults.insert(Key::Char('q'), vec![Action::Quit]); + defaults.insert( + Key::Ctrl('g'), + vec![Action::SwitchToMode(InputMode::Normal)], + ); + for i in '1'..='9' { + defaults.insert(Key::Char(i), vec![Action::GoToTab(i.to_digit(10).unwrap())]); + } + defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); + } + InputMode::Scroll => { + defaults.insert(Key::Char('j'), vec![Action::ScrollDown]); + defaults.insert(Key::Char('k'), vec![Action::ScrollUp]); + + defaults.insert(Key::Down, vec![Action::ScrollDown]); + defaults.insert(Key::Up, vec![Action::ScrollUp]); + + defaults.insert(Key::Ctrl('n'), vec![Action::ScrollDown]); + defaults.insert(Key::Ctrl('p'), vec![Action::ScrollUp]); + + defaults.insert(Key::Char('q'), vec![Action::Quit]); + defaults.insert( + Key::Ctrl('g'), + vec![Action::SwitchToMode(InputMode::Normal)], + ); + defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); + } + } + + Ok(ModeKeybinds::from(defaults)) + } + + /// Converts a [`Key`] terminal event to a sequence of [`Action`]s according to the current + /// [`InputMode`] and [`Keybinds`]. + pub fn key_to_actions( + key: &Key, + input: Vec, + mode: &InputMode, + keybinds: &Keybinds, + ) -> Vec { + let mode_keybind_or_action = |action: Action| { + keybinds + .0 + .get(mode) + .unwrap_or_else(|| unreachable!("Unrecognized mode: {:?}", mode)) + .0 + .get(key) + .cloned() + .unwrap_or_else(|| vec![action]) + }; + match *mode { + InputMode::Normal => mode_keybind_or_action(Action::Write(input)), + _ => mode_keybind_or_action(Action::NoOp), + } } } + +impl ModeKeybinds { + pub fn new() -> ModeKeybinds { + ModeKeybinds::from(HashMap::>::new()) + } + + /// Merges `self` with `other`, if keys are the same, `other` overwrites. + pub fn merge(self, other: ModeKeybinds) -> ModeKeybinds { + let mut merged = self; + merged.0.extend(other.0.clone()); + merged + } +} + +/// Converts from the `MultipleKeybinds` struct to a `Keybinds` struct. +/// TODO @a-kenji Error on conflicting keybinds? +/// Atm the Lower Keybind will take precedence and will overwrite the +/// one further up. +impl From for Keybinds { + fn from(multiple: MultipleKeybinds) -> Keybinds { + let mut keybinds = Keybinds::new(); + + for mode in InputMode::iter() { + let mut mode_keybinds = ModeKeybinds::new(); + for mode_keybind in multiple.0.get(&mode).iter() { + for keybind in mode_keybind.iter() { + match keybind { + ModeKeybind::ModeKeybinds(k) => { + mode_keybinds = mode_keybinds.clone().merge(k.clone()); + } + ModeKeybind::MultipleModeKeybinds(k) => { + mode_keybinds = + mode_keybinds.clone().merge(ModeKeybinds::from(k.clone())); + } + } + } + } + keybinds.0.insert(mode, mode_keybinds); + } + keybinds + } +} + +impl From> for Keybinds { + fn from(map: HashMap) -> Keybinds { + Keybinds(map) + } +} +impl From>> for ModeKeybinds { + fn from(map: HashMap>) -> ModeKeybinds { + ModeKeybinds(map) + } +} + +impl From for ModeKeybinds { + fn from(multiple: MultipleModeKeybinds) -> ModeKeybinds { + let single: HashMap> = multiple + .0 + .into_iter() + .flat_map(|(k, v)| (k.into_iter().map(move |k| (k, v.clone())))) + .collect(); + ModeKeybinds::from(single) + } +} + +// The unit test location. +#[cfg(test)] +#[path = "./ut/keybinds_test.rs"] +mod keybinds_test; diff --git a/src/common/input/macros.rs b/src/common/input/macros.rs deleted file mode 100644 index 4b7c6df1..00000000 --- a/src/common/input/macros.rs +++ /dev/null @@ -1,12 +0,0 @@ -//! Use a list of commands and execute them in a -//! defined predictable order. - -use super::actions::Action; - -use serde::Deserialize; - -#[derive(Debug, Clone, Deserialize)] -pub struct Macro { - name: Option, - sequence: Vec, -} diff --git a/src/common/input/mod.rs b/src/common/input/mod.rs index 438df224..a7dca7ed 100644 --- a/src/common/input/mod.rs +++ b/src/common/input/mod.rs @@ -1,6 +1,5 @@ -//! The way terminal iput is handled. +//! The way terminal input is handled. pub mod actions; pub mod handler; pub mod keybinds; -pub mod macros; diff --git a/src/common/input/ut/config_test.rs b/src/common/input/ut/config_test.rs new file mode 100644 index 00000000..001aab15 --- /dev/null +++ b/src/common/input/ut/config_test.rs @@ -0,0 +1,51 @@ +//! For Configuration Options + +use super::super::config::*; +use crate::common::input::keybinds::*; +use crate::common::input::actions::*; +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"); + let config = Config::from_option_or_default(Some(no_file)).unwrap(); + let default = Config::default(); + assert_eq!(config, default); +} + +#[test] +fn no_config_option_file_equals_default_config() { + let config = Config::from_option_or_default(None).unwrap(); + 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 new file mode 100644 index 00000000..e69de29b diff --git a/src/common/mod.rs b/src/common/mod.rs index c8b2ec98..fdd66649 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -29,6 +29,7 @@ use wasmer_wasi::{Pipe, WasiState}; use crate::cli::CliArgs; use crate::common::config::Config; use crate::layout::Layout; +use crate::utils::logging::*; use command_is_executing::CommandIsExecuting; use errors::{AppContext, ContextType, ErrorContext, PluginContext, PtyContext, ScreenContext}; use input::handler::input_loop; @@ -168,8 +169,13 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let mut app_state = AppState::default(); + #[allow(unused_must_use)] let config = Config::from_option_or_default(opts.config) - .map_err(|e| eprintln!{"Config Error: {}", e}) + .map_err(|e| { + debug_log_to_file(format!("There was an error in the config file:\n{}", e)); + eprintln!("There was an error in the config file:\n{}", e); + std::process::exit(1); + }) .unwrap(); let command_is_executing = CommandIsExecuting::new(); diff --git a/src/tests/fixtures/config/functioning_simple_config.yaml b/src/tests/fixtures/config/functioning_simple_config.yaml new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/fixtures/config/multiple_keys_for_one_action.yaml b/src/tests/fixtures/config/multiple_keys_for_one_action.yaml new file mode 100644 index 00000000..7da62541 --- /dev/null +++ b/src/tests/fixtures/config/multiple_keys_for_one_action.yaml @@ -0,0 +1,8 @@ +--- +keybindings: + Normal: + - ? - F: 6 + - F: 7 + - F: 8 + : - {GoToTab: 5} + From 2b2b7325e7e6b6c3db6376cc0031148a3af1c086 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Thu, 11 Mar 2021 13:58:58 +0100 Subject: [PATCH 04/23] Poking around --- assets/completions/_zellij | 1 - assets/completions/zellij.bash | 6 +- assets/completions/zellij.fish | 2 +- config.yaml | 23 +++--- src/cli.rs | 2 +- src/common/config.rs | 11 ++- src/common/input/handler.rs | 72 ++++++++---------- src/common/input/keybinds.rs | 109 ++++++++++----------------- src/common/input/ut/config_test.rs | 43 ++++++----- src/common/input/ut/keybinds_test.rs | 76 +++++++++++++++++++ src/common/mod.rs | 2 +- 11 files changed, 193 insertions(+), 154 deletions(-) diff --git a/assets/completions/_zellij b/assets/completions/_zellij index cd8dce56..f292d560 100644 --- a/assets/completions/_zellij +++ b/assets/completions/_zellij @@ -22,7 +22,6 @@ _zellij() { '--max-panes=[Maximum panes on screen, caution: opening more panes will close old ones]' \ '-l+[Path to a layout yaml file]' \ '--layout=[Path to a layout yaml file]' \ -'-c+[Path to the configuration yaml file]' \ '--config=[Path to the configuration yaml file]' \ '-m[Send "move focused pane" to active zellij session]' \ '--move-focus[Send "move focused pane" to active zellij session]' \ diff --git a/assets/completions/zellij.bash b/assets/completions/zellij.bash index 0ede5cce..17afd610 100644 --- a/assets/completions/zellij.bash +++ b/assets/completions/zellij.bash @@ -20,7 +20,7 @@ _zellij() { case "${cmd}" in zellij) - opts=" -m -d -h -V -s -o -l -c --move-focus --debug --help --version --split --open-file --max-panes --layout --config " + opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --layout --config " if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -59,10 +59,6 @@ _zellij() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; - -c) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; *) COMPREPLY=() ;; diff --git a/assets/completions/zellij.fish b/assets/completions/zellij.fish index eaba9fa4..36425a25 100644 --- a/assets/completions/zellij.fish +++ b/assets/completions/zellij.fish @@ -2,7 +2,7 @@ complete -c zellij -n "__fish_use_subcommand" -s s -l split -d 'Send "split (dir complete -c zellij -n "__fish_use_subcommand" -s o -l open-file -d 'Send "open file in new pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -l max-panes -d 'Maximum panes on screen, caution: opening more panes will close old ones' complete -c zellij -n "__fish_use_subcommand" -s l -l layout -d 'Path to a layout yaml file' -complete -c zellij -n "__fish_use_subcommand" -s c -l config -d 'Path to the configuration yaml file' +complete -c zellij -n "__fish_use_subcommand" -l config -d 'Path to the configuration yaml file' complete -c zellij -n "__fish_use_subcommand" -s m -l move-focus -d 'Send "move focused pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -s d -l debug complete -c zellij -n "__fish_use_subcommand" -s h -l help -d 'Prints help information' diff --git a/config.yaml b/config.yaml index 719d15a2..eb227d4e 100644 --- a/config.yaml +++ b/config.yaml @@ -1,12 +1,17 @@ --- keybinds: Normal: - - {F: 1}: [GoToTab: 1,] - - {F: 2}: [GoToTab: 2,] - - {F: 3}: [GoToTab: 3,] - - {F: 4}: [GoToTab: 4,] - - {F: 5}: [NewTab: ,] - - ? - F: 6 - - F: 7 - : - {GoToTab: 5} - - Backspace: [NewPane: Right, NewPane: Right,] + - action: [GoToTab: 1,] + key: [F: 1,] + - action: [GoToTab: 2,] + key: [F: 2,] + - action: [GoToTab: 3,] + key: [F: 3,] + - action: [GoToTab: 4,] + key: [F: 3,] + - action: [NewTab,] + key: [F: 5,] + - action: [NewPane: Right, NewPane: Right,] + key: [F: 6,] + - action: [NewPane: Right, NewPane: Right,] + key: [F: 6,] diff --git a/src/cli.rs b/src/cli.rs index 67a92196..d6d3057e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -25,7 +25,7 @@ pub struct CliArgs { pub layout: Option, /// Path to the configuration yaml file - #[structopt(short, long)] + #[structopt(long)] pub config: Option, #[structopt(short, long)] diff --git a/src/common/config.rs b/src/common/config.rs index bf8a9a9f..4a66acb0 100644 --- a/src/common/config.rs +++ b/src/common/config.rs @@ -1,12 +1,11 @@ //! Deserializes configuration options. -use std; use std::error; use std::fmt::{self, Display}; use std::fs::File; use std::io::{self, Read}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; -use super::input::keybinds::{Keybinds, MultipleKeybinds}; +use super::input::keybinds::{Keybinds, KeybindsFromYaml}; use crate::utils::logging::*; use directories_next::ProjectDirs; @@ -15,7 +14,7 @@ use serde::Deserialize; /// Intermediate deserialisation config struct #[derive(Debug, Deserialize)] pub struct ConfigFromYaml { - pub keybinds: Option, + pub keybinds: Option, } /// Main configuration. @@ -53,7 +52,7 @@ impl Config { /// 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: &PathBuf) -> Result { + pub fn new(path: &Path) -> Result { match File::open(path) { Ok(mut file) => { let mut yaml_config = String::new(); @@ -81,7 +80,7 @@ impl Config { 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().into(); + let mut config_path: PathBuf = project_dirs.config_dir().to_owned(); config_path.push("config.yaml"); Ok(Config::new(&config_path)?) } diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 9b08491b..3438bba8 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -60,49 +60,43 @@ impl InputHandler { self.send_pty_instructions.update(err_ctx); self.send_app_instructions.update(err_ctx); self.send_screen_instructions.update(err_ctx); - if let Ok(_keybinds) = Keybinds::get_default_keybinds() { - let keybinds = self.config.keybinds.clone(); - 'input_loop: loop { - //@@@ I think this should actually just iterate over stdin directly - let stdin_buffer = self.os_input.read_from_stdin(); - drop( - self.send_plugin_instructions - .send(PluginInstruction::GlobalInput(stdin_buffer.clone())), - ); - for key_result in stdin_buffer.events_and_raw() { - match key_result { - Ok((event, raw_bytes)) => match event { - termion::event::Event::Key(key) => { - // FIXME this explicit break is needed because the current test - // framework relies on it to not create dead threads that loop - // and eat up CPUs. Do not remove until the test framework has - // been revised. Sorry about this (@categorille) - if { - let mut should_break = false; - // Hacked on way to have a means of testing Macros, needs to - // get properly integrated - for action in Keybinds::key_to_actions( - &key, raw_bytes, &self.mode, &keybinds, - ) { - should_break |= self.dispatch_action(action); - } - should_break - } { - break 'input_loop; + let keybinds = self.config.keybinds.clone(); + 'input_loop: loop { + //@@@ I think this should actually just iterate over stdin directly + let stdin_buffer = self.os_input.read_from_stdin(); + drop( + self.send_plugin_instructions + .send(PluginInstruction::GlobalInput(stdin_buffer.clone())), + ); + for key_result in stdin_buffer.events_and_raw() { + match key_result { + Ok((event, raw_bytes)) => match event { + termion::event::Event::Key(key) => { + // FIXME this explicit break is needed because the current test + // framework relies on it to not create dead threads that loop + // and eat up CPUs. Do not remove until the test framework has + // been revised. Sorry about this (@categorille) + if { + let mut should_break = false; + // Hacked on way to have a means of testing Macros, needs to + // get properly integrated + for action in + Keybinds::key_to_actions(&key, raw_bytes, &self.mode, &keybinds) + { + should_break |= self.dispatch_action(action); } + should_break + } { + break 'input_loop; } - termion::event::Event::Mouse(_) - | termion::event::Event::Unsupported(_) => { - unimplemented!("Mouse and unsupported events aren't supported!"); - } - }, - Err(err) => panic!("Encountered read error: {:?}", err), - } + } + termion::event::Event::Mouse(_) | termion::event::Event::Unsupported(_) => { + unimplemented!("Mouse and unsupported events aren't supported!"); + } + }, + Err(err) => panic!("Encountered read error: {:?}", err), } } - } else { - //@@@ Error handling? - self.exit(); } } diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 8b089ecf..2c395d37 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -8,50 +8,41 @@ use serde::Deserialize; use strum::IntoEnumIterator; use termion::event::Key; -#[derive(Clone, Debug, PartialEq, Deserialize)] +#[derive(Clone, Debug, PartialEq)] pub struct Keybinds(HashMap); -#[derive(Clone, Debug, Default, PartialEq, Deserialize)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct ModeKeybinds(HashMap>); -/// Intermediate struct for configuration. -#[derive(Clone, Debug, Default, PartialEq, Deserialize)] -pub struct MultipleKeybinds(HashMap>); -/// Intermediate struct for configuration. -#[derive(Clone, Debug, Default, PartialEq, Deserialize)] -struct MultipleModeKeybinds(HashMap, Vec>); +/// Intermediate struct used for deserialisation +#[derive(Clone, Debug, PartialEq, Deserialize)] +pub struct KeybindsFromYaml(HashMap>); -/// Intermediate enum for configuration. -#[derive(Debug, Clone, PartialEq, Deserialize)] -#[serde(untagged)] -enum ModeKeybind { - ModeKeybinds(ModeKeybinds), - MultipleModeKeybinds(MultipleModeKeybinds), +/// Intermediate struct used for deserialisation +#[derive(Clone, Debug, PartialEq, Deserialize)] +pub struct KeyActionFromYaml { + action: Vec, + key: Vec, } impl Default for Keybinds { fn default() -> Keybinds { - Keybinds::get_default_keybinds().expect("Something is wrong with the default Keybinds") - } -} - -impl Keybinds { - pub fn new() -> Keybinds { - Keybinds::from(HashMap::::new()) - } - - pub fn get_default_keybinds() -> Result { let mut defaults = Keybinds::new(); for mode in InputMode::iter() { defaults .0 - .insert(mode, Keybinds::get_defaults_for_mode(&mode)?); + .insert(mode, Keybinds::get_defaults_for_mode(&mode)); } + defaults + } +} - Ok(defaults) +impl Keybinds { + pub fn new() -> Keybinds { + Keybinds(HashMap::::new()) } - pub fn get_default_keybinds_with_config(keybinds: Option) -> Keybinds { + pub fn get_default_keybinds_with_config(keybinds: Option) -> Keybinds { let default_keybinds = Keybinds::default(); if let Some(keybinds) = keybinds { default_keybinds.merge_keybinds(Keybinds::from(keybinds)) @@ -80,7 +71,7 @@ impl Keybinds { } /// Returns the default keybinds for a given [`InputMode`]. - fn get_defaults_for_mode(mode: &InputMode) -> Result { + fn get_defaults_for_mode(mode: &InputMode) -> ModeKeybinds { let mut defaults = HashMap::new(); match *mode { @@ -212,8 +203,7 @@ impl Keybinds { defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]); } } - - Ok(ModeKeybinds::from(defaults)) + ModeKeybinds(defaults) } /// Converts a [`Key`] terminal event to a sequence of [`Action`]s according to the current @@ -242,39 +232,27 @@ impl Keybinds { } impl ModeKeybinds { - pub fn new() -> ModeKeybinds { - ModeKeybinds::from(HashMap::>::new()) + fn new() -> ModeKeybinds { + ModeKeybinds(HashMap::>::new()) } /// Merges `self` with `other`, if keys are the same, `other` overwrites. - pub fn merge(self, other: ModeKeybinds) -> ModeKeybinds { + fn merge(self, other: ModeKeybinds) -> ModeKeybinds { let mut merged = self; - merged.0.extend(other.0.clone()); + merged.0.extend(other.0); merged } } -/// Converts from the `MultipleKeybinds` struct to a `Keybinds` struct. -/// TODO @a-kenji Error on conflicting keybinds? -/// Atm the Lower Keybind will take precedence and will overwrite the -/// one further up. -impl From for Keybinds { - fn from(multiple: MultipleKeybinds) -> Keybinds { +impl From for Keybinds { + fn from(keybinds_from_yaml: KeybindsFromYaml) -> Keybinds { let mut keybinds = Keybinds::new(); for mode in InputMode::iter() { let mut mode_keybinds = ModeKeybinds::new(); - for mode_keybind in multiple.0.get(&mode).iter() { - for keybind in mode_keybind.iter() { - match keybind { - ModeKeybind::ModeKeybinds(k) => { - mode_keybinds = mode_keybinds.clone().merge(k.clone()); - } - ModeKeybind::MultipleModeKeybinds(k) => { - mode_keybinds = - mode_keybinds.clone().merge(ModeKeybinds::from(k.clone())); - } - } + for key_action in keybinds_from_yaml.0.get(&mode).iter() { + for keybind in key_action.iter() { + mode_keybinds = mode_keybinds.merge(ModeKeybinds::from(keybind.clone())); } } keybinds.0.insert(mode, mode_keybinds); @@ -283,25 +261,18 @@ impl From for Keybinds { } } -impl From> for Keybinds { - fn from(map: HashMap) -> Keybinds { - Keybinds(map) - } -} -impl From>> for ModeKeybinds { - fn from(map: HashMap>) -> ModeKeybinds { - ModeKeybinds(map) - } -} +/// For each `Key` assigned to `Action`s, +/// map the `Action`s to the key +impl From for ModeKeybinds { + fn from(key_action: KeyActionFromYaml) -> ModeKeybinds { + let keys = key_action.key; + let actions = key_action.action; -impl From for ModeKeybinds { - fn from(multiple: MultipleModeKeybinds) -> ModeKeybinds { - let single: HashMap> = multiple - .0 - .into_iter() - .flat_map(|(k, v)| (k.into_iter().map(move |k| (k, v.clone())))) - .collect(); - ModeKeybinds::from(single) + ModeKeybinds( + keys.into_iter() + .map(|k| (k, actions.clone())) + .collect::>>(), + ) } } diff --git a/src/common/input/ut/config_test.rs b/src/common/input/ut/config_test.rs index 001aab15..17649d14 100644 --- a/src/common/input/ut/config_test.rs +++ b/src/common/input/ut/config_test.rs @@ -1,13 +1,12 @@ //! For Configuration Options use super::super::config::*; -use crate::common::input::keybinds::*; 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"); @@ -23,29 +22,29 @@ fn no_config_option_file_equals_default_config() { 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} - "; +//#[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(); +//let config_options = Config::from_yaml(&options).unwrap(); - assert_eq!(config_options, config_options) -} +//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(); +//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 e69de29b..44472985 100644 --- a/src/common/input/ut/keybinds_test.rs +++ b/src/common/input/ut/keybinds_test.rs @@ -0,0 +1,76 @@ +use super::super::actions::*; +use super::super::keybinds::*; +use termion::event::Key; + +#[test] +fn merge_keybinds_merges_different_keys() { + let mut mode_keybinds_self = ModeKeybinds::new(); + mode_keybinds_self.0.insert(Key::F(1), vec![Action::NoOp]); + let mut mode_keybinds_other = ModeKeybinds::new(); + mode_keybinds_other + .0 + .insert(Key::Backspace, vec![Action::NoOp]); + + let mut mode_keybinds_expected = ModeKeybinds::new(); + mode_keybinds_expected + .0 + .insert(Key::F(1), vec![Action::NoOp]); + mode_keybinds_expected + .0 + .insert(Key::Backspace, vec![Action::NoOp]); + + let mode_keybinds_merged = mode_keybinds_self.merge(mode_keybinds_other); + + assert_eq!(mode_keybinds_expected, mode_keybinds_merged); +} + +#[test] +fn merge_mode_keybinds_overwrites_same_keys() { + let mut mode_keybinds_self = ModeKeybinds::new(); + mode_keybinds_self.0.insert(Key::F(1), vec![Action::NoOp]); + let mut mode_keybinds_other = ModeKeybinds::new(); + mode_keybinds_other + .0 + .insert(Key::F(1), vec![Action::GoToTab(1)]); + + let mut mode_keybinds_expected = ModeKeybinds::new(); + mode_keybinds_expected + .0 + .insert(Key::F(1), vec![Action::GoToTab(1)]); + + let mode_keybinds_merged = mode_keybinds_self.merge(mode_keybinds_other); + + assert_eq!(mode_keybinds_expected, mode_keybinds_merged); +} + +#[test] +fn merge_keybinds_merges() { + let mut mode_keybinds_self = ModeKeybinds::new(); + mode_keybinds_self.0.insert(Key::F(1), vec![Action::NoOp]); + let mut mode_keybinds_other = ModeKeybinds::new(); + mode_keybinds_other + .0 + .insert(Key::Backspace, vec![Action::NoOp]); + 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::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 + ); + + assert_eq!( + keybinds_expected, + keybinds_self.merge_keybinds(keybinds_other) + ) +} diff --git a/src/common/mod.rs b/src/common/mod.rs index fdd66649..3e2f370a 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -656,7 +656,7 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let send_pty_instructions = send_pty_instructions.clone(); let send_plugin_instructions = send_plugin_instructions.clone(); let os_input = os_input.clone(); - let config = config.clone(); + let config = config; move || { input_loop( os_input, From b4fca93eb71ad15c39a539d0d511b57785d177ad Mon Sep 17 00:00:00 2001 From: a-kenji Date: Thu, 11 Mar 2021 16:38:22 +0100 Subject: [PATCH 05/23] Poking --- src/common/input/keybinds.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 2c395d37..de75c38f 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -54,18 +54,17 @@ impl Keybinds { /// Merges two Keybinds structs into one Keybinds struct /// `other` overrides the ModeKeybinds of `self`. fn merge_keybinds(&self, other: Keybinds) -> Keybinds { - let mut keybinds = Keybinds::default(); + let mut keybinds = Keybinds::new(); - for mode in InputMode::iter() { - let mut mode_keybinds: ModeKeybinds = if let Some(keybind) = self.0.get(&mode) { - keybind.clone() - } else { - ModeKeybinds::default() + for mode in self.0.keys().chain(other.0.keys()) { + let mut mode_keybinds = ModeKeybinds::new(); + if let Some(keybind) = self.0.get(&mode) { + mode_keybinds.0.extend(keybind.0.clone()); }; if let Some(keybind) = other.0.get(&mode) { mode_keybinds.0.extend(keybind.0.clone()); } - keybinds.0.insert(mode, mode_keybinds); + keybinds.0.insert(mode.clone(), mode_keybinds); } keybinds } From a86d8c216186030754a42968768f2af6a131d90d Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 12 Mar 2021 22:05:41 +0100 Subject: [PATCH 06/23] Add example config file. --- config.yaml => example/config.yaml | 8 +- src/common/{ => input}/config.rs | 7 +- src/common/input/handler.rs | 9 ++- src/common/input/keybinds.rs | 6 +- src/common/input/mod.rs | 1 + src/common/input/ut/config_test.rs | 31 -------- src/common/input/ut/keybinds_test.rs | 75 +++++++++++++++++-- src/common/mod.rs | 3 +- .../config/functioning_simple_config.yaml | 0 .../config/multiple_keys_for_one_action.yaml | 8 -- 10 files changed, 85 insertions(+), 63 deletions(-) rename config.yaml => example/config.yaml (58%) rename src/common/{ => input}/config.rs (93%) delete mode 100644 src/tests/fixtures/config/functioning_simple_config.yaml delete mode 100644 src/tests/fixtures/config/multiple_keys_for_one_action.yaml diff --git a/config.yaml b/example/config.yaml similarity index 58% rename from config.yaml rename to example/config.yaml index eb227d4e..f19a2114 100644 --- a/config.yaml +++ b/example/config.yaml @@ -1,6 +1,6 @@ --- keybinds: - Normal: + normal: - action: [GoToTab: 1,] key: [F: 1,] - action: [GoToTab: 2,] @@ -8,10 +8,6 @@ keybinds: - action: [GoToTab: 3,] key: [F: 3,] - action: [GoToTab: 4,] - key: [F: 3,] + key: [F: 4,] - action: [NewTab,] key: [F: 5,] - - action: [NewPane: Right, NewPane: Right,] - key: [F: 6,] - - action: [NewPane: Right, NewPane: Right,] - key: [F: 6,] diff --git a/src/common/config.rs b/src/common/input/config.rs similarity index 93% rename from src/common/config.rs rename to src/common/input/config.rs index 4a66acb0..b63ccef1 100644 --- a/src/common/config.rs +++ b/src/common/input/config.rs @@ -5,7 +5,7 @@ use std::fs::File; use std::io::{self, Read}; use std::path::{Path, PathBuf}; -use super::input::keybinds::{Keybinds, KeybindsFromYaml}; +use super::keybinds::{Keybinds, KeybindsFromYaml}; use crate::utils::logging::*; use directories_next::ProjectDirs; @@ -61,9 +61,6 @@ impl Config { Ok(Config::from_yaml(&yaml_config)?) } Err(e) => { - // TODO logging, if a file is not found - // at an expected position - should not - // panic, but log to file @a-kenji debug_log_to_file(format!( "{}\nUsing the default configuration!", ConfigError::IoPath(e, path.to_path_buf()) @@ -123,5 +120,5 @@ impl From for ConfigError { // The unit test location. #[cfg(test)] -#[path = "./input/ut/config_test.rs"] +#[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>); +//let actions = vec![Action::NoOp, Action::GoToTab(1), ]; +//let keyaction = KeyActionFromYaml { +//action : actions.clone(), +//key : vec![ Key::F(1), Key::Backspace , Key::Char('t'), ], +//}; +//} diff --git a/src/common/mod.rs b/src/common/mod.rs index c9b4959a..8351bc4a 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,5 +1,4 @@ pub mod command_is_executing; -pub mod config; pub mod errors; pub mod input; pub mod install; @@ -27,7 +26,7 @@ use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; use wasmer_wasi::{Pipe, WasiState}; use crate::cli::CliArgs; -use crate::common::config::Config; +use crate::common::input::config::Config; use crate::layout::Layout; use crate::utils::logging::*; use command_is_executing::CommandIsExecuting; diff --git a/src/tests/fixtures/config/functioning_simple_config.yaml b/src/tests/fixtures/config/functioning_simple_config.yaml deleted file mode 100644 index e69de29b..00000000 diff --git a/src/tests/fixtures/config/multiple_keys_for_one_action.yaml b/src/tests/fixtures/config/multiple_keys_for_one_action.yaml deleted file mode 100644 index 7da62541..00000000 --- a/src/tests/fixtures/config/multiple_keys_for_one_action.yaml +++ /dev/null @@ -1,8 +0,0 @@ ---- -keybindings: - Normal: - - ? - F: 6 - - F: 7 - - F: 8 - : - {GoToTab: 5} - From 1da743b1c7eba86ff0178a043053978596583a50 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 13 Mar 2021 10:20:56 +0100 Subject: [PATCH 07/23] Add more options to example config file --- example/config.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/example/config.yaml b/example/config.yaml index f19a2114..fcd75a66 100644 --- a/example/config.yaml +++ b/example/config.yaml @@ -11,3 +11,11 @@ keybinds: key: [F: 4,] - action: [NewTab,] key: [F: 5,] + - action: [SwitchToMode: Pane , MoveFocus: Left, SwitchToMode: Normal,] + key: [ Alt: h,] + - action: [SwitchToMode: Pane , MoveFocus: Right, SwitchToMode: Normal,] + key: [ Alt: l,] + - action: [SwitchToMode: Pane , MoveFocus: Down, SwitchToMode: Normal,] + key: [ Alt: j,] + - action: [SwitchToMode: Pane , MoveFocus: Up, SwitchToMode: Normal,] + key: [ Alt: k,] From cac143db76344b90202762af029b8263cc651649 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 13 Mar 2021 10:37:38 +0100 Subject: [PATCH 08/23] Modify example config file --- example/config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/config.yaml b/example/config.yaml index fcd75a66..a91f24f9 100644 --- a/example/config.yaml +++ b/example/config.yaml @@ -11,11 +11,11 @@ keybinds: key: [F: 4,] - action: [NewTab,] key: [F: 5,] - - action: [SwitchToMode: Pane , MoveFocus: Left, SwitchToMode: Normal,] + - action: [MoveFocus: Left,] key: [ Alt: h,] - - action: [SwitchToMode: Pane , MoveFocus: Right, SwitchToMode: Normal,] + - action: [MoveFocus: Right,] key: [ Alt: l,] - - action: [SwitchToMode: Pane , MoveFocus: Down, SwitchToMode: Normal,] + - action: [MoveFocus: Down,] key: [ Alt: j,] - - action: [SwitchToMode: Pane , MoveFocus: Up, SwitchToMode: Normal,] + - action: [MoveFocus: Up,] key: [ Alt: k,] From 9c0762fc53b567a34d234b2f152e49add895a620 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 13 Mar 2021 12:25:28 +0100 Subject: [PATCH 09/23] Update default config file --- example/config.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/example/config.yaml b/example/config.yaml index a91f24f9..3bb4fbfa 100644 --- a/example/config.yaml +++ b/example/config.yaml @@ -19,3 +19,8 @@ keybinds: key: [ Alt: j,] - action: [MoveFocus: Up,] key: [ Alt: k,] + pane: + - action: [ NewPane:, SwitchToMode: Normal,] + key: [Char: 'n',] + - action: [ NewPane: , ] + key: [Char: 'N',] From e0d0502539a558b547df7206f621034605021463 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 13 Mar 2021 12:48:27 +0100 Subject: [PATCH 10/23] Add config to README --- README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.md b/README.md index f9bc9fe0..e1986d9c 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,42 @@ Zellij was initially called "Mosaic". The status bar on the bottom should guide you through the possible keyboard shortcuts in the app. +# Configuration +It is possible to configure keyboard shortcuts and their actions in a yaml file. +An example file can be found under `example/config.yaml`. + +To pass a config file to zellij run it either with: +`cargo run -- --config [FILE]` or `zellij --config [FILE]`. + +The structure is as follows: +``` +keybinds: + normal: + - action: [] + key: [] +``` +`normal` is one of the `modes` zellij can be in. +It is possible to bind a sequence of actions to numerous keys at the same time. + + +For example: +``` +keybinds: + normal: + - action: [ NewTab, GoToTab: 1,] + key: [ Char: 'c',] +``` +Will create a new tab and then switch to tab number 1 on pressing the +`c` key. +Whereas: +``` +keybinds: + normal: + - action: [ NewTab,] + key: [ Char: 'c', Char: 'd',] +``` +Will create a new tab on pressing either the `c` or the `d` key. + # What is the current status of the project? Zellij is in the last stages of being VT compatible. As much as modern terminals are. From 2ce034a66acb09a826b124f9d83e57f611ff9aed Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 13 Mar 2021 13:07:46 +0100 Subject: [PATCH 11/23] Remove unneccessary comment --- src/common/input/handler.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index dc9fa382..49d87124 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -78,8 +78,6 @@ impl InputHandler { // been revised. Sorry about this (@categorille) if { let mut should_break = false; - // Hacked on way to have a means of testing Macros, needs to - // get properly integrated for action in Keybinds::key_to_actions(&key, raw_bytes, &self.mode, &keybinds) { From d3a72a27df5021d35600071b96edd8c0f79e9105 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 16 Mar 2021 21:14:10 +0100 Subject: [PATCH 12/23] Add key events documentation Add key events documentation from termion to the README --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1986d9c..eea40060 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ The status bar on the bottom should guide you through the possible keyboard shor It is possible to configure keyboard shortcuts and their actions in a yaml file. An example file can be found under `example/config.yaml`. -To pass a config file to zellij run it either with: +Zellij will look for a file `/zellij/config.yaml` in the default configuration location of your os. + +To pass a config file directly to zellij run it either with: `cargo run -- --config [FILE]` or `zellij --config [FILE]`. The structure is as follows: @@ -52,7 +54,7 @@ keybinds: ``` `normal` is one of the `modes` zellij can be in. It is possible to bind a sequence of actions to numerous keys at the same time. - +Here a reference to the [Key](https://docs.rs/termion/1.5.6/termion/event/enum.Key.html) format that is used. For example: ``` From 564ded0bb3a47ab0918e20d5505385f35d31906b Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 16 Mar 2021 21:22:59 +0100 Subject: [PATCH 13/23] Add short --config -c option --- assets/completions/_zellij | 1 + assets/completions/zellij.bash | 6 +++++- assets/completions/zellij.fish | 2 +- src/cli.rs | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/assets/completions/_zellij b/assets/completions/_zellij index f292d560..cd8dce56 100644 --- a/assets/completions/_zellij +++ b/assets/completions/_zellij @@ -22,6 +22,7 @@ _zellij() { '--max-panes=[Maximum panes on screen, caution: opening more panes will close old ones]' \ '-l+[Path to a layout yaml file]' \ '--layout=[Path to a layout yaml file]' \ +'-c+[Path to the configuration yaml file]' \ '--config=[Path to the configuration yaml file]' \ '-m[Send "move focused pane" to active zellij session]' \ '--move-focus[Send "move focused pane" to active zellij session]' \ diff --git a/assets/completions/zellij.bash b/assets/completions/zellij.bash index 17afd610..0ede5cce 100644 --- a/assets/completions/zellij.bash +++ b/assets/completions/zellij.bash @@ -20,7 +20,7 @@ _zellij() { case "${cmd}" in zellij) - opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --layout --config " + opts=" -m -d -h -V -s -o -l -c --move-focus --debug --help --version --split --open-file --max-panes --layout --config " if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -59,6 +59,10 @@ _zellij() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + -c) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; *) COMPREPLY=() ;; diff --git a/assets/completions/zellij.fish b/assets/completions/zellij.fish index 36425a25..eaba9fa4 100644 --- a/assets/completions/zellij.fish +++ b/assets/completions/zellij.fish @@ -2,7 +2,7 @@ complete -c zellij -n "__fish_use_subcommand" -s s -l split -d 'Send "split (dir complete -c zellij -n "__fish_use_subcommand" -s o -l open-file -d 'Send "open file in new pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -l max-panes -d 'Maximum panes on screen, caution: opening more panes will close old ones' complete -c zellij -n "__fish_use_subcommand" -s l -l layout -d 'Path to a layout yaml file' -complete -c zellij -n "__fish_use_subcommand" -l config -d 'Path to the configuration yaml file' +complete -c zellij -n "__fish_use_subcommand" -s c -l config -d 'Path to the configuration yaml file' complete -c zellij -n "__fish_use_subcommand" -s m -l move-focus -d 'Send "move focused pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -s d -l debug complete -c zellij -n "__fish_use_subcommand" -s h -l help -d 'Prints help information' diff --git a/src/cli.rs b/src/cli.rs index d6d3057e..67a92196 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -25,7 +25,7 @@ pub struct CliArgs { pub layout: Option, /// Path to the configuration yaml file - #[structopt(long)] + #[structopt(short, long)] pub config: Option, #[structopt(short, long)] From 224c606b4507c3a350a9bf50d01d1ddad380649a Mon Sep 17 00:00:00 2001 From: a-kenji Date: Tue, 16 Mar 2021 21:34:50 +0100 Subject: [PATCH 14/23] Remove logging to log.txt --- src/common/input/config.rs | 9 +-------- src/common/mod.rs | 2 -- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/common/input/config.rs b/src/common/input/config.rs index b63ccef1..d9d1f784 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -6,7 +6,6 @@ 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; @@ -49,8 +48,6 @@ impl Config { } /// 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 { match File::open(path) { @@ -60,11 +57,7 @@ impl 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()) - )); + Err(_) => { Ok(Config::default()) } } diff --git a/src/common/mod.rs b/src/common/mod.rs index 8351bc4a..02e75f98 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -28,7 +28,6 @@ use wasmer_wasi::{Pipe, WasiState}; use crate::cli::CliArgs; use crate::common::input::config::Config; use crate::layout::Layout; -use crate::utils::logging::*; use command_is_executing::CommandIsExecuting; use errors::{AppContext, ContextType, ErrorContext, PluginContext, PtyContext, ScreenContext}; use input::handler::input_loop; @@ -173,7 +172,6 @@ pub fn start(mut os_input: Box, opts: CliArgs) { #[allow(unused_must_use)] let config = Config::from_option_or_default(opts.config) .map_err(|e| { - debug_log_to_file(format!("There was an error in the config file:\n{}", e)); eprintln!("There was an error in the config file:\n{}", e); std::process::exit(1); }) From e64192b42e584663ac6a64a73cff1921f098d945 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 17 Mar 2021 16:56:02 +0100 Subject: [PATCH 15/23] Add a clean config option Add a clean config option, which makes zellij not look for a default configuration file. --- assets/completions/_zellij | 49 +++++++++++++++++++++++++++++++--- assets/completions/zellij.bash | 46 ++++++++++++++++++++++++------- assets/completions/zellij.fish | 8 +++++- src/cli.rs | 18 ++++++++++--- src/common/input/config.rs | 21 ++++++++++----- src/common/mod.rs | 3 +-- 6 files changed, 119 insertions(+), 26 deletions(-) diff --git a/assets/completions/_zellij b/assets/completions/_zellij index cd8dce56..8b89db14 100644 --- a/assets/completions/_zellij +++ b/assets/completions/_zellij @@ -22,8 +22,6 @@ _zellij() { '--max-panes=[Maximum panes on screen, caution: opening more panes will close old ones]' \ '-l+[Path to a layout yaml file]' \ '--layout=[Path to a layout yaml file]' \ -'-c+[Path to the configuration yaml file]' \ -'--config=[Path to the configuration yaml file]' \ '-m[Send "move focused pane" to active zellij session]' \ '--move-focus[Send "move focused pane" to active zellij session]' \ '-d[]' \ @@ -32,16 +30,59 @@ _zellij() { '--help[Prints help information]' \ '-V[Prints version information]' \ '--version[Prints version information]' \ +":: :_zellij_commands" \ +"*::: :->zellij" \ && ret=0 - + case $state in + (zellij) + words=($line[1] "${words[@]}") + (( CURRENT += 1 )) + curcontext="${curcontext%:*:*}:zellij-command-$line[1]:" + case $line[1] in + (config) +_arguments "${_arguments_options[@]}" \ +'--clean[Disables loading of configuration file at default location]' \ +'-h[Prints help information]' \ +'--help[Prints help information]' \ +'-V[Prints version information]' \ +'--version[Prints version information]' \ +'::path:_files' \ +&& ret=0 +;; +(help) +_arguments "${_arguments_options[@]}" \ +'-h[Prints help information]' \ +'--help[Prints help information]' \ +'-V[Prints version information]' \ +'--version[Prints version information]' \ +&& ret=0 +;; + esac + ;; +esac } (( $+functions[_zellij_commands] )) || _zellij_commands() { local commands; commands=( - + "config:Path to the configuration yaml file" \ +"help:Prints this message or the help of the given subcommand(s)" \ ) _describe -t commands 'zellij commands' commands "$@" } +(( $+functions[_zellij__config_commands] )) || +_zellij__config_commands() { + local commands; commands=( + + ) + _describe -t commands 'zellij config commands' commands "$@" +} +(( $+functions[_zellij__help_commands] )) || +_zellij__help_commands() { + local commands; commands=( + + ) + _describe -t commands 'zellij help commands' commands "$@" +} _zellij "$@" \ No newline at end of file diff --git a/assets/completions/zellij.bash b/assets/completions/zellij.bash index 0ede5cce..549b1e9f 100644 --- a/assets/completions/zellij.bash +++ b/assets/completions/zellij.bash @@ -13,6 +13,12 @@ _zellij() { cmd="zellij" ;; + config) + cmd+="__config" + ;; + help) + cmd+="__help" + ;; *) ;; esac @@ -20,7 +26,7 @@ _zellij() { case "${cmd}" in zellij) - opts=" -m -d -h -V -s -o -l -c --move-focus --debug --help --version --split --open-file --max-panes --layout --config " + opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --layout config help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -55,14 +61,6 @@ _zellij() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; - --config) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -c) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; *) COMPREPLY=() ;; @@ -71,6 +69,36 @@ _zellij() { return 0 ;; + zellij__config) + opts=" -h -V --clean --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; + zellij__help) + opts=" -h -V --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; esac } diff --git a/assets/completions/zellij.fish b/assets/completions/zellij.fish index eaba9fa4..36bab192 100644 --- a/assets/completions/zellij.fish +++ b/assets/completions/zellij.fish @@ -2,8 +2,14 @@ complete -c zellij -n "__fish_use_subcommand" -s s -l split -d 'Send "split (dir complete -c zellij -n "__fish_use_subcommand" -s o -l open-file -d 'Send "open file in new pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -l max-panes -d 'Maximum panes on screen, caution: opening more panes will close old ones' complete -c zellij -n "__fish_use_subcommand" -s l -l layout -d 'Path to a layout yaml file' -complete -c zellij -n "__fish_use_subcommand" -s c -l config -d 'Path to the configuration yaml file' complete -c zellij -n "__fish_use_subcommand" -s m -l move-focus -d 'Send "move focused pane" to active zellij session' complete -c zellij -n "__fish_use_subcommand" -s d -l debug complete -c zellij -n "__fish_use_subcommand" -s h -l help -d 'Prints help information' complete -c zellij -n "__fish_use_subcommand" -s V -l version -d 'Prints version information' +complete -c zellij -n "__fish_use_subcommand" -f -a "config" -d 'Path to the configuration yaml file' +complete -c zellij -n "__fish_use_subcommand" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)' +complete -c zellij -n "__fish_seen_subcommand_from config" -l clean -d 'Disables loading of configuration file at default location' +complete -c zellij -n "__fish_seen_subcommand_from config" -s h -l help -d 'Prints help information' +complete -c zellij -n "__fish_seen_subcommand_from config" -s V -l version -d 'Prints version information' +complete -c zellij -n "__fish_seen_subcommand_from help" -s h -l help -d 'Prints help information' +complete -c zellij -n "__fish_seen_subcommand_from help" -s V -l version -d 'Prints version information' diff --git a/src/cli.rs b/src/cli.rs index 67a92196..740879f0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use structopt::StructOpt; -#[derive(StructOpt, Debug, Default)] +#[derive(StructOpt, Debug)] #[structopt(name = "zellij")] pub struct CliArgs { /// Send "split (direction h == horizontal / v == vertical)" to active zellij session @@ -24,10 +24,20 @@ pub struct CliArgs { #[structopt(short, long)] pub layout: Option, - /// Path to the configuration yaml file - #[structopt(short, long)] - pub config: Option, + #[structopt(subcommand)] + pub config: Option, #[structopt(short, long)] pub debug: bool, } + +#[derive(Debug, StructOpt)] +pub enum ConfigCli { + /// Path to the configuration yaml file + Config { + path: Option, + #[structopt(long)] + /// Disables loading of configuration file at default location + clean: bool, + }, +} diff --git a/src/common/input/config.rs b/src/common/input/config.rs index d9d1f784..668b4106 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -6,10 +6,13 @@ use std::io::{self, Read}; use std::path::{Path, PathBuf}; use super::keybinds::{Keybinds, KeybindsFromYaml}; +use crate::cli::ConfigCli; use directories_next::ProjectDirs; use serde::Deserialize; +type ConfigResult = Result; + /// Intermediate deserialisation config struct #[derive(Debug, Deserialize)] pub struct ConfigFromYaml { @@ -41,7 +44,7 @@ impl Default for Config { impl Config { /// Uses defaults, but lets config override them. - pub fn from_yaml(yaml_config: &str) -> Result { + pub fn from_yaml(yaml_config: &str) -> ConfigResult { 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 }) @@ -49,7 +52,7 @@ impl Config { /// Deserializes from given path. #[allow(unused_must_use)] - pub fn new(path: &Path) -> Result { + pub fn new(path: &Path) -> ConfigResult { match File::open(path) { Ok(mut file) => { let mut yaml_config = String::new(); @@ -57,15 +60,13 @@ impl Config { .map_err(|e| ConfigError::IoPath(e, path.to_path_buf()))?; Ok(Config::from_yaml(&yaml_config)?) } - Err(_) => { - Ok(Config::default()) - } + Err(_) => 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) -> Result { + fn from_option_or_default(option: Option) -> ConfigResult { if let Some(config_path) = option { Ok(Config::new(&config_path)?) } else { @@ -75,6 +76,14 @@ impl Config { Ok(Config::new(&config_path)?) } } + + pub fn from_cli_config(cli_config: Option) -> ConfigResult { + match cli_config { + Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()), + Some(ConfigCli::Config { path, .. }) => Ok(Config::from_option_or_default(path)?), + None => Ok(Config::default()), + } + } } impl Display for ConfigError { diff --git a/src/common/mod.rs b/src/common/mod.rs index 02e75f98..777b615f 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -169,8 +169,7 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let mut app_state = AppState::default(); - #[allow(unused_must_use)] - let config = Config::from_option_or_default(opts.config) + let config = Config::from_cli_config(opts.config) .map_err(|e| { eprintln!("There was an error in the config file:\n{}", e); std::process::exit(1); From 91c109a90245edb3791cd3234787a8a1f6a8f794 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 17 Mar 2021 22:50:03 +0100 Subject: [PATCH 16/23] Move test to /unit --- src/cli.rs | 2 +- src/common/input/config.rs | 20 +++++++++++++++++-- src/common/input/keybinds.rs | 2 +- .../input/{ut => unit}/keybinds_test.rs | 0 src/common/input/ut/config_test.rs | 19 ------------------ 5 files changed, 20 insertions(+), 23 deletions(-) rename src/common/input/{ut => unit}/keybinds_test.rs (100%) delete mode 100644 src/common/input/ut/config_test.rs diff --git a/src/cli.rs b/src/cli.rs index 740879f0..d4c0f78a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use structopt::StructOpt; -#[derive(StructOpt, Debug)] +#[derive(StructOpt, Default, Debug)] #[structopt(name = "zellij")] pub struct CliArgs { /// Send "split (direction h == horizontal / v == vertical)" to active zellij session diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 668b4106..7d804d68 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -122,5 +122,21 @@ impl From for ConfigError { // The unit test location. #[cfg(test)] -#[path = "./ut/config_test.rs"] -mod config_test; +mod config_test { + use super::*; + + #[test] + fn no_config_file_equals_default_config() { + let no_file = PathBuf::from(r"../fixtures/config/config.yamlll"); + let config = Config::from_option_or_default(Some(no_file)).unwrap(); + let default = Config::default(); + assert_eq!(config, default); + } + + #[test] + fn no_config_option_file_equals_default_config() { + let config = Config::from_option_or_default(None).unwrap(); + let default = Config::default(); + assert_eq!(config, default); + } +} diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 0c37693c..c70123b6 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -368,5 +368,5 @@ impl From for ModeKeybinds { // The unit test location. #[cfg(test)] -#[path = "./ut/keybinds_test.rs"] +#[path = "./unit/keybinds_test.rs"] mod keybinds_test; diff --git a/src/common/input/ut/keybinds_test.rs b/src/common/input/unit/keybinds_test.rs similarity index 100% rename from src/common/input/ut/keybinds_test.rs rename to src/common/input/unit/keybinds_test.rs diff --git a/src/common/input/ut/config_test.rs b/src/common/input/ut/config_test.rs deleted file mode 100644 index eb4bc960..00000000 --- a/src/common/input/ut/config_test.rs +++ /dev/null @@ -1,19 +0,0 @@ -//! For Configuration Options - -use super::super::config::*; -use std::path::PathBuf; - -#[test] -fn no_config_file_equals_default_config() { - let no_file = PathBuf::from(r"../fixtures/config/config.yamlll"); - let config = Config::from_option_or_default(Some(no_file)).unwrap(); - let default = Config::default(); - assert_eq!(config, default); -} - -#[test] -fn no_config_option_file_equals_default_config() { - let config = Config::from_option_or_default(None).unwrap(); - let default = Config::default(); - assert_eq!(config, default); -} From b30400ac4746e3b40243700fdd4425f671864cae Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 26 Mar 2021 21:15:12 +0100 Subject: [PATCH 17/23] Fix config default location The default location is now correctly used again --- src/common/input/config.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 7d804d68..4678343e 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -60,28 +60,30 @@ impl Config { .map_err(|e| ConfigError::IoPath(e, path.to_path_buf()))?; Ok(Config::from_yaml(&yaml_config)?) } - Err(_) => Ok(Config::default()), + Err(e) => Err(ConfigError::IoPath(e, path.into())), } } - /// Deserializes the config from an optional path, or a platform specific path, + /// Deserializes the config from a default platform specific path, /// merges the default configuration - options take precedence. - fn from_option_or_default(option: Option) -> ConfigResult { - 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)?) + fn from_default_path() -> ConfigResult { + 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"); + + match Config::new(&config_path) { + Ok(config) => Ok(config), + Err(ConfigError::IoPath(_,_)) => Ok(Config::default()), + Err(e) => Err(e), } } + /// Entry point of the configuration pub fn from_cli_config(cli_config: Option) -> ConfigResult { match cli_config { Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()), - Some(ConfigCli::Config { path, .. }) => Ok(Config::from_option_or_default(path)?), - None => Ok(Config::default()), + Some(ConfigCli::Config { path, .. }) if path.is_some()=> Ok(Config::new(&path.unwrap())?), + Some(_) | None => Ok(Config::from_default_path()?), } } } From 98c4227c816bf2852aac7a638cf9f955451c5f35 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 26 Mar 2021 21:59:20 +0100 Subject: [PATCH 18/23] Fix tests --- src/common/input/config.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 4678343e..d7420f67 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -128,16 +128,17 @@ mod config_test { use super::*; #[test] - fn no_config_file_equals_default_config() { + fn clean_option_equals_default_config() { let no_file = PathBuf::from(r"../fixtures/config/config.yamlll"); - let config = Config::from_option_or_default(Some(no_file)).unwrap(); + let cli_config = ConfigCli::Config { path: Some(no_file) ,clean: true}; + let config = Config::from_cli_config(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_option_or_default(None).unwrap(); + let config = Config::from_cli_config(None).unwrap(); let default = Config::default(); assert_eq!(config, default); } From a80d5f700b9d8b5544f560ce382ad86527453960 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 26 Mar 2021 21:59:38 +0100 Subject: [PATCH 19/23] (Chore): rustfmt --- src/common/input/config.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/common/input/config.rs b/src/common/input/config.rs index d7420f67..0713e54e 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -73,7 +73,7 @@ impl Config { match Config::new(&config_path) { Ok(config) => Ok(config), - Err(ConfigError::IoPath(_,_)) => Ok(Config::default()), + Err(ConfigError::IoPath(_, _)) => Ok(Config::default()), Err(e) => Err(e), } } @@ -82,7 +82,9 @@ impl Config { pub fn from_cli_config(cli_config: Option) -> ConfigResult { 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(ConfigCli::Config { path, .. }) if path.is_some() => { + Ok(Config::new(&path.unwrap())?) + } Some(_) | None => Ok(Config::from_default_path()?), } } @@ -130,7 +132,10 @@ 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 cli_config = ConfigCli::Config { + path: Some(no_file), + clean: true, + }; let config = Config::from_cli_config(Some(cli_config)).unwrap(); let default = Config::default(); assert_eq!(config, default); From 68737f78ac247814f2ad79e27d4f0da7c960b4a9 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 26 Mar 2021 22:12:01 +0100 Subject: [PATCH 20/23] Config: Fix README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eea40060..203de8cc 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ An example file can be found under `example/config.yaml`. Zellij will look for a file `/zellij/config.yaml` in the default configuration location of your os. To pass a config file directly to zellij run it either with: -`cargo run -- --config [FILE]` or `zellij --config [FILE]`. +`cargo run -- config [FILE]` or `zellij config [FILE]`. The structure is as follows: ``` From a4430bf1580a0ab09706179867bb09ef1b19fc8e Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 3 Apr 2021 21:28:57 +0200 Subject: [PATCH 21/23] Chore(rustfmt) --- src/common/input/handler.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 7db14d64..2020fc20 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -82,11 +82,10 @@ impl InputHandler { break 'input_loop; } } - termion::event::Event::Mouse(_) - | termion::event::Event::Unsupported(_) => { - // Mouse and unsupported events aren't implemented yet, - // use a NoOp untill then. - } + termion::event::Event::Mouse(_) | termion::event::Event::Unsupported(_) => { + // Mouse and unsupported events aren't implemented yet, + // use a NoOp untill then. + } }, Err(err) => panic!("Encountered read error: {:?}", err), } From ca014d7aaced18c534c5a3577906922db0a58e75 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 4 Apr 2021 18:03:24 +0200 Subject: [PATCH 22/23] Use Default Configuration for Tests Uses the default configuration for tests, in order to not mess up test cases with different configurations in the config file. --- src/common/input/config.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 0713e54e..4bb9e10f 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -79,6 +79,7 @@ impl Config { } /// Entry point of the configuration + #[cfg(not(test))] pub fn from_cli_config(cli_config: Option) -> ConfigResult { match cli_config { Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()), @@ -88,6 +89,13 @@ impl Config { 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) -> ConfigResult { + Ok(Config::default()) + } } impl Display for ConfigError { From d9a85e977d4c81f6efcf73785b1e17175d3edc59 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 4 Apr 2021 19:27:28 +0200 Subject: [PATCH 23/23] Readd short option for config --- assets/completions/_zellij | 36 +++++++++++++++++++++++++++++++++- assets/completions/zellij.bash | 20 ++++++++++++++++++- src/cli.rs | 1 + 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/assets/completions/_zellij b/assets/completions/_zellij index 8b89db14..fc1cd00f 100644 --- a/assets/completions/_zellij +++ b/assets/completions/_zellij @@ -39,7 +39,27 @@ _zellij() { (( CURRENT += 1 )) curcontext="${curcontext%:*:*}:zellij-command-$line[1]:" case $line[1] in - (config) + (c) +_arguments "${_arguments_options[@]}" \ +'--clean[Disables loading of configuration file at default location]' \ +'-h[Prints help information]' \ +'--help[Prints help information]' \ +'-V[Prints version information]' \ +'--version[Prints version information]' \ +'::path:_files' \ +&& ret=0 +;; +(c) +_arguments "${_arguments_options[@]}" \ +'--clean[Disables loading of configuration file at default location]' \ +'-h[Prints help information]' \ +'--help[Prints help information]' \ +'-V[Prints version information]' \ +'--version[Prints version information]' \ +'::path:_files' \ +&& ret=0 +;; +(config) _arguments "${_arguments_options[@]}" \ '--clean[Disables loading of configuration file at default location]' \ '-h[Prints help information]' \ @@ -70,6 +90,20 @@ _zellij_commands() { ) _describe -t commands 'zellij commands' commands "$@" } +(( $+functions[_c_commands] )) || +_c_commands() { + local commands; commands=( + + ) + _describe -t commands 'c commands' commands "$@" +} +(( $+functions[_zellij__c_commands] )) || +_zellij__c_commands() { + local commands; commands=( + + ) + _describe -t commands 'zellij c commands' commands "$@" +} (( $+functions[_zellij__config_commands] )) || _zellij__config_commands() { local commands; commands=( diff --git a/assets/completions/zellij.bash b/assets/completions/zellij.bash index 549b1e9f..10c18d53 100644 --- a/assets/completions/zellij.bash +++ b/assets/completions/zellij.bash @@ -13,6 +13,9 @@ _zellij() { cmd="zellij" ;; + c) + cmd+="__c" + ;; config) cmd+="__config" ;; @@ -26,7 +29,7 @@ _zellij() { case "${cmd}" in zellij) - opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --layout config help" + opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --layout config help c c" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -69,6 +72,21 @@ _zellij() { return 0 ;; + zellij__c) + opts=" -h -V --clean --help --version " + if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi + case "${prev}" in + + *) + COMPREPLY=() + ;; + esac + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + ;; zellij__config) opts=" -h -V --clean --help --version " if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then diff --git a/src/cli.rs b/src/cli.rs index d4c0f78a..b1fb8f38 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -34,6 +34,7 @@ pub struct CliArgs { #[derive(Debug, StructOpt)] pub enum ConfigCli { /// Path to the configuration yaml file + #[structopt(alias = "c")] Config { path: Option, #[structopt(long)]