diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-25 13:22:16 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-25 13:22:16 +0800 |
| commit | fece037f453006c83c45825e3649495180eb30c9 (patch) | |
| tree | 5ce6f59c1daf525eeb73ce54393b9cd572774a53 /scripts/deploy/completions/bash.sh | |
| parent | 8f69598f6abfb2b3db076a15fed85c9c186ccbde (diff) | |
Split Unix completion script entry into separate bash/zsh/fish versions
Diffstat (limited to 'scripts/deploy/completions/bash.sh')
| -rw-r--r-- | scripts/deploy/completions/bash.sh | 90 |
1 files changed, 36 insertions, 54 deletions
diff --git a/scripts/deploy/completions/bash.sh b/scripts/deploy/completions/bash.sh index e1b2a31..a9a3263 100644 --- a/scripts/deploy/completions/bash.sh +++ b/scripts/deploy/completions/bash.sh @@ -1,65 +1,47 @@ #!/usr/bin/env bash _jvn_bash_completion() { - local cur prev words cword - - local line="${COMP_LINE}" - local point="${COMP_POINT}" - - if [ "${point}" -gt "${#line}" ]; then - point="${#line}" - fi - - words=($line) - cword=0 - - local i=0 - local pos=0 - for word in "${words[@]}"; do - local word_start=$pos - local word_end=$((pos + ${#word})) - - if [ "${point}" -ge "${word_start}" ] && [ "${point}" -le "${word_end}" ]; then - cword=$i - cur="${word}" - break - fi - - pos=$((pos + ${#word} + 1)) - i=$((i + 1)) + 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") + + for word in "${COMP_WORDS[@]}"; do + args+=(-a="${word//-/^}") done - if [ "${point}" -gt "${pos}" ]; then - cword=${#words[@]} - cur="" - fi - - if [ "${cword}" -gt 0 ]; then - prev="${words[$((cword-1))]}" - else - prev="" - fi - - local args=( - -f "${COMP_LINE//-/^}" - -C "$COMP_POINT" - -w "${cur//-/^}" - -p "${prev//-/^}" - -c "${words[0]}" - -i "$cword" - -a "${words[@]//-/^}" - ) - local suggestions if suggestions=$(jvn_comp "${args[@]}" 2>/dev/null); then - if [ "$suggestions" = "_file_" ]; then - compopt -o default - COMPREPLY=() - else - mapfile -t COMPREPLY < <(printf '%s\n' "$suggestions") + 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 - else - COMPREPLY=() fi + + COMPREPLY=() } complete -F _jvn_bash_completion jvn |
