diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-11 16:50:57 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-11 16:50:57 +0800 |
| commit | 58ef8a8f42a68c7a81118ef9120705730ce3f458 (patch) | |
| tree | 80f302b07f011d2e636f5f8d3ec815fe6a4dafab /mingling_core/tmpls/comps/zsh.zsh | |
| parent | 839326946560166da84c04d4770385795d96cff0 (diff) | |
Add shell completion script generation feature
Diffstat (limited to 'mingling_core/tmpls/comps/zsh.zsh')
| -rw-r--r-- | mingling_core/tmpls/comps/zsh.zsh | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/mingling_core/tmpls/comps/zsh.zsh b/mingling_core/tmpls/comps/zsh.zsh new file mode 100644 index 0000000..c1c18bb --- /dev/null +++ b/mingling_core/tmpls/comps/zsh.zsh @@ -0,0 +1,68 @@ +#!/usr/bin/env zsh +_<<<bin_name>>>_completion() { + local -a args + local suggestions + + local buffer="$BUFFER" + local cursor="$CURSOR" + local current_word="${words[$CURRENT]}" + local previous_word="" + local command_name="${words[1]}" + local word_index="$CURRENT" + + if [[ $CURRENT -gt 1 ]]; then + previous_word="${words[$((CURRENT-1))]}" + fi + + args=( + -f "${buffer//-/^}" + -C "$cursor" + -w "${current_word//-/^}" + -p "${previous_word//-/^}" + -c "$command_name" + -i "$word_index" + -a "${(@)words//-/^}" + -F "zsh" + ) + + suggestions=$(<<<bin_name>>> __comp "${args[@]}" 2>/dev/null) + + if [[ $? -eq 0 ]] && [[ -n "$suggestions" ]]; then + local -a completions + completions=(${(f)suggestions}) + + if [[ "${completions[1]}" == "_file_" ]]; then + shift completions + _files + else + local -a parsed_completions + for item in "${completions[@]}"; do + if [[ "$item" =~ '^([^$]+)\$\((.+)\)$' ]]; then + parsed_completions+=("${match[1]}:${match[2]}") + else + parsed_completions+=("$item") + fi + done + + if (( $+functions[_describe] )); then + _describe '<<<bin_name>>> commands' parsed_completions + else + local -a simple_completions + for item in "${parsed_completions[@]}"; do + if [[ "$item" =~ '^([^:]+):(.+)$' ]]; then + simple_completions+=("${match[1]}") + else + simple_completions+=("$item") + fi + done + compadd -a simple_completions + fi + fi + fi +} + +compdef _<<<bin_name>>>_completion <<<bin_name>>> + +if [[ $? -ne 0 ]]; then + compctl -K _<<<bin_name>>>_completion <<<bin_name>>> +fi |
