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/bash.sh | |
| parent | 839326946560166da84c04d4770385795d96cff0 (diff) | |
Add shell completion script generation feature
Diffstat (limited to 'mingling_core/tmpls/comps/bash.sh')
| -rw-r--r-- | mingling_core/tmpls/comps/bash.sh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/mingling_core/tmpls/comps/bash.sh b/mingling_core/tmpls/comps/bash.sh new file mode 100644 index 0000000..1af4f6c --- /dev/null +++ b/mingling_core/tmpls/comps/bash.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +_<<<bin_name>>>_bash_completion() { + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="" + [ $COMP_CWORD -gt 0 ] && prev="${COMP_WORDS[COMP_CWORD-1]}" + + local word_index=$((COMP_CWORD + 1)) + + local args=() + args+=(-f="${COMP_LINE//-/^}") + args+=(-C="$COMP_POINT") + args+=(-w="${cur//-/^}") + args+=(-p="${prev//-/^}") + args+=(-c="${COMP_WORDS[0]//-/^}") + args+=(-i="$word_index") + args+=(-F="bash") + + for word in "${COMP_WORDS[@]}"; do + args+=(-a="${word//-/^}") + done + + local suggestions + if suggestions=$(<<<bin_name>>> __comp "${args[@]}" 2>/dev/null); then + if [ $? -eq 0 ]; then + if [ "$suggestions" = "_file_" ]; then + compopt -o default + COMPREPLY=() + return + fi + + if [ -n "$suggestions" ]; then + local -a all_suggestions filtered + mapfile -t all_suggestions < <(printf '%s\n' "$suggestions") + + for suggestion in "${all_suggestions[@]}"; do + [ -z "$cur" ] || [[ "$suggestion" == "$cur"* ]] && filtered+=("$suggestion") + done + + [ ${#filtered[@]} -gt 0 ] && COMPREPLY=("${filtered[@]}") + return + fi + fi + fi + + COMPREPLY=() +} + +complete -F _<<<bin_name>>>_bash_completion <<<bin_name>>> |
