summaryrefslogtreecommitdiffstats
path: root/etc/bash-complete-sunhpc
blob: fb272fefd01c7e1f04df7a32d95c217d716062d8 (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
# sunhpc(1) completion
#
# /etc/bash_completion.d/sunhpc
#

_sunhpc_array_delete_at()
{
    eval "local ARRAY=(\"\${$1[@]}\")"
    local i
    local tmp=()
    local lower=$2
    local upper=${3:-$lower}

    for i in "${!ARRAY[@]}"; do
        if [[ "$i" -lt "$2" || "$i" -gt "${3-$2}" ]]; then
            tmp=("${tmp[@]}" "${ARRAY[$i]}")
        fi
    done
    eval "$1=(\"\${tmp[@]}\")"
}

_sunhpc()
{
    local cur prev words cword i output
    _init_completion || return

    _sunhpc_array_delete_at words $((cword+1)) ${#words[@]}
    _sunhpc_array_delete_at words 0

    for i in ${!words[@]}; do
        words[i]="$(printf '%s' "${words[i]}" | xargs printf '%s\n' 2>/dev/null || true)"
    done

    if [[ "$cur" =~ ^[[:space:]]+ ]]; then
        cur=''
    fi

    output="$(sunhpc report completion ${words[@]} 2>/dev/null)"

    if [ $? = 65 ]; then
        compopt -o default
        COMPREPLY=()
        return 0
    fi

    #local IFS=$'\n'
    COMPREPLY=( $( compgen -W '$output' -- ${cur} ) )

    # do not append a space to words that end with =
    [[ $COMPREPLY == *= ]] && compopt -o nospace

} &&
complete -F _sunhpc sunhpc

# ex: ts=4 sw=4 et filetype=sh