#compdef ollama

(( $+functions[_ollama_commands] )) ||
_ollama_commands() {
  local -a commands
  commands=(
    'serve:Start ollama'
    'create:Create a model from a Modelfile'
    'show:Show information for a model'
    'run:Run a model'
    'stop:Stop a running model'
    'pull:Pull a model from a registry'
    'push:Push a model to a registry'
    'list:List models'
    'ps:List running models'
    'cp:Copy a model'
    'rm:Remove a model'
    'help:Help about any command'
  )
  _describe 'commands' commands
}

_ollama_quantization() {
  local opts
  qa=( q8_0 q4_K_S q4_K_M )
  _wanted opts expl 'quantization' compadd -S '' ${^qa} && ret=0
}

# https://ollama.com/library
_ollama_models() {
  local -a args
  models=(
    all-minilm
    all-minilm:latest
    all-minilm:l6-v2
    all-minilm:l12-v2
    # codegemma
    # codegemma:latest
    # codegemma:instruct
    # codegemma:7b
    codellama
    codellama:7b
    codellama:latest
    codellama:instruct
    codellama:python
    embeddinggemma
    functiongemma:latest
    gemma3n
    gemma3n:e4b
    gemma3n:e2b
    gemma3
    gemma3:latest
    gemma3:4b
    gemma3:1b
    gemma3:1b-it-q4_K_M
    gemma3:1b-it-q8_0
    gemma3:1b-it-fp16
    hf.co/Menlo/Jan-nano-gguf:Q4_K_M
    jan-nano-4b-q8
    lfm2.5-thinking:latest
    lfm2.5-thinking:1.2b
    lfm2.5-thinking:1.2b-q4_K_M
    llama3.2
    llama3.1
    llama2-uncensored
    llava
    ministral-3:latest
    ministral-3:8b
    ministral-3:8b-instruct-2512-q4_K_M
    ministral-3:8b-instruct-2512-q8_0
    ministral-3:8b-instruct-2512-fp16
    ministral-3:3b
    ministral-3:3b-instruct-2512-q4_K_M
    ministral-3:3b-instruct-2512-q8_0
    ministral-3:3b-instruct-2512-fp16
    ministral-3:14b
    ministral-3:14b-instruct-2512-q4_K_M
    ministral-3:14b-instruct-2512-q8_0
    mistral
    mistral:latest
    mistral:instruct
    mistral:7b
    mistral:7b-instruct
    nomic-embed-text
    nomic-embed-text:latest
    olmo2
    olmo2:7b
    olmo2:13b
    olmo3
    olmo-3:latest
    olmo-3:7b
    olmo-3:7b-instruct
    olmo-3:7b-instruct-q4_K_M
    olmo-3:7b-instruct-q8_0
    olmo-3:7b-think
    olmo-3:7b-think-q4_K_M
    olmo-3:7b-think-q8_0
    phi4:14b
    phi4-mini
    phi4-mini:3.8b
    qwen3
    qwen3:8b
    qwen3:4b
    qwen3:14b
    qwen3:1.7b
    qwen2.5-coder
    qwen2.5-coder:7b
    qwen2.5-coder:7b-instruct-q4_K_M
    # mradermacher/Qwen2.5-Coder-7B-i1-GGUF:Q4_K_M
    # qwen2.5-coder-tools:7b-q8_0
    # qwen2.5-coder:3b
    # qwen2.5-coder:14b
    smollm2
    smollm2:latest
    smollm2:1.7b
    smollm2:1.7b-instruct-q4_K_S
    smollm2:1.7b-instruct-q4_K_M
    smollm2:1.7b-instruct-q6_K
    smollm2:1.7b-instruct-q8_0
    smollm2:360m-instruct-q8_0
    starcoder2:3b
    starcoder2:7b
    starcoder2:15b
    tulu3
    tulu3:latest
    tulu3:8b
    tulu3:8b-q4_K_M
    tulu3:8b-q8_0
  )
  _wanted args expl 'ollama models' compadd -S '' "$models[@]"
}

_ollama_model() {
  local -a args
  om=($(ollama list | awk '{ print $1 }' | tail -n+2))
  _wanted args expl 'ollama model' compadd -S '' "$om[@]"
}

_ollama() {
  # local context state line curcontext="$curcontext"
  local context curcontext="$curcontext" state line
  typeset -A opt_args
  local ret=1

_arguments -C \
  '(-h)-h[help for ollama]' \
  '(--help)--help[help for ollama]' \
  '(-v)--version[Show version information]' \
  '(--version)-v[Show version information]' \
  '1:: :_ollama_commands' \
  '*:: :->command_args' && ret=0

local rules

case "$state" in
  (command_args)
    case "$words[1]" in
      (serve)
        _arguments \
          '-h[help for serve]' \
          '--help[help for serve]' \
        && ret=0
        _values 'serve' \
          'OLLAMA_DEBUG[Show additional debug information (e.g. OLLAMA_DEBUG=1)]' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
          'OLLAMA_KEEP_ALIVE[The duration that models stay loaded in memory (default "5m")]:duration:' \
          'OLLAMA_MAX_LOADED_MODELS[Maximum number of loaded models per GPU]' \
          'OLLAMA_MAX_QUEUE[Maximum number of queued requests]' \
          'OLLAMA_MODELS[The path to the models directory]' \
          'OLLAMA_NUM_PARALLEL[Maximum number of parallel requests]' \
          'OLLAMA_NOPRUNE[Do not prune model blobs on startup]' \
          'OLLAMA_ORIGINS[A comma separated list of allowed origins]' \
          'OLLAMA_TMPDIR[Location for temporary files]' \
          'OLLAMA_FLASH_ATTENTION[Enabled flash attention]' \
          'OLLAMA_LLM_LIBRARY[Set LLM library to bypass autodetection]' \
        && ret=0
        ;;
      (create)
        _arguments \
          '-f[Name of the Modelfile (default "Modelfile")]:string:' \
          '--file[Name of the Modelfile (default "Modelfile")]:string:_files' \
          '-h[help for create]' \
          '--help[help for create]' \
          '-q[Quantize model to this level (e.g. q4_0)]:string:_ollama_quantization' \
          '--quantize[Quantize model to this level (e.g. q4_0)]:string:_ollama_quantization' \
          '*:file:_files' \
        && ret=0
        _values 'create' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (show)
        _arguments \
          '-h[help for show]' \
          '--help[help for show]' \
          '--license[Show license of a model]:model:_ollama_model' \
          '--modelfile[Show Modelfile of a model]:model:_ollama_model' \
          '--parameters[Show parameters of a model]:model:_ollama_model' \
          '--system[Show system message of a model]:model:_ollama_model' \
          '--template[Show template of a model]:model:_ollama_model' \
          '*:file:_files' \
        && ret=0
        _values 'show' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (run)
        _arguments \
          '--format[Response format (e.g. json)]:string:' \
          '-h[help for run]' \
          '--help[help for run]' \
          '--insecure[Use an insecure registry]' \
          '--keepalive[Duration to keep a model loaded (e.g. 5m)]:int:' \
          '--nowordwrap[Do not wrap words to the next line automatically]' \
          '--verbose[Show timings for response]' \
        && ret=0
        _values 'run' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
          'OLLAMA_NOHISTORY[Do not preserve readline history]:bool:' \
        && ret=0
        ;;
      (stop)
        _arguments \
          '-h[help for stop]' \
          '--help[help for stop]' \
          '1:model:_ollama_model' \
          && ret=0
        _values 'stop' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (pull)
        _arguments \
          '-h[help for pull]' \
          '--help[help for pull]' \
          '--insecure[Use an insecure registry]' \
          '1:model:_ollama_models' \
        && ret=0
        _values 'pull' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (push)
        _arguments \
          '-h[help for push]' \
          '--help[help for push]' \
          '--insecure[Use an insecure registry]' \
        && ret=0
        _values 'push' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (list)
        _arguments \
          '-h[help for list]' \
          '--help[help for list]' \
        && ret=0
        _values 'list' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (ps)
        _arguments \
          '-h[help for ps]' \
          '--help[help for ps]' \
          '1:model:_ollama_model' \
        && ret=0
        _values 'ps' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (cp)
        _arguments \
          '-h[help for cp]' \
          '--help[help for cp]' \
          '1:model:_ollama_model' \
          && ret=0
        _values 'cp' \
          'OLLAMA_HOST[IP Address for the ollama server (default 127.0.0.1:11434)]:int:' \
        && ret=0
        ;;
      (rm)
        _arguments \
          '-h[help for rm]' \
          '--help[help for rm]' \
          '1:model:_ollama_model' \
        && ret=0
        ;;
      (help)
        _arguments \
          '1: :_ollama_commands' \
        && ret=0
        ;;
    esac
    ;;
esac

return ret
}

_ollama "$@"
