core/cli: add completion for hpi command
This commit is contained in:
parent
f1b18beef7
commit
8b01674fed
5 changed files with 133 additions and 0 deletions
35
misc/completion/README.md
Normal file
35
misc/completion/README.md
Normal file
|
@ -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
|
29
misc/completion/bash/_hpi
Normal file
29
misc/completion/bash/_hpi
Normal file
|
@ -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;
|
||||||
|
|
22
misc/completion/fish/hpi.fish
Normal file
22
misc/completion/fish/hpi.fish
Normal file
|
@ -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)";
|
||||||
|
|
12
misc/completion/generate
Executable file
12
misc/completion/generate
Executable file
|
@ -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
|
35
misc/completion/zsh/_hpi
Normal file
35
misc/completion/zsh/_hpi
Normal file
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue