Skip to content

Commit

Permalink
Improve the shell completion templates
Browse files Browse the repository at this point in the history
* Complete file names when there are no matches.
* Convert to a native Zsh completion script .
* Don't call compinit. That is up to the user. Calling it resets the
completion system.
* Quote array expansion with '@' to avoid dropping empty entries.
* The Zsh completion script can both be eval-ed and installed into
fpath.
  • Loading branch information
segevfiner authored and alecthomas committed Oct 31, 2018
1 parent a395891 commit c2ca6a1
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,27 @@ _{{.App.Name}}_bash_autocomplete() {
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$( ${COMP_WORDS[0]} --completion-bash ${COMP_WORDS[@]:1:$COMP_CWORD} )
opts=$( ${COMP_WORDS[0]} --completion-bash "${COMP_WORDS[@]:1:$COMP_CWORD}" )
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _{{.App.Name}}_bash_autocomplete {{.App.Name}}
complete -F _{{.App.Name}}_bash_autocomplete -o default {{.App.Name}}
`

var ZshCompletionTemplate = `
#compdef {{.App.Name}}
autoload -U compinit && compinit
autoload -U bashcompinit && bashcompinit
_{{.App.Name}}_bash_autocomplete() {
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$( ${COMP_WORDS[0]} --completion-bash ${COMP_WORDS[@]:1:$COMP_CWORD} )
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
_{{.App.Name}}() {
local matches=($(${words[1]} --completion-bash "${(@)words[1,$CURRENT]}"))
compadd -a matches
if [[ $compstate[nmatches] -eq 0 && $words[$CURRENT] != -* ]]; then
_files
fi
}
complete -F _{{.App.Name}}_bash_autocomplete {{.App.Name}}
if [[ "$(basename -- ${(%):-%x})" != "_{{.App.Name}}" ]]; then
compdef _{{.App.Name}} {{.App.Name}}
fi
`

0 comments on commit c2ca6a1

Please sign in to comment.