From 9aca589167fec7b5b309c60208bd4cd203bc9c6e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 25 Apr 2021 14:33:53 +0200 Subject: [PATCH 1/2] Add subcommand to generate completions Signed-off-by: Matthias Beyer --- assets/completions/_zellij | 90 ------------------------- assets/completions/zellij.bash | 117 --------------------------------- assets/completions/zellij.fish | 17 ----- build.rs | 23 ------- src/cli.rs | 6 ++ src/main.rs | 14 ++++ 6 files changed, 20 insertions(+), 247 deletions(-) delete mode 100644 assets/completions/_zellij delete mode 100644 assets/completions/zellij.bash delete mode 100644 assets/completions/zellij.fish delete mode 100644 build.rs diff --git a/assets/completions/_zellij b/assets/completions/_zellij deleted file mode 100644 index 774a28fa44..0000000000 --- a/assets/completions/_zellij +++ /dev/null @@ -1,90 +0,0 @@ -#compdef zellij - -autoload -U is-at-least - -_zellij() { - typeset -A opt_args - typeset -a _arguments_options - local ret=1 - - if is-at-least 5.2; then - _arguments_options=(-s -S -C) - else - _arguments_options=(-s -C) - fi - - local context curcontext="$curcontext" state line - _arguments "${_arguments_options[@]}" \ -'-s+[Send "split (direction h == horizontal / v == vertical)" to active zellij session]' \ -'--split=[Send "split (direction h == horizontal / v == vertical)" to active zellij session]' \ -'-o+[Send "open file in new pane" to active zellij session]' \ -'--open-file=[Send "open file in new pane" to active zellij session]' \ -'--max-panes=[Maximum panes on screen, caution: opening more panes will close old ones]' \ -'--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[]' \ -'--debug[]' \ -'-h[Prints help information]' \ -'--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 - (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]' \ -&& 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=( - "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[_zellij__help_commands] )) || -_zellij__help_commands() { - local commands; 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 deleted file mode 100644 index 9cd8f89082..0000000000 --- a/assets/completions/zellij.bash +++ /dev/null @@ -1,117 +0,0 @@ -_zellij() { - local i cur prev opts cmds - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - cmd="" - opts="" - - for i in ${COMP_WORDS[@]} - do - case "${i}" in - zellij) - cmd="zellij" - ;; - - help) - cmd+="__help" - ;; - option) - cmd+="__option" - ;; - *) - ;; - esac - done - - case "${cmd}" in - zellij) - 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 - fi - case "${prev}" in - - --split) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -s) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - --open-file) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -o) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - --max-panes) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - --data-dir) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - --layout) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -l) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - --config) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -c) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - *) - 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 - ;; - 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 -} - -complete -F _zellij -o bashdefault -o default zellij diff --git a/assets/completions/zellij.fish b/assets/completions/zellij.fish deleted file mode 100644 index 4c1e536f11..0000000000 --- a/assets/completions/zellij.fish +++ /dev/null @@ -1,17 +0,0 @@ -complete -c zellij -n "__fish_use_subcommand" -s s -l split -d 'Send "split (direction h == horizontal / v == vertical)" to active zellij session' -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" -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 "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 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/build.rs b/build.rs deleted file mode 100644 index 3240db67e0..0000000000 --- a/build.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::fs; -use structopt::clap::Shell; - -include!("src/cli.rs"); - -const BIN_NAME: &str = "zellij"; - -fn main() { - // Generate Shell Completions - let mut clap_app = CliArgs::clap(); - println!("cargo:rerun-if-changed=src/cli.rs"); - let mut out_dir = std::env::var_os("CARGO_MANIFEST_DIR").unwrap(); - out_dir.push("/assets/completions"); - - println!( - "Completion files will to added to this location: {:?}", - out_dir - ); - fs::create_dir_all(&out_dir).unwrap(); - clap_app.gen_completions(BIN_NAME, Shell::Bash, &out_dir); - clap_app.gen_completions(BIN_NAME, Shell::Zsh, &out_dir); - clap_app.gen_completions(BIN_NAME, Shell::Fish, &out_dir); -} diff --git a/src/cli.rs b/src/cli.rs index 008fb28cf3..b6b0ec3cf4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -51,4 +51,10 @@ pub enum ConfigCli { /// Disables loading of configuration file at default location clean: bool, }, + + #[structopt(name = "generate-completion")] + GenerateCompletion { + shell: String, + }, } + diff --git a/src/main.rs b/src/main.rs index ec249e8a16..377ef4f6e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,20 @@ pub fn main() { let mut stream = UnixStream::connect(ZELLIJ_IPC_PIPE).unwrap(); let api_command = bincode::serialize(&ApiCommand::OpenFile(file_to_open)).unwrap(); stream.write_all(&api_command).unwrap(); + } else if let Some(crate::cli::ConfigCli::GenerateCompletion { shell }) = opts.option { + let shell = match shell.as_ref() { + "bash" => structopt::clap::Shell::Bash, + "fish" => structopt::clap::Shell::Fish, + "zsh" => structopt::clap::Shell::Zsh, + "powerShell" => structopt::clap::Shell::PowerShell, + "elvish" => structopt::clap::Shell::Elvish, + other => { + eprintln!("Unsupported shell: {}", other); + std::process::exit(1); + }, + }; + let mut out = std::io::stdout(); + CliArgs::clap().gen_completions_to("zellij", shell, &mut out); } else { let os_input = get_os_input(); atomic_create_dir(ZELLIJ_TMP_DIR).unwrap(); From 49429688cf08081fd5e1dcd1a1769f0f969c9d3a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 25 Apr 2021 15:53:17 +0200 Subject: [PATCH 2/2] fixup! Add subcommand to generate completions --- src/cli.rs | 5 +---- src/main.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index b6b0ec3cf4..ba6911d9af 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -53,8 +53,5 @@ pub enum ConfigCli { }, #[structopt(name = "generate-completion")] - GenerateCompletion { - shell: String, - }, + GenerateCompletion { shell: String }, } - diff --git a/src/main.rs b/src/main.rs index 377ef4f6e7..8da85c875f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,15 +47,15 @@ pub fn main() { stream.write_all(&api_command).unwrap(); } else if let Some(crate::cli::ConfigCli::GenerateCompletion { shell }) = opts.option { let shell = match shell.as_ref() { - "bash" => structopt::clap::Shell::Bash, - "fish" => structopt::clap::Shell::Fish, - "zsh" => structopt::clap::Shell::Zsh, + "bash" => structopt::clap::Shell::Bash, + "fish" => structopt::clap::Shell::Fish, + "zsh" => structopt::clap::Shell::Zsh, "powerShell" => structopt::clap::Shell::PowerShell, - "elvish" => structopt::clap::Shell::Elvish, - other => { + "elvish" => structopt::clap::Shell::Elvish, + other => { eprintln!("Unsupported shell: {}", other); std::process::exit(1); - }, + } }; let mut out = std::io::stdout(); CliArgs::clap().gen_completions_to("zellij", shell, &mut out);