aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/tmpls/comps/bash.sh
blob: edec28d7f6bc3ef803c3965e104367cef6123894 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
_<<<bin_name>>>_bash_completion() {
    local line="${COMP_LINE:0:COMP_POINT}"
    local cur="${line##* }"
    local prev=""
    local word_index=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")

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

    local suggestions
    if suggestions=$(<<<bin_name>>> __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

                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
    fi

    COMPREPLY=()
}

complete -F _<<<bin_name>>>_bash_completion <<<bin_name>>>