summaryrefslogtreecommitdiff
path: root/scripts/deploy/completions/bash.sh
blob: 647b5bb386be41b6e52de699c376923136bf396b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
_jvn_bash_completion() {
    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")
    args+=(-F="bash")

    for word in "${COMP_WORDS[@]}"; do
        args+=(-a="${word//-/^}")
    done

    local suggestions
    if suggestions=$(jvn_comp "${args[@]}" 2>/dev/null); then
        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
    fi

    COMPREPLY=()
}

complete -F _jvn_bash_completion jvn