aboutsummaryrefslogtreecommitdiffstats
path: root/completions/_rsp
blob: 1ddc1fe3547a65b6b2f74c2269137035de3782a3 (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
# Dynamic completion overrides for rsp play/queue arguments.

# Save clap-generated function so we can delegate non-dynamic cases.
functions[_rsp_clap]=$functions[_rsp]

_rsp() {
    case $words[2] in
        play|queue)
            _rsp_complete_args
            ;;
        *)
            _rsp_clap "$@"
            ;;
    esac
}

_rsp_complete_args() {
    setopt localoptions
    setopt nobanghist
    # Reconstruct full query from all argument words after subcommand.
    # This handles spaces in names (e.g. "Master of Puppets") that get
    # split across multiple $words entries when backslash escaping breaks.
    local -a args=("${(@)words[3,$CURRENT]}")
    local cur="${(j: :)args}"
    local -a suggestions
    local n_slashes=${#cur//[^\/]/}
    case $n_slashes in
        0) suggestions=(${(f)"$(rsp db-complete artists "$cur" 2>/dev/null)"}) ;;
        1) suggestions=(${(f)"$(rsp db-complete albums "$cur" 2>/dev/null)"}) ;;
        *) suggestions=(${(f)"$(rsp db-complete tracks "$cur" 2>/dev/null)"}) ;;
    esac
    # display: what the user sees in the completion menu (raw values)
    local -a display=("${suggestions[@]}")
    # vals: what gets inserted (with ZSH backslash escaping + ! escape)
    local -a vals=()
    local slash_idx=$cur[(I)/]
    local s
    for s in "${suggestions[@]}"; do
        local esc="${(q)s:gs/\!/\\\!/}"
        if (( slash_idx )); then
            local dir_part=$cur[1,$slash_idx]
            local edir="${(q)dir_part:gs/\!/\\\!/}"
            vals+=("${edir}${esc}")
        else
            vals+=("${esc}")
        fi
    done
    if (( ${#suggestions} )); then
        if [[ ${vals[1]} == */ ]]; then
            compadd -U -S '' -d display -- "${vals[@]}"
        else
            compadd -U -d display -- "${vals[@]}"
        fi
    fi
}