summaryrefslogtreecommitdiffstats
path: root/completions/mr
blob: 660dfd8d7e1ada893a3de9029f1797d0963560ee (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# mr completion                                            -*- shell-script -*-

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

    local help commands options

    help="$(PERLDOC_PAGER=cat PERLDOC=-otext "${1}" help 2>/dev/null)"

    commands="$(
        # shellcheck disable=SC2030
        printf %s "$help" | while read -r _ options cmd _; do
            [[ $options != "[options]" ]] || printf "%s\n" "$cmd"
        done
    )"
    # Split [online|offline] and remove `action` placeholder.
    commands="${commands//@(action|[\[\|\]])/ }"
    # Add standard aliases.
    commands="${commands} ci co ls"
    _comp_split commands "$commands"
    local IFS='|'
    local glob_commands="@(${commands[*]})"
    _comp_unlocal IFS

    # Determine if user has entered an `mr` command. Used to block top-level
    # (option and command) completions.
    local cmd has_cmd="" i
    for ((i = 1; i < cword; i++)); do
        # shellcheck disable=SC2053
        if [[ ${words[i]} == $glob_commands ]]; then
            cmd="${words[i]}"
            has_cmd=set
            break
        fi
    done

    # Complete options for specific commands.
    if [[ $has_cmd ]]; then
        case $cmd in
            bootstrap)
                _comp_compgen_filedir
                # Also complete stdin (-) as a potential bootstrap source.
                if [[ ! ${cur} || $cur == - ]] && [[ $prev != - ]]; then
                    COMPREPLY+=(-)
                fi
                return
                ;;
            clean)
                if [[ ${cur} == -* ]]; then
                    _comp_compgen -- -W '-f'
                fi
                return
                ;;
            commit | ci | record)
                if [[ ${cur} == -* ]]; then
                    _comp_compgen -- -W '-m'
                fi
                return
                ;;
            run)
                _comp_compgen_commands
                return
                ;;
            *)
                # Do not complete any other command.
                return
                ;;
        esac
    fi

    # Complete top-level options and commands.
    local noargopts='!(-*|*[cd]*)'
    # shellcheck disable=SC2254
    case $prev in
        --config | -${noargopts}c)
            _comp_compgen_filedir
            return
            ;;
        --directory | -${noargopts}d)
            _comp_compgen_filedir -d
            return
            ;;
    esac

    if [[ $cur == -* ]]; then
        _comp_compgen -Rv options help - <<<"$help"
        # -X '-[a-z]': Remove short options (all have compatible long options).
        # -X '--path': Remove deprecated options.
        _comp_compgen -- -W '"${options[@]}"' -X '@(-[a-z]|--path)'
    else
        _comp_compgen -- -W '"${commands[@]}"'
    fi
} &&
    complete -F _comp_cmd_mr mr

# ex: filetype=sh