aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/tmpls/comps
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-03 17:22:44 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-03 17:22:44 +0800
commit61c42889b4bd4b91fe8660a3f17d3ad0dedf247b (patch)
tree19cc4124525e9800cef0ea39b6b5eaf81ca72a5d /mingling_core/tmpls/comps
parentc31aefaf46c33dea092b357d6311fac7819a3cea (diff)
feat(comps): improve bash completion script word tracking
Rework bash completion script to fix word-index tracking when the cursor is in the middle of a word, compute the current word from COMP_LINE truncated at COMP_POINT, and use space-separated option arguments. Also trim colon prefixes from completion items when COMP_WORDBREAKS includes colons.
Diffstat (limited to 'mingling_core/tmpls/comps')
-rw-r--r--mingling_core/tmpls/comps/bash.sh43
1 files changed, 31 insertions, 12 deletions
diff --git a/mingling_core/tmpls/comps/bash.sh b/mingling_core/tmpls/comps/bash.sh
index 1af4f6c..edec28d 100644
--- a/mingling_core/tmpls/comps/bash.sh
+++ b/mingling_core/tmpls/comps/bash.sh
@@ -1,22 +1,31 @@
#!/usr/bin/env bash
_<<<bin_name>>>_bash_completion() {
- local cur="${COMP_WORDS[COMP_CWORD]}"
+ local line="${COMP_LINE:0:COMP_POINT}"
+ local cur="${line##* }"
local prev=""
- [ $COMP_CWORD -gt 0 ] && prev="${COMP_WORDS[COMP_CWORD-1]}"
+ local word_index=1
- local word_index=$((COMP_CWORD + 1))
+ local before="${line:0:$(( ${#line} - ${#cur} ))}"
+ local -a before_words
+ if [[ -n "$before" ]]; then
+ read -ra before_words <<< "$before"
+ word_index=$(( ${#before_words[@]} + 1 ))
+ if [[ $word_index -gt 1 ]]; then
+ prev="${before_words[${#before_words[@]}-1]}"
+ fi
+ fi
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")
+ 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//-/^}")
+ args+=(-a "${word//-/^}")
done
local suggestions
@@ -36,7 +45,17 @@ _<<<bin_name>>>_bash_completion() {
[ -z "$cur" ] || [[ "$suggestion" == "$cur"* ]] && filtered+=("$suggestion")
done
- [ ${#filtered[@]} -gt 0 ] && COMPREPLY=("${filtered[@]}")
+ if [ ${#filtered[@]} -gt 0 ]; then
+ COMPREPLY=("${filtered[@]}")
+ if [[ "$cur" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
+ local colon_prefix="${cur%"${cur##*:}"}"
+ local -a ltrimmed=()
+ for suggestion in "${COMPREPLY[@]}"; do
+ ltrimmed+=("${suggestion#"$colon_prefix"}")
+ done
+ COMPREPLY=("${ltrimmed[@]}")
+ fi
+ fi
return
fi
fi