2018-04-27 17:50:56 +09:00
|
|
|
#compdef task
|
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-25 00:58:22 -05:00
|
|
|
|
2026-06-29 17:38:24 +02:00
|
|
|
TASK_CMD="${TASK_EXE:-task}"
|
2018-04-27 17:50:56 +09:00
|
|
|
|
2026-06-29 17:38:24 +02:00
|
|
|
_task() {
|
2026-07-03 16:03:33 +02:00
|
|
|
local -a args lines completions opts ctl
|
|
|
|
|
local output directive line
|
2026-06-29 17:38:24 +02:00
|
|
|
|
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 KEEP_ORDER=32
|
|
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
# Map the zsh completion zstyles to engine flags. `-T` is true when the
|
|
|
|
|
# style is unset (its default) or explicitly true, so a flag is only passed
|
|
|
|
|
# when the user turned the style off.
|
|
|
|
|
zstyle -T ":completion:${curcontext}:" show-aliases || ctl+=(--no-aliases)
|
|
|
|
|
zstyle -T ":completion:${curcontext}:" verbose || ctl+=(--no-descriptions)
|
2026-06-29 17:38:24 +02:00
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
# (@) preserves a trailing empty string, which the engine relies on to
|
|
|
|
|
# know the cursor is on a fresh word.
|
|
|
|
|
args=("${(@)words[2,CURRENT]}")
|
|
|
|
|
(( ${#args} == 0 )) && args=("")
|
2025-09-16 19:34:57 +02:00
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
output=$("$TASK_CMD" __complete "${ctl[@]}" "${args[@]}" 2>/dev/null)
|
|
|
|
|
if [[ -z "$output" ]]; then
|
|
|
|
|
_files
|
|
|
|
|
return
|
|
|
|
|
fi
|
2026-06-29 17:38:24 +02:00
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
lines=("${(f)output}")
|
|
|
|
|
directive="${lines[-1]#:}"
|
|
|
|
|
lines=("${(@)lines[1,-2]}")
|
2025-09-16 19:34:57 +02:00
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
if (( directive & FILTER_FILE_EXT )); then
|
2026-07-03 16:03:33 +02:00
|
|
|
local -a globs
|
|
|
|
|
for line in "${lines[@]}"; do
|
|
|
|
|
globs+=("*.${line}")
|
|
|
|
|
done
|
|
|
|
|
_files -g "(${(j:|:)globs})"
|
|
|
|
|
return
|
|
|
|
|
fi
|
2022-02-25 00:58:22 -05:00
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
if (( directive & FILTER_DIRS )); then
|
2026-07-03 16:03:33 +02:00
|
|
|
_path_files -/
|
|
|
|
|
return
|
|
|
|
|
fi
|
2025-12-18 08:35:56 +01:00
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
# `:` inside the value must be escaped: _describe splits on the first
|
|
|
|
|
# unescaped colon (e.g. "docs:serve" would otherwise become value "docs").
|
|
|
|
|
local value desc
|
|
|
|
|
for line in "${lines[@]}"; do
|
|
|
|
|
if [[ "$line" == *$'\t'* ]]; then
|
|
|
|
|
value="${line%%$'\t'*}"
|
|
|
|
|
desc="${line#*$'\t'}"
|
|
|
|
|
completions+=("${value//:/\\:}:$desc")
|
|
|
|
|
else
|
|
|
|
|
completions+=("${line//:/\\:}")
|
|
|
|
|
fi
|
|
|
|
|
done
|
2025-11-29 12:16:58 +01:00
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
(( directive & NO_SPACE )) && opts+=(-S '')
|
|
|
|
|
(( directive & KEEP_ORDER )) && opts+=(-V)
|
2025-11-29 12:16:58 +01:00
|
|
|
|
2026-07-03 16:03:33 +02:00
|
|
|
if (( ${#completions} > 0 )); then
|
|
|
|
|
_describe -t tasks 'task' completions "${opts[@]}"
|
|
|
|
|
fi
|
|
|
|
|
|
2026-07-03 22:29:42 +02:00
|
|
|
(( directive & NO_FILE_COMP )) && return
|
2026-07-03 16:03:33 +02:00
|
|
|
_files
|
2024-09-18 16:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-29 17:38:24 +02:00
|
|
|
compdef _task "$TASK_CMD"
|