From 2c12e8ff8324b3ebaa10ac8162af09f5da295e1b Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 21 Apr 2021 08:58:38 +0200 Subject: [PATCH 1/2] Change Config To Flag (#290) This changes the `config` subcommand to a config flag. Add option subcommand for now, for the clearing of default path. Add `ZELLIJ_CONFIG` environment variable. The configuration locations are now as follows: * `--config` flag * `ZELLIJ_CONFIG` environment variable * default config location in that order. --- assets/completions/_zellij | 55 ++++++----------------------- assets/completions/zellij.bash | 64 ++++++++++++++-------------------- assets/completions/zellij.fish | 9 ++--- src/cli.rs | 14 +++++--- src/common/input/config.rs | 20 ++++++----- src/common/mod.rs | 2 +- 6 files changed, 65 insertions(+), 99 deletions(-) diff --git a/assets/completions/_zellij b/assets/completions/_zellij index 9eecf8e2..774a28fa 100644 --- a/assets/completions/_zellij +++ b/assets/completions/_zellij @@ -23,6 +23,8 @@ _zellij() { '--data-dir=[Change where zellij looks for layouts and plugins]' \ '-l+[Path to a layout yaml file]' \ '--layout=[Path to a layout yaml file]' \ +'-c+[Change where zellij looks for the configuration]' \ +'--config=[Change where zellij looks for the configuration]' \ '-m[Send "move focused pane" to active zellij session]' \ '--move-focus[Send "move focused pane" to active zellij session]' \ '-d[]' \ @@ -40,34 +42,13 @@ _zellij() { (( CURRENT += 1 )) curcontext="${curcontext%:*:*}:zellij-command-$line[1]:" case $line[1] in - (c) + (option) _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]' \ -'--help[Prints help information]' \ -'-V[Prints version information]' \ -'--version[Prints version information]' \ -'::path:_files' \ && ret=0 ;; (help) @@ -86,32 +67,11 @@ esac (( $+functions[_zellij_commands] )) || _zellij_commands() { local commands; commands=( - "config:Path to the configuration yaml file" \ + "option:Change the behaviour of zellij" \ "help:Prints this message or the help of the given subcommand(s)" \ ) _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=( - - ) - _describe -t commands 'zellij config commands' commands "$@" -} (( $+functions[_zellij__help_commands] )) || _zellij__help_commands() { local commands; commands=( @@ -119,5 +79,12 @@ _zellij__help_commands() { ) _describe -t commands 'zellij help commands' commands "$@" } +(( $+functions[_zellij__option_commands] )) || +_zellij__option_commands() { + local commands; commands=( + + ) + _describe -t commands 'zellij option commands' commands "$@" +} _zellij "$@" \ No newline at end of file diff --git a/assets/completions/zellij.bash b/assets/completions/zellij.bash index 4f28140a..9cd8f890 100644 --- a/assets/completions/zellij.bash +++ b/assets/completions/zellij.bash @@ -13,15 +13,12 @@ _zellij() { cmd="zellij" ;; - c) - cmd+="__c" - ;; - config) - cmd+="__config" - ;; help) cmd+="__help" ;; + option) + cmd+="__option" + ;; *) ;; esac @@ -29,7 +26,7 @@ _zellij() { case "${cmd}" in zellij) - opts=" -m -d -h -V -s -o -l --move-focus --debug --help --version --split --open-file --max-panes --data-dir --layout config help c c" + opts=" -m -d -h -V -s -o -l -c --move-focus --debug --help --version --split --open-file --max-panes --data-dir --layout --config option help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -68,6 +65,14 @@ _zellij() { COMPREPLY=($(compgen -f "${cur}")) return 0 ;; + --config) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; + -c) + COMPREPLY=($(compgen -f "${cur}")) + return 0 + ;; *) COMPREPLY=() ;; @@ -76,36 +81,6 @@ _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 - 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 @@ -121,6 +96,21 @@ _zellij() { COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 ;; + zellij__option) + 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 + ;; esac } diff --git a/assets/completions/zellij.fish b/assets/completions/zellij.fish index ec758be6..4c1e536f 100644 --- a/assets/completions/zellij.fish +++ b/assets/completions/zellij.fish @@ -3,14 +3,15 @@ complete -c zellij -n "__fish_use_subcommand" -s o -l open-file -d 'Send "open f 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" -l data-dir -d 'Change where zellij looks for layouts and plugins' 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 'Change where zellij looks for the configuration' 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 "option" -d 'Change the behaviour of zellij' 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 option" -l clean -d 'Disables loading of configuration file at default location' +complete -c zellij -n "__fish_seen_subcommand_from option" -s h -l help -d 'Prints help information' +complete -c zellij -n "__fish_seen_subcommand_from option" -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 43047dd3..008fb28c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,9 @@ use std::path::PathBuf; use structopt::StructOpt; +// TODO add to consts.rs +const ZELLIJ_CONFIG_ENV: &str = "ZELLIJ_CONFIG"; + #[derive(StructOpt, Default, Debug)] #[structopt(name = "zellij")] pub struct CliArgs { @@ -28,8 +31,12 @@ pub struct CliArgs { #[structopt(short, long)] pub layout: Option, + /// Change where zellij looks for the configuration + #[structopt(short, long, env=ZELLIJ_CONFIG_ENV)] + pub config: Option, + #[structopt(subcommand)] - pub config: Option, + pub option: Option, #[structopt(short, long)] pub debug: bool, @@ -37,10 +44,9 @@ pub struct CliArgs { #[derive(Debug, StructOpt)] pub enum ConfigCli { - /// Path to the configuration yaml file - #[structopt(alias = "c")] + /// Change the behaviour of zellij + #[structopt(name = "option")] 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 1926b11c..17e1edf1 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -79,19 +79,23 @@ impl Config { /// Entry point of the configuration #[cfg(not(test))] - pub fn from_cli_config(cli_config: Option) -> ConfigResult { + pub fn from_cli_config( + location: Option, + cli_config: Option, + ) -> ConfigResult { + if let Some(path) = location { + return Config::new(&path); + } + match cli_config { Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()), - Some(ConfigCli::Config { path, .. }) if path.is_some() => { - Ok(Config::new(&path.unwrap())?) - } Some(_) | None => Ok(Config::from_default_path()?), } } /// In order not to mess up tests from changing configurations #[cfg(test)] - pub fn from_cli_config(_: Option) -> ConfigResult { + pub fn from_cli_config(_: Option, _: Option) -> ConfigResult { Ok(Config::default()) } } @@ -137,19 +141,17 @@ mod config_test { #[test] fn clean_option_equals_default_config() { - let no_file = PathBuf::from(r"../fixtures/config/config.yamlll"); let cli_config = ConfigCli::Config { - path: Some(no_file), clean: true, }; - let config = Config::from_cli_config(Some(cli_config)).unwrap(); + let config = Config::from_cli_config(None, Some(cli_config)).unwrap(); let default = Config::default(); assert_eq!(config, default); } #[test] fn no_config_option_file_equals_default_config() { - let config = Config::from_cli_config(None).unwrap(); + let config = Config::from_cli_config(None, None).unwrap(); let default = Config::default(); assert_eq!(config, default); } diff --git a/src/common/mod.rs b/src/common/mod.rs index 860d12db..e2cd2220 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -128,7 +128,7 @@ pub fn start(mut os_input: Box, opts: CliArgs) { .write(take_snapshot.as_bytes()) .unwrap(); - let config = Config::from_cli_config(opts.config) + let config = Config::from_cli_config(opts.config, opts.option) .map_err(|e| { eprintln!("There was an error in the config file:\n{}", e); std::process::exit(1); From ae010964ff2251007b49b11bd6f6b131e9cc627a Mon Sep 17 00:00:00 2001 From: a-kenji Date: Wed, 21 Apr 2021 09:11:58 +0200 Subject: [PATCH 2/2] Chore(Rustfmt) --- src/common/input/config.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/common/input/config.rs b/src/common/input/config.rs index 17e1edf1..542116d9 100644 --- a/src/common/input/config.rs +++ b/src/common/input/config.rs @@ -141,9 +141,7 @@ mod config_test { #[test] fn clean_option_equals_default_config() { - let cli_config = ConfigCli::Config { - clean: true, - }; + let cli_config = ConfigCli::Config { clean: true }; let config = Config::from_cli_config(None, Some(cli_config)).unwrap(); let default = Config::default(); assert_eq!(config, default);