Skip to content

Commit

Permalink
Support different bash completion options
Browse files Browse the repository at this point in the history
#1508

Based on the documentation found here
https://www.gnu.org/software/bash/manual/html_node/Commands-For-Completion.html
we do special handling for the cases:
- menu-complete/menu-complete-backward
- insert-completions

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
  • Loading branch information
marckhouzam committed Oct 19, 2021
1 parent c1973d3 commit 1766812
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion bash_completionsV2.go
Expand Up @@ -138,13 +138,55 @@ __%[1]s_process_completion_results() {
_filedir -d
fi
else
__%[1]s_handle_standard_completion_case
__%[1]s_handle_completion_types
fi
__%[1]s_handle_special_char "$cur" :
__%[1]s_handle_special_char "$cur" =
}
__%[1]s_handle_completion_types() {
__helm_debug "__%[1]s_handle_completion_types: COMP_TYPE is $COMP_TYPE"
local tab comp
tab=$(printf '\t')
case $COMP_TYPE in
37) # Type: menu-complete and menu-complete-backward
# If the user requested listing one completion at a time
# we must remove the descriptions.
# https://github.com/spf13/cobra/issues/1508
while IFS='' read -r comp; do
# Strip any description
comp=${comp%%%%$tab*}
# Only consider the completions that match
comp=$(compgen -W "$comp" -- "$cur")
COMPREPLY+=("$comp")
done < <(printf "%%s\n" "${out[@]}")
;;
42) # Type: insert-completions
# If the user requested inserting all completions at once
# there is no point in returning more than a single one.
# We return the first one that matches
while IFS='' read -r comp; do
# Strip any description
comp=${comp%%%%$tab*}
# Only consider the completions that match
comp=$(compgen -W "$comp" -- "$cur")
if [ -n "$comp" ]; then
COMPREPLY+=("$comp")
break
fi
done < <(printf "%%s\n" "${out[@]}")
;;
*) # Type: complete (normal completion)
__%[1]s_handle_standard_completion_case
;;
esac
}
__%[1]s_handle_standard_completion_case() {
local tab comp
tab=$(printf '\t')
Expand Down

0 comments on commit 1766812

Please sign in to comment.