summaryrefslogtreecommitdiffstats
path: root/completions/dict
blob: f3b0cb63c3eeb349a613ffc96c157ec9de7559b5 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# dict(1) completion                                       -*- shell-script -*-

_comp_cmd_dict__compgen_dictdata()
{
    # shellcheck disable=SC2086
    _comp_compgen_split -- "$(
        "$@" 2>/dev/null | command sed -ne \
            's/^[[:blank:]]\{1,\}\([^[:blank:]]*\).*$/\1/p'
    )"
}

_comp_cmd_dict()
{
    local cur prev words cword comp_args
    _comp_initialize -- "$@" || return

    local -a dict_command=("$1")
    local host="" port="" db i

    local noargopts='!(-*|*[hpdis]*)'
    for ((i = 1; i < cword; i++)); do
        # shellcheck disable=SC2254
        case ${words[i]} in
            --host | -${noargopts}h)
                host=${words[++i]}
                [[ $host ]] && dict_command+=(-h "$host")
                ;;
            --port | -${noargopts}p)
                port=${words[++i]}
                [[ $port ]] && dict_command+=(-p "$port")
                ;;
            --database | -${noargopts}d)
                db=${words[++i]}
                [[ $db ]] && dict_command+=(-d "$db")
                ;;
        esac
    done

    if [[ $cur == -* ]]; then
        _comp_compgen_help
        return
    fi

    # shellcheck disable=SC2254
    case $prev in
        --database | -info | -${noargopts}[di])
            _comp_cmd_dict__compgen_dictdata "${dict_command[@]}" -D
            return
            ;;
        --strategy | -${noargopts}s)
            _comp_cmd_dict__compgen_dictdata "${dict_command[@]}" -S
            return
            ;;
    esac

    local dictfile=/usr/share/dict/words
    if [[ -r $dictfile ]]; then
        # Dictfile may be too large for practical compgen -W usage, so narrow
        # it down with grep if $cur looks like something that's safe to embed
        # in a pattern instead.
        if [[ $cur == +([-A-Za-z0-9/.]) ]]; then
            _comp_compgen_split -- "$(
                command grep "^${cur//./\\.}" "$dictfile"
            )"
        else
            _comp_compgen_split -- "$(cat "$dictfile")"
        fi
    fi
} &&
    complete -F _comp_cmd_dict -o default dict rdict

# ex: filetype=sh