From: Frantisek Tobias Date: Fri, 15 Nov 2024 08:59:12 +0000 (+0100) Subject: utils: bash-client: only complete up to the cursor location regardless of what follows X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=28c98ecb70aab40dd5bfff604607514112e73d66;p=thirdparty%2Fknot-resolver.git utils: bash-client: only complete up to the cursor location regardless of what follows --- diff --git a/utils/shell-completion/client.bash b/utils/shell-completion/client.bash index 2f3b4b453..de61c4295 100644 --- a/utils/shell-completion/client.bash +++ b/utils/shell-completion/client.bash @@ -1,62 +1,39 @@ #/usr/bin/env bash -_kresctl_skip_next=0 - -_kresctl_filter_switches() +_kresctl_filter_double_dash() { - # skip kresctl, it is not a valid argument - local words=("${COMP_WORDS[@]:1}") + local words=("$@") local new_words=() local count=0 for WORD in "${words[@]}" do - if [[ $_kresctl_skip_next -eq 0 ]] + if [[ "$WORD" != "--" ]] then - if [[ ! $WORD =~ ^-{1,2} ]] - then - new_words[count]="$WORD" - ((count++)) - else - new_words[count]=" " - ((count++)) - _kresctl_skip_next=1 - fi - else - _kresctl_skip_next=0 + new_words[count]="$WORD" + ((count++)) fi - done printf "%s\n" "${new_words[@]}" - return $count } - _kresctl_completion() { COMPREPLY=() local cur opts cmp_words - for (( i=2; i<${#COMP_WORDS[@]}; i++ )); do - if [[ "${COMP_WORDS[i-2]}" == "config" ]] && - ([[ "${COMP_WORDS[i]}" == "-p" ]] || [[ "${COMP_WORDS[i]}" == "--path" ]]); then - COMP_WORDS[i]="conf_get_path_subst" - break - fi - done - cur="${COMP_WORDS[COMP_CWORD]}" - cmp_words=($(_kresctl_filter_switches)) + local line="${COMP_LINE:0:$COMP_POINT}" + local words_up_to_cursor=($line) + + cmp_words=($(_kresctl_filter_double_dash "${words_up_to_cursor[@]}")) - # check if there is a word is empty - # that means there is a space after last non-empty word - if [[ -z "$cur" ]] + if [[ -z "$cur" && "$COMP_POINT" -gt 0 && "${line: -1}" == " " ]] then - # no word to complete, return all posible options - opts=$(kresctl completion --bash --space "${cmp_words[@]}") + opts=$(kresctl completion --bash --space --args "${cmp_words[@]}") else - opts=$(kresctl completion --bash --space "${cmp_words[@]}") + opts=$(kresctl completion --bash --args "${cmp_words[@]}") fi # if there is no completion from kresctl @@ -65,7 +42,7 @@ _kresctl_completion() then COMPREPLY=($(compgen -d -f -- "${cur}")) else - COMPREPLY=($(compgen -W "${opts}" -- ${cur})) + COMPREPLY=($(compgen -W "${opts}" -- "${cur}")) fi return 0