Skip to content

Commit

Permalink
Merge pull request #1896 from bartekpacia/fix/autocomplete
Browse files Browse the repository at this point in the history
Make it possible to `source` the Zsh autocomplete script
  • Loading branch information
bartekpacia committed May 1, 2024
2 parents 38003d1 + 9835d74 commit 955bc3e
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions autocomplete/zsh_autocomplete
@@ -1,20 +1,30 @@
#compdef $PROG
#compdef program
compdef _program program

_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi
# Replace all occurences of "program" in this file with the actual name of your
# CLI program. We recommend using Find+Replace feature of your editor. Let's say
# your CLI program is called "acme", then replace like so:
# * program => acme
# * _program => _acme

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
_program() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}

compdef _cli_zsh_autocomplete $PROG
# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_program" ]; then
_program
fi

0 comments on commit 955bc3e

Please sign in to comment.