From 8b01674fed417ae142502f1cc607ae65bd877f42 Mon Sep 17 00:00:00 2001 From: Sean Breckenridge Date: Tue, 15 Feb 2022 18:20:47 -0800 Subject: [PATCH] core/cli: add completion for hpi command --- misc/completion/README.md | 35 +++++++++++++++++++++++++++++++++++ misc/completion/bash/_hpi | 29 +++++++++++++++++++++++++++++ misc/completion/fish/hpi.fish | 22 ++++++++++++++++++++++ misc/completion/generate | 12 ++++++++++++ misc/completion/zsh/_hpi | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 misc/completion/README.md create mode 100644 misc/completion/bash/_hpi create mode 100644 misc/completion/fish/hpi.fish create mode 100755 misc/completion/generate create mode 100644 misc/completion/zsh/_hpi diff --git a/misc/completion/README.md b/misc/completion/README.md new file mode 100644 index 0000000..f2fc09e --- /dev/null +++ b/misc/completion/README.md @@ -0,0 +1,35 @@ +To enable completion for the `hpi` command: + +If you don't want to use the files here, you can do this when you launch your shell like: + +```bash +eval "$(_HPI_COMPLETE=bash_source hpi)" # in ~/.bashrc +eval "$(_HPI_COMPLETE=zsh_source hpi)" # in ~/.zshrc +eval "$(_HPI_COMPLETE=fish_source hpi)" # in ~/.config/fish/config.fish +``` + +That is slightly slower since its generating the completion code on the fly -- see [click docs](https://click.palletsprojects.com/en/8.0.x/shell-completion/?highlight=completion#enabling-completion) for more info + +To use the completions here: + +### bash + +Put `source /path/to/bash/_hpi` in your `~/.bashrc` + +### zsh + +You can either source the file: + +`source /path/to/zsh/_hpi` + +..or add the directory to your `fpath` to load it lazily: + +`fpath=("/path/to/zsh/" "${fpath[@]}")` (Note: the directory, not the script `_hpi`) + +If your zsh configuration doesn't automatically run `compinit`, after modifying your `fpath` you should: + +`autoload -Uz compinit && compinit` + +### fish + +`cp ./fish/hpi.fish ~/.config/fish/completions/`, an then restart your shell diff --git a/misc/completion/bash/_hpi b/misc/completion/bash/_hpi new file mode 100644 index 0000000..1080d7e --- /dev/null +++ b/misc/completion/bash/_hpi @@ -0,0 +1,29 @@ +_hpi_completion() { + local IFS=$'\n' + local response + + response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _HPI_COMPLETE=bash_complete $1) + + for completion in $response; do + IFS=',' read type value <<< "$completion" + + if [[ $type == 'dir' ]]; then + COMREPLY=() + compopt -o dirnames + elif [[ $type == 'file' ]]; then + COMREPLY=() + compopt -o default + elif [[ $type == 'plain' ]]; then + COMPREPLY+=($value) + fi + done + + return 0 +} + +_hpi_completion_setup() { + complete -o nosort -F _hpi_completion hpi +} + +_hpi_completion_setup; + diff --git a/misc/completion/fish/hpi.fish b/misc/completion/fish/hpi.fish new file mode 100644 index 0000000..e8a8e56 --- /dev/null +++ b/misc/completion/fish/hpi.fish @@ -0,0 +1,22 @@ +function _hpi_completion; + set -l response; + + for value in (env _HPI_COMPLETE=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) hpi); + set response $response $value; + end; + + for completion in $response; + set -l metadata (string split "," $completion); + + if test $metadata[1] = "dir"; + __fish_complete_directories $metadata[2]; + else if test $metadata[1] = "file"; + __fish_complete_path $metadata[2]; + else if test $metadata[1] = "plain"; + echo $metadata[2]; + end; + end; +end; + +complete --no-files --command hpi --arguments "(_hpi_completion)"; + diff --git a/misc/completion/generate b/misc/completion/generate new file mode 100755 index 0000000..3f316a2 --- /dev/null +++ b/misc/completion/generate @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# assumes HPI is already installed +# generates the completion files + +cd "$(realpath "$(dirname "${BASH_SOURCE[0]}")")" + +mkdir -p ./bash ./zsh ./fish + +_HPI_COMPLETE=fish_source hpi >./fish/hpi.fish +# underscores to allow these directories to be lazily loaded +_HPI_COMPLETE=zsh_source hpi >./zsh/_hpi +_HPI_COMPLETE=bash_source hpi >./bash/_hpi diff --git a/misc/completion/zsh/_hpi b/misc/completion/zsh/_hpi new file mode 100644 index 0000000..95190b0 --- /dev/null +++ b/misc/completion/zsh/_hpi @@ -0,0 +1,35 @@ +#compdef hpi + +_hpi_completion() { + local -a completions + local -a completions_with_descriptions + local -a response + (( ! $+commands[hpi] )) && return 1 + + response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _HPI_COMPLETE=zsh_complete hpi)}") + + for type key descr in ${response}; do + if [[ "$type" == "plain" ]]; then + if [[ "$descr" == "_" ]]; then + completions+=("$key") + else + completions_with_descriptions+=("$key":"$descr") + fi + elif [[ "$type" == "dir" ]]; then + _path_files -/ + elif [[ "$type" == "file" ]]; then + _path_files -f + fi + done + + if [ -n "$completions_with_descriptions" ]; then + _describe -V unsorted completions_with_descriptions -U + fi + + if [ -n "$completions" ]; then + compadd -U -V unsorted -a completions + fi +} + +compdef _hpi_completion hpi; +