Files
xo-cli/src/cli/autocomplete/scripts/zsh.zsh

230 lines
7.6 KiB
Bash

# ------------------------------------------------------------------------------
# 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 <category> <template> [field]
# Relative positions from subcommand:
# 1 => category, 2 => template, 3 => field (inspect only)
local pos=$((CURRENT - subcmd_idx))
if [[ $pos -eq 1 ]]; then
compadd -- action transaction output lockingscript variable
elif [[ $pos -eq 2 ]]; then
local templates
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
if [[ ${#templates[@]} -gt 0 ]]; then
compadd -- "${templates[@]}"
fi
elif [[ $pos -eq 3 && "${subcmd}" == "inspect" ]]; then
# Field suggestions depend on selected category and template.
local category="${words[subcmd_idx + 1]}"
local template_arg="${words[subcmd_idx + 2]}"
local fields
fields=("${(@f)$(__xo_complete fields "${category}" "${template_arg}" "${words[CURRENT]}")}")
if [[ ${#fields[@]} -gt 0 ]]; then
compadd -- "${fields[@]}"
fi
fi
elif [[ "${subcmd}" == "set-default" ]]; then
# template set-default <template> <output> <role>
# First positional argument is template name.
local pos=$((CURRENT - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local templates
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
if [[ ${#templates[@]} -gt 0 ]]; then
compadd -- "${templates[@]}"
fi
fi
fi
;;
invitation)
if [[ -z "${subcmd}" ]]; then
compadd -- {{INVITATION_SUBS}}
else
case "${subcmd}" in
create)
# invitation create <template> <action>
# Action list is template-specific.
local pos=$((CURRENT - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local templates
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
if [[ ${#templates[@]} -gt 0 ]]; then
compadd -- "${templates[@]}"
fi
elif [[ $pos -eq 2 ]]; then
local template_arg="${words[subcmd_idx + 1]}"
local actions
actions=("${(@f)$(__xo_complete actions "${template_arg}" "${words[CURRENT]}")}")
if [[ ${#actions[@]} -gt 0 ]]; then
compadd -- "${actions[@]}"
fi
fi
;;
append|sign|broadcast|requirements|export|inspect)
# These subcommands take invitation ID as first argument.
local pos=$((CURRENT - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local invitations
invitations=("${(@f)$(__xo_complete invitations "${words[CURRENT]}")}")
if [[ ${#invitations[@]} -gt 0 ]]; then
compadd -- "${invitations[@]}"
fi
fi
;;
import)
# invitation import <path>: delegate to zsh file completion.
_files
;;
esac
fi
;;
resource)
if [[ -z "${subcmd}" ]]; then
compadd -- {{RESOURCE_SUBS}}
elif [[ "${subcmd}" == "unreserve" ]]; then
# resource unreserve <txhash:vout>
local pos=$((CURRENT - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local resources
resources=("${(@f)$(__xo_complete resources "${words[CURRENT]}")}")
if [[ ${#resources[@]} -gt 0 ]]; then
compadd -- "${resources[@]}"
fi
fi
fi
;;
settings)
if [[ -z "${subcmd}" ]]; then
compadd -- show get set
elif [[ "${subcmd}" == "get" || "${subcmd}" == "set" ]]; then
local pos=$((CURRENT - subcmd_idx))
if [[ $pos -eq 1 ]]; then
compadd -- currency default-mnemonic
fi
fi
;;
receive)
# receive <template> [output]
local pos=$((CURRENT - cmd_idx))
if [[ $pos -eq 1 ]]; then
local templates
templates=("${(@f)$(__xo_complete templates "${words[CURRENT]}")}")
if [[ ${#templates[@]} -gt 0 ]]; then
compadd -- "${templates[@]}"
fi
fi
;;
completions)
# Shell target for completion generation.
if [[ -z "${subcmd}" ]]; then
compadd -- bash zsh fish
fi
;;
esac
}
# Register completion function for the executable name.
compdef _{{FUNC_NAME}}_completions {{BIN_NAME}}