Skip to content

Commit

Permalink
Fix completions broken since click>=8
Browse files Browse the repository at this point in the history
  • Loading branch information
voidus committed May 24, 2022
1 parent b820093 commit 8ee6ef0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions scripts/create-completion-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ case $1 in
exit 0
;;
bash)
src_command="source"
src_command="bash_source"
target_file="watson.completion"
;;
zsh)
src_command="source_zsh"
src_command="zsh_source"
target_file="watson.zsh-completion"
;;
*)
Expand Down
38 changes: 23 additions & 15 deletions watson.completion
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
_watson_completion() {
local IFS=$'
'
COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
_WATSON_COMPLETE=complete $1 ) )
local IFS=$'\n'
local response

response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _WATSON_COMPLETE=bash_complete $1)

for completion in $response; do
IFS=',' read type value <<< "$completion"

if [[ $type == 'dir' ]]; then
COMPREPLY=()
compopt -o dirnames
elif [[ $type == 'file' ]]; then
COMPREPLY=()
compopt -o default
elif [[ $type == 'plain' ]]; then
COMPREPLY+=($value)
fi
done

return 0
}

_watson_completionetup() {
local COMPLETION_OPTIONS=""
local BASH_VERSION_ARR=(${BASH_VERSION//./ })
# Only BASH version 4.4 and later have the nosort option.
if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
COMPLETION_OPTIONS="-o nosort"
fi

complete $COMPLETION_OPTIONS -F _watson_completion watson
_watson_completion_setup() {
complete -o nosort -F _watson_completion watson
}

_watson_completionetup;
_watson_completion_setup;

25 changes: 14 additions & 11 deletions watson.zsh-completion
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ _watson_completion() {
local -a response
(( ! $+commands[watson] )) && return 1

response=("${(@f)$( env COMP_WORDS="${words[*]}" \
COMP_CWORD=$((CURRENT-1)) \
_WATSON_COMPLETE="complete_zsh" \
watson )}")
response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _WATSON_COMPLETE=zsh_complete watson)}")

for key descr in ${(kv)response}; do
if [[ "$descr" == "_" ]]; then
completions+=("$key")
else
completions_with_descriptions+=("$key":"$descr")
fi
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
Expand All @@ -26,7 +29,7 @@ _watson_completion() {
if [ -n "$completions" ]; then
compadd -U -V unsorted -a completions
fi
compstate[insert]="automenu"
}

compdef _watson_completion watson;

0 comments on commit 8ee6ef0

Please sign in to comment.