mirror of
https://github.com/zellij-org/zellij.git
synced 2024-11-23 19:10:09 +03:00
Merge pull request #300 from a-kenji/config-command
Change Config To Flag (#290)
This commit is contained in:
commit
4863fb7b5e
@ -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 "$@"
|
@ -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 <path> "
|
||||
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 <path> "
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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'
|
||||
|
14
src/cli.rs
14
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<PathBuf>,
|
||||
|
||||
/// Change where zellij looks for the configuration
|
||||
#[structopt(short, long, env=ZELLIJ_CONFIG_ENV)]
|
||||
pub config: Option<PathBuf>,
|
||||
|
||||
#[structopt(subcommand)]
|
||||
pub config: Option<ConfigCli>,
|
||||
pub option: Option<ConfigCli>,
|
||||
|
||||
#[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<PathBuf>,
|
||||
#[structopt(long)]
|
||||
/// Disables loading of configuration file at default location
|
||||
clean: bool,
|
||||
|
@ -79,19 +79,23 @@ impl Config {
|
||||
|
||||
/// Entry point of the configuration
|
||||
#[cfg(not(test))]
|
||||
pub fn from_cli_config(cli_config: Option<ConfigCli>) -> ConfigResult {
|
||||
pub fn from_cli_config(
|
||||
location: Option<PathBuf>,
|
||||
cli_config: Option<ConfigCli>,
|
||||
) -> ConfigResult {
|
||||
if let Some(path) = location {
|
||||
return Config::new(&path);
|
||||
}
|
||||
|
||||
match cli_config {
|
||||
Some(ConfigCli::Config { clean, .. }) if clean => Ok(Config::default()),
|
||||
Some(ConfigCli::Config { path, .. }) if path.is_some() => {
|
||||
Ok(Config::new(&path.unwrap())?)
|
||||
}
|
||||
Some(_) | None => Ok(Config::from_default_path()?),
|
||||
}
|
||||
}
|
||||
|
||||
/// In order not to mess up tests from changing configurations
|
||||
#[cfg(test)]
|
||||
pub fn from_cli_config(_: Option<ConfigCli>) -> ConfigResult {
|
||||
pub fn from_cli_config(_: Option<PathBuf>, _: Option<ConfigCli>) -> ConfigResult {
|
||||
Ok(Config::default())
|
||||
}
|
||||
}
|
||||
@ -137,19 +141,15 @@ mod config_test {
|
||||
|
||||
#[test]
|
||||
fn clean_option_equals_default_config() {
|
||||
let no_file = PathBuf::from(r"../fixtures/config/config.yamlll");
|
||||
let cli_config = ConfigCli::Config {
|
||||
path: Some(no_file),
|
||||
clean: true,
|
||||
};
|
||||
let config = Config::from_cli_config(Some(cli_config)).unwrap();
|
||||
let cli_config = ConfigCli::Config { clean: true };
|
||||
let config = Config::from_cli_config(None, Some(cli_config)).unwrap();
|
||||
let default = Config::default();
|
||||
assert_eq!(config, default);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_config_option_file_equals_default_config() {
|
||||
let config = Config::from_cli_config(None).unwrap();
|
||||
let config = Config::from_cli_config(None, None).unwrap();
|
||||
let default = Config::default();
|
||||
assert_eq!(config, default);
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, 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);
|
||||
|
Loading…
Reference in New Issue
Block a user