2022-08-13 21:55:35 +02:00
|
|
|
# vim: set tabstop=2 shiftwidth=2 expandtab:
|
2026-08-29 10:36:54 +02:00
|
|
|
#
|
|
|
|
|
# Thin wrapper around `task __complete`: all suggestion logic lives in the Go engine.
|
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-08-29 10:36:54 +02:00
|
|
|
# `=` stays inside the current word (see `_init_completion -n =:`), so an inline
|
|
|
|
|
# `--flag=` prefix must be stripped before _filedir and re-applied after.
|
|
|
|
|
_task_filedir() {
|
|
|
|
|
local fpfx="" savecur="$cur"
|
|
|
|
|
if [[ "$cur" == -*=* ]]; then
|
|
|
|
|
fpfx="${cur%%=*}="
|
|
|
|
|
cur="${cur#*=}"
|
|
|
|
|
fi
|
|
|
|
|
_filedir ${1:+"$1"}
|
|
|
|
|
cur="$savecur"
|
|
|
|
|
if [[ -n "$fpfx" ]]; then
|
|
|
|
|
COMPREPLY=( ${COMPREPLY[@]+"${COMPREPLY[@]/#/$fpfx}"} )
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_task() {
|
2022-08-13 21:55:35 +02:00
|
|
|
local cur prev words cword
|
2026-08-29 10:36:54 +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 KEEP_ORDER=32
|
|
|
|
|
|
|
|
|
|
# `=` and `:` out of the word breaks: `--output=`, `docs:serve` stay one token.
|
|
|
|
|
_init_completion -n =: || return
|
|
|
|
|
|
|
|
|
|
local -a args=( "${words[@]:1:cword}" )
|
|
|
|
|
if (( ${#args[@]} == 0 )); then
|
|
|
|
|
args=( "" )
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local output
|
|
|
|
|
output=$("$TASK_CMD" __complete "${args[@]}" 2>/dev/null)
|
|
|
|
|
if [[ -z "$output" ]]; then
|
|
|
|
|
_task_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]'
|
|
|
|
|
|
|
|
|
|
if (( directive & FILTER_FILE_EXT )); then
|
|
|
|
|
local exts=""
|
|
|
|
|
# ${arr[@]+…} guards an empty array under `set -u` in bash 3.2 (macOS).
|
|
|
|
|
for line in ${lines[@]+"${lines[@]}"}; do
|
|
|
|
|
exts+="${exts:+|}$line"
|
|
|
|
|
done
|
|
|
|
|
_task_filedir "@($exts)"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if (( directive & FILTER_DIRS )); then
|
|
|
|
|
_task_filedir -d
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Not `compgen -W`: it splits the word list on IFS, mangling values with spaces.
|
|
|
|
|
local value
|
|
|
|
|
COMPREPLY=()
|
|
|
|
|
for line in ${lines[@]+"${lines[@]}"}; do
|
|
|
|
|
value="${line%%$'\t'*}"
|
|
|
|
|
if [[ -z "$cur" || "$value" == "$cur"* ]]; then
|
|
|
|
|
COMPREPLY+=( "$value" )
|
2022-09-08 19:03:29 +02:00
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2026-08-29 10:36:54 +02:00
|
|
|
if (( directive & NO_SPACE )); then
|
|
|
|
|
compopt -o nospace 2>/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# nosort needs bash 4.4; the 3.2 shipped by macOS ignores it and stays sorted.
|
|
|
|
|
if (( directive & KEEP_ORDER )); then
|
|
|
|
|
compopt -o nosort 2>/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
2022-08-13 21:55:35 +02:00
|
|
|
__ltrim_colon_completions "$cur"
|
2026-08-29 10:36:54 +02:00
|
|
|
|
|
|
|
|
if (( ${#COMPREPLY[@]} == 0 )) && ! (( directive & NO_FILE_COMP )); then
|
|
|
|
|
_task_filedir
|
|
|
|
|
fi
|
2019-08-30 09:59:03 +10:00
|
|
|
}
|
|
|
|
|
|
2025-12-07 12:20:45 +01:00
|
|
|
complete -F _task "$TASK_CMD"
|