Simplify ollama completion: consolidate cases, add timeout, trim comments

Collapse five identical _ollama_models case arms into one, add timeout
to prevent shell hang when remote ollama host is unreachable, replace
launch integrations function with a plain variable, and remove
redundant comments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2026-03-29 19:56:51 +02:00
commit de1a4ec6fc

View file

@ -7,18 +7,14 @@
command -v ollama &>/dev/null || return command -v ollama &>/dev/null || return
_ollama_models() { _ollama_models() {
# List locally available model names (NAME column, strip tag if ":latest") timeout 2 ollama list 2>/dev/null | awk 'NR>1 {print $1}'
ollama list 2>/dev/null | awk 'NR>1 {print $1}'
} }
_ollama_running_models() { _ollama_running_models() {
# List currently running model names timeout 2 ollama ps 2>/dev/null | awk 'NR>1 {print $1}'
ollama ps 2>/dev/null | awk 'NR>1 {print $1}'
} }
_ollama_launch_integrations() { _OLLAMA_LAUNCH_INTEGRATIONS="claude cline codex droid opencode openclaw clawdbot moltbot pi"
printf '%s\n' claude cline codex droid opencode openclaw clawdbot moltbot pi
}
_ollama_completions() { _ollama_completions() {
local cur prev local cur prev
@ -27,16 +23,10 @@ _ollama_completions() {
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
local commands="serve create show run stop pull push signin signout list ls ps cp rm launch help" local commands="serve create show run stop pull push signin signout list ls ps cp rm launch help"
local global_opts="--help --nowordwrap --verbose --version" local global_opts="--help --nowordwrap --verbose --version"
# Subcommand-specific flags
case "${prev}" in case "${prev}" in
run) run|show|rm|cp|push|--model)
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0
;;
show)
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") ) COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0 return 0
;; ;;
@ -44,31 +34,17 @@ _ollama_completions() {
COMPREPLY=( $(compgen -W "$(_ollama_running_models)" -- "${cur}") ) COMPREPLY=( $(compgen -W "$(_ollama_running_models)" -- "${cur}") )
return 0 return 0
;; ;;
rm)
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0
;;
cp)
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0
;;
push)
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0
;;
pull) pull)
# No local completions useful for pull (user types a registry name)
return 0 return 0
;; ;;
launch) launch)
COMPREPLY=( $(compgen -W "$(_ollama_launch_integrations)" -- "${cur}") ) COMPREPLY=( $(compgen -W "${_OLLAMA_LAUNCH_INTEGRATIONS}" -- "${cur}") )
return 0 return 0
;; ;;
help) help)
COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
return 0 return 0
;; ;;
# Flag-specific value completions
--format) --format)
COMPREPLY=( $(compgen -W "json" -- "${cur}") ) COMPREPLY=( $(compgen -W "json" -- "${cur}") )
return 0 return 0
@ -81,13 +57,8 @@ _ollama_completions() {
COMPREPLY=( $(compgen -f -- "${cur}") ) COMPREPLY=( $(compgen -f -- "${cur}") )
return 0 return 0
;; ;;
--model)
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0
;;
esac esac
# Determine which subcommand we're in (if any)
local subcmd="" local subcmd=""
local i local i
for (( i=1; i < COMP_CWORD; i++ )); do for (( i=1; i < COMP_CWORD; i++ )); do
@ -99,7 +70,6 @@ _ollama_completions() {
esac esac
done done
# Per-subcommand flag completion
if [[ "${cur}" == -* ]]; then if [[ "${cur}" == -* ]]; then
local sub_opts="" local sub_opts=""
case "${subcmd}" in case "${subcmd}" in
@ -126,19 +96,18 @@ _ollama_completions() {
return 0 return 0
fi fi
# Handle model-name completion for rm (supports multiple models) # rm supports multiple model arguments
if [[ "${subcmd}" == "rm" ]]; then if [[ "${subcmd}" == "rm" ]]; then
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") ) COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0 return 0
fi fi
# Handle second argument for cp (destination) # cp destination (second argument)
if [[ "${subcmd}" == "cp" && ${COMP_CWORD} -ge 3 ]]; then if [[ "${subcmd}" == "cp" && ${COMP_CWORD} -ge 3 ]]; then
COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") ) COMPREPLY=( $(compgen -W "$(_ollama_models)" -- "${cur}") )
return 0 return 0
fi fi
# Top-level: complete subcommands and global options
if [[ ${COMP_CWORD} -eq 1 ]]; then if [[ ${COMP_CWORD} -eq 1 ]]; then
COMPREPLY=( $(compgen -W "${commands} ${global_opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${commands} ${global_opts}" -- "${cur}") )
fi fi