# ------------------------------------------------------------------------------ # Zsh completion template for {{BIN_NAME}} # ------------------------------------------------------------------------------ # Installation: # eval "$({{BIN_NAME}} completions zsh)" # # This file is generated from a template. Placeholders (for example # `{{MNEMONIC_SUBS}}`) are replaced with concrete command values. # ------------------------------------------------------------------------------ # Prefer a helper on PATH; otherwise fall back to helper next to the CLI binary. # This keeps dynamic completion functional in both installed and portable layouts. __xo_complete_bin="" if (( $+commands[xo-complete] )); then __xo_complete_bin="xo-complete" elif (( $+commands[{{BIN_NAME}}] )); then __xo_complete_bin="${commands[{{BIN_NAME}}]:h}/xo-complete" fi # @description # Calls the dynamic helper while silencing helper stderr to avoid noisy # completion menus if helper lookup fails. # @param "$@" Arguments forwarded to xo-complete. __xo_complete() { [[ -n "${__xo_complete_bin}" ]] && "${__xo_complete_bin}" "$@" 2>/dev/null } # @description # Lists mnemonic aliases directly from the config directory without starting # the dynamic Node helper. __xo_complete_mnemonics() { local config_dir="${XO_CONFIG_DIR:-${HOME}/.config/xo-cli}" local file mnemonic for file in "${config_dir}"/mnemonics/mnemonic-*(N); do [[ -f "${file}" ]] || continue mnemonic="${file:t}" [[ "${mnemonic}" == "$1"* ]] && print -r -- "${mnemonic}" done } # @description # Main zsh completion dispatcher registered via `compdef`. # It resolves command context from `$words`/`$CURRENT` and serves: # - static command words via `compadd` # - dynamic values from `xo-complete` # - filesystem completions where file paths are expected _{{FUNC_NAME}}_completions() { local -a commands commands=({{COMMANDS}}) # If previous token is `-m/--mnemonic-file`, complete mnemonic sources. if [[ "${words[CURRENT-1]}" == "-m" || "${words[CURRENT-1]}" == "--mnemonic-file" ]]; then local mnemonics mnemonics=("${(@f)$(__xo_complete_mnemonics "${words[CURRENT]}")}") if [[ ${#mnemonics[@]} -gt 0 ]]; then compadd -- "${mnemonics[@]}" return fi fi # Option context: if current token starts with `-`, complete known options. if [[ "${words[${CURRENT}]}" == -* ]]; then compadd -- {{OPTIONS}} return fi # Find first and second non-option tokens before the cursor. # `cmd_idx` and `subcmd_idx` are used for positional argument calculations. local cmd="" subcmd="" cmd_idx=0 subcmd_idx=0 for ((i=2; i < CURRENT; i++)); do if [[ "${words[i]}" != -* ]]; then if [[ -z "${cmd}" ]]; then cmd="${words[i]}" cmd_idx=$i else subcmd="${words[i]}" subcmd_idx=$i break fi fi done # No command token yet: offer top-level commands. if [[ -z "${cmd}" ]]; then compadd -- ${commands[@]} return fi # Command-specific completion behavior. case "${cmd}" in mnemonic) if [[ -z "${subcmd}" ]]; then compadd -- {{MNEMONIC_SUBS}} fi ;; template) if [[ -z "${subcmd}" ]]; then compadd -- {{TEMPLATE_SUBS}} elif [[ "${subcmd}" == "list" || "${subcmd}" == "inspect" ]]; then # template list/inspect