|
| 1 | +_clap_reassemble_words() { |
| 2 | + if [[ "$COMP_WORDBREAKS" != *:* ]]; then |
| 3 | + return |
| 4 | + fi |
| 5 | + local i j=0 line=$COMP_LINE |
| 6 | + words=() |
| 7 | + _CLAP_COMPLETE_INDEX=0 |
| 8 | + for ((i = 0; i < ${#COMP_WORDS[@]}; i++)); do |
| 9 | + if ((i > 0 && j > 0)) && [[ "${COMP_WORDS[i]}" == :* || "${words[j-1]}" == *: ]] && [[ "$line" != [[:blank:]]* ]]; then |
| 10 | + words[j-1]="${words[j-1]}${COMP_WORDS[i]}" |
| 11 | + else |
| 12 | + words[j]="${COMP_WORDS[i]}" |
| 13 | + ((j++)) |
| 14 | + fi |
| 15 | + if ((i == COMP_CWORD)); then |
| 16 | + _CLAP_COMPLETE_INDEX=$((j - 1)) |
| 17 | + fi |
| 18 | + line=${line#*"${COMP_WORDS[i]}"} |
| 19 | + done |
| 20 | +} |
| 21 | + |
| 22 | +_clap_trim_completions() { |
| 23 | + local cur="${words[_CLAP_COMPLETE_INDEX]}" |
| 24 | + if [[ "$cur" != *:* || "$COMP_WORDBREAKS" != *:* ]]; then |
| 25 | + return |
| 26 | + fi |
| 27 | + local colon_word=${cur%"${cur##*:}"} |
| 28 | + local i=${#COMPREPLY[*]} |
| 29 | + while [[ $((--i)) -ge 0 ]]; do |
| 30 | + COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"} |
| 31 | + done |
| 32 | +} |
| 33 | + |
| 34 | +_clap_complete_vp() { |
| 35 | + local IFS=$'\013' |
| 36 | + local _CLAP_COMPLETE_INDEX=${COMP_CWORD} |
| 37 | + local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} |
| 38 | + if compopt +o nospace 2> /dev/null; then |
| 39 | + local _CLAP_COMPLETE_SPACE=false |
| 40 | + else |
| 41 | + local _CLAP_COMPLETE_SPACE=true |
| 42 | + fi |
| 43 | + local words=("${COMP_WORDS[@]}") |
| 44 | + _clap_reassemble_words |
| 45 | + COMPREPLY=( $( \ |
| 46 | + _CLAP_IFS="$IFS" \ |
| 47 | + _CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \ |
| 48 | + _CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \ |
| 49 | + _CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \ |
| 50 | + VP_COMPLETE="bash" \ |
| 51 | + "vp" -- "${words[@]}" \ |
| 52 | + ) ) |
| 53 | + if [[ $? != 0 ]]; then |
| 54 | + unset COMPREPLY |
| 55 | + elif [[ $_CLAP_COMPLETE_SPACE == false ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then |
| 56 | + compopt -o nospace |
| 57 | + fi |
| 58 | + _clap_trim_completions |
| 59 | +} |
| 60 | +if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then |
| 61 | + complete -o nospace -o bashdefault -o nosort -F _clap_complete_vp vp |
| 62 | +else |
| 63 | + complete -o nospace -o bashdefault -F _clap_complete_vp vp |
| 64 | +fi |
0 commit comments