1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

bash-completion refactoring

1. 'compgen -c' lists _all_ command names on the system, which is not
   appropriate for this script, furthermore echo does not read from stdin
   so the output is lost.

2. use _get_comp_words_by_ref and __ltrim_colon_completions to handle task
   names with colons.

   "...modifying COMP_WORDBREAKS in your completion script is not safe
   (as it is a global variable and it has the side effect of affecting
   the behavior of other completion scripts"
   Ref.: https://stackoverflow.com/a/12495727/7044304

3. Add options completion

4. Use task --list-all
This commit is contained in:
Jess Thrysoee
2022-02-13 20:20:01 +01:00
parent 2373743eac
commit 19be1f1bf0

View File

@@ -1,21 +1,22 @@
# /bin/bash
_task_completion() _task_completion()
{ {
local scripts; local cur
local curr_arg; _get_comp_words_by_ref -n : cur
# Remove colon from word breaks case "$cur" in
COMP_WORDBREAKS=${COMP_WORDBREAKS//:} --*)
local options="$(_parse_help task)"
COMPREPLY=( $(compgen -W "$options" -- "$cur") )
;;
*)
local tasks="$(task --list-all | awk 'NR>1 { sub(/:$/,"",$2); print $2 }')"
COMPREPLY=( $(compgen -W "$tasks" -- "$cur") )
;;
esac
scripts=$(task -l | sed '1d' | awk '{ print $2 }' | sed 's/:$//'); __ltrim_colon_completions "$cur"
curr_arg="${COMP_WORDS[COMP_CWORD]:-"."}"
# Do not accept more than 1 argument
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi
COMPREPLY=($(compgen -c | echo "$scripts" | grep -- $curr_arg));
} }
complete -F _task_completion task complete -F _task_completion task