scripts/dav-bash-completion.bash

Sat, 26 Oct 2019 15:38:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Oct 2019 15:38:50 +0200
changeset 676
fb466ba2edc5
parent 673
8e7e56cfc103
child 684
a4b4257c1a5f
permissions
-rw-r--r--

bash completion now handles spaces correctly in both quoted and unquoted strings

dav_completion_build_args() {
    local word
    for i in "${!COMP_WORDS[@]}"; do
        word="${COMP_WORDS[$i]}"
        if [[ "$word" =~ ^\'|^\" ]]; then
            printf "%s\0" "$word"
        else
            echo "$word" | xargs printf "%s"
            printf "\0"
        fi
    done
}

dav_completion() {
    local cmd
    local out
    local cmd_res

    cmd="${COMP_WORDS[0]} complete $COMP_CWORD"
    out=$(dav_completion_build_args | xargs -0 $cmd)
    cmd_res=$?

    if [[ $cmd_res -eq 10 ]]; then
        compopt -o nospace
    fi
    if [[ $cmd_res -eq 12 ]]; then
        compopt -o default
        COMPREPLY=()
    else
        IFS=$'\n' declare -ga 'COMPREPLY=($out)'
    fi
}

complete -F dav_completion dav

mercurial