2022-08-13 21:55:35 +02:00
|
|
|
# vim: set tabstop=2 shiftwidth=2 expandtab:
|
2026-06-29 17:38:24 +02:00
|
|
|
#
|
|
|
|
|
# Thin wrapper around `task __complete`. All suggestion logic lives in the
|
|
|
|
|
# Go engine — do not add completion logic here.
|
2022-02-13 20:20:01 +01:00
|
|
|
|
2025-12-07 12:20:45 +01:00
|
|
|
TASK_CMD="${TASK_EXE:-task}"
|
2022-05-22 16:16:50 +02:00
|
|
|
|
2026-06-29 17:38:24 +02:00
|
|
|
_task() {
|
2022-08-13 21:55:35 +02:00
|
|
|
local cur prev words cword
|
2026-07-03 22:29:42 +02:00
|
|
|
|
|
|
|
|
# Completion directives, mirroring internal/complete/complete.go.
|
|
|
|
|
local -ri NO_SPACE=2 NO_FILE_COMP=4 FILTER_FILE_EXT=8 FILTER_DIRS=16
|
|
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
# Exclude both `=` and `:` from the word breaks so `--output=` and
|
|
|
|
|
# `docs:serve` reach the engine as single tokens.
|
|
|
|
|
_init_completion -n =: || return
|
2019-08-30 09:59:03 +10:00
|
|
|
|
2026-06-29 17:38:24 +02:00
|
|
|
local -a args=()
|
|
|
|
|
if (( cword > 0 )); then
|
|
|
|
|
args=( "${words[@]:1:cword}" )
|
|
|
|
|
fi
|
|
|
|
|
if (( ${#args[@]} == 0 )); then
|
|
|
|
|
args=( "" )
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local output
|
|
|
|
|
output=$("$TASK_CMD" __complete "${args[@]}" 2>/dev/null)
|
|
|
|
|
if [[ -z "$output" ]]; then
|
|
|
|
|
_filedir
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local -a lines=()
|
|
|
|
|
local line
|
|
|
|
|
while IFS= read -r line; do
|
|
|
|
|
lines+=( "$line" )
|
|
|
|
|
done <<< "$output"
|
|
|
|
|
|
|
|
|
|
local last_idx=$(( ${#lines[@]} - 1 ))
|
|
|
|
|
local directive="${lines[$last_idx]#:}"
|
|
|
|
|
unset 'lines[$last_idx]'
|
|
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
if (( directive & FILTER_FILE_EXT )); then
|
2026-06-29 17:38:24 +02:00
|
|
|
local exts=""
|
2026-07-03 22:29:42 +02:00
|
|
|
# ${arr[@]+…} guards against "unbound variable" on an empty array under
|
|
|
|
|
# `set -u` in bash 3.2 (macOS).
|
|
|
|
|
for line in ${lines[@]+"${lines[@]}"}; do
|
2026-06-29 17:38:24 +02:00
|
|
|
exts+="${exts:+|}$line"
|
|
|
|
|
done
|
|
|
|
|
_filedir "@($exts)"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
if (( directive & FILTER_DIRS )); then
|
2026-06-29 17:38:24 +02:00
|
|
|
_filedir -d
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
# Prefix-filter by hand instead of `compgen -W`: the latter joins/splits the
|
|
|
|
|
# word list on IFS, which mangles any suggestion value containing a space.
|
|
|
|
|
local value
|
|
|
|
|
COMPREPLY=()
|
2026-07-03 22:29:42 +02:00
|
|
|
for line in ${lines[@]+"${lines[@]}"}; do
|
2026-07-03 16:03:33 +02:00
|
|
|
value="${line%%$'\t'*}"
|
|
|
|
|
if [[ -z "$cur" || "$value" == "$cur"* ]]; then
|
|
|
|
|
COMPREPLY+=( "$value" )
|
|
|
|
|
fi
|
2022-09-08 19:03:29 +02:00
|
|
|
done
|
|
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
if (( directive & NO_SPACE )); then
|
2026-06-29 17:38:24 +02:00
|
|
|
compopt -o nospace 2>/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
2022-08-13 21:55:35 +02:00
|
|
|
__ltrim_colon_completions "$cur"
|
2026-06-29 17:38:24 +02:00
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
if (( ${#COMPREPLY[@]} == 0 )) && ! (( directive & NO_FILE_COMP )); then
|
2026-06-29 17:38:24 +02:00
|
|
|
_filedir
|
|
|
|
|
fi
|
2019-08-30 09:59:03 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-07 12:20:45 +01:00
|
|
|
complete -F _task "$TASK_CMD"
|