aboutsummaryrefslogtreecommitdiff
path: root/grimoire/.local/bin/completion/grimoire.completion
blob: da83392b82208fb3216cc5be7ccd2c37de5a9ebf (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
#/usr/bin/env bash

_wiki() 
{
    ROOT=$HOME/git/wiki
    WORDS="list tree read edit peek search new remove sync push help"
    if [ "${#COMP_WORDS[@]}" = "2" ]; then
        # Command completion
        COMPREPLY=($(compgen -W "$WORDS" -- "${COMP_WORDS[1]}"))
    elif [ "${#COMP_WORDS[@]}" -ge "3" ]; then
        case "${COMP_WORDS[1]}" in
            new)
                # Special path/category completion for new page
                # creation. The idea is specify the categories of a new
                # page and they will converted into a path.
                # E.g.:
                #   wiki new main cat sub page.md
                # creates
                #   main/cat/sub/page.md
                local path i
                local -a toks
                for i in $(seq 2 $((${#COMP_WORDS[@]}-2)))
                do
                    path="$path/${COMP_WORDS[i]}"
                done
                toks=($(compgen -d -- "$ROOT$path/${COMP_WORDS[${#COMP_WORDS[@]}-1]}"))
                for tok in "${toks[@]}"
                do
                    # `${tok##*/}` is a Bash-ism to remove everything
                    # until the last `/` (included).
                    COMPREPLY+=(${tok##*/})
                done
                ;;
            *)
                ;;
        esac
    else
        return
    fi
}

complete -F _wiki wiki