summaryrefslogtreecommitdiffstats
path: root/completions/xrandr
blob: c071ab44799632e55b75fc9689046b7cfc0fbca1 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# bash completion for xrandr                               -*- shell-script -*-

_comp_cmd_xrandr__compgen_outputs()
{
    _comp_compgen_split -- "$("$1" -q 2>/dev/null | _comp_awk '/connected/ {print $1}')"
}

_comp_cmd_xrandr__compgen_monitors()
{
    _comp_compgen_split -- "$(
        "$1" --listmonitors 2>/dev/null |
            command sed -ne 's/.* [0-9]*: [+\*]*\([^ ]*\).*/\1/p'
    )"
}

_comp_cmd_xrandr__compgen_providers()
{
    _comp_compgen_split -- "$("$1" --listproviders 2>/dev/null |
        command sed -ne "s/.*cap:.*$2.* name:\([^ ]*\).*/\1/p")"
}

_comp_cmd_xrandr__compgen_modes()
{
    _comp_compgen_split -- "$(
        "$1" -q 2>/dev/null | command sed \
            -e "1,/^$2 / d" \
            -e '/connected/,$ d' \
            -e '/^[[:space:]]*h: / d' \
            -e '/^[[:space:]]*v: / d' \
            -e 's/\([^[:space:]]\)[[:space:]].*/\1/'
    )"
}

_comp_cmd_xrandr__compgen_all_modes()
{
    _comp_compgen_split -- "$(
        "$1" -q 2>/dev/null | command sed \
            -e '/^[^[:space:]].*/ d' \
            -e '/^[[:space:]]*h: / d' \
            -e '/^[[:space:]]*v: / d' \
            -e 's/[[:space:]]*\([^[:space:]]*\)[[:space:]].*/\1/'
    )"
}

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

    local i output has_output=""
    for ((i = cword - 1; i > 0; i--)); do
        if [[ ${words[i]} == --output ]]; then
            output=${words[i + 1]}
            has_output=set
            break
        fi
    done

    case "$prev" in
        -display | -d | -s | --size | -r | --rate | --refresh | --screen | \
            --fb | --fbmm | --pos | --set | --scale | --transform | --crtc | \
            --panning | --gamma | --newmode | --rmmode | --setmonitor)
            return
            ;;
        --output | --addmode | --delmode | --dpi)
            _comp_cmd_xrandr__compgen_outputs "$1"
            return
            ;;
        --left-of | --right-of | --above | --below | --same-as)
            if [[ $has_output ]]; then
                _comp_cmd_xrandr__compgen_outputs "$1"
            fi
            return
            ;;
        --mode)
            if [[ $has_output ]]; then
                _comp_cmd_xrandr__compgen_modes "$1" "$output"
            fi
            return
            ;;
        -o | --orientation)
            _comp_compgen -- -W 'normal inverted left right 0 1 2 3'
            return
            ;;
        --reflect)
            if [[ $has_output ]]; then
                _comp_compgen -- -W 'normal x y xy'
            fi
            return
            ;;
        --rotate)
            if [[ $has_output ]]; then
                _comp_compgen -- -W 'normal inverted left right'
            fi
            return
            ;;
        --filter)
            if [[ $has_output ]]; then
                _comp_compgen -- -W 'bilinear nearest'
            fi
            return
            ;;
        --setprovideroutputsource)
            _comp_cmd_xrandr__compgen_providers "$1" "Sink Output"
            return
            ;;
        --setprovideroffloadsink)
            _comp_cmd_xrandr__compgen_providers "$1" "Source Offload"
            return
            ;;
        --delmonitor)
            _comp_cmd_xrandr__compgen_monitors "$1"
            return
            ;;
    esac

    # second arguments
    if ((cword >= 2)); then
        case "${words[cword - 2]}" in
            --set)
                return
                ;;
            --addmode)
                _comp_cmd_xrandr__compgen_all_modes "$1"
                return
                ;;
            --delmode)
                _comp_cmd_xrandr__compgen_modes "$1" "${words[cword - 1]}"
                return
                ;;
            --setmonitor)
                _comp_compgen -- -W 'auto'
                return
                ;;
            --setprovideroutputsource)
                _comp_cmd_xrandr__compgen_providers "$1" "Source Output"
                _comp_compgen -a -- -W "0x0"
                return
                ;;
            --setprovideroffloadsink)
                _comp_cmd_xrandr__compgen_providers "$1" "Sink Offload"
                _comp_compgen -a -- -W "0x0"
                return
                ;;
        esac
    fi

    # third arguments
    if ((cword >= 3)); then
        case "${words[cword - 3]}" in
            --setmonitor)
                _comp_compgen -c "${cur##*,}" -i xrandr outputs "$1"
                _comp_compgen -ac "${cur##*,}" -- -W "none"
                _comp_delimited , -W '"${COMPREPLY[@]}"'
                return
                ;;
        esac
    fi

    local options
    if [[ $has_output ]]; then
        _comp_compgen -v options help - <<<"$(
            "$1" --help 2>/dev/null |
                command sed -e 's/ or /\n  /g' -e 's/<[^>]*>]//g'
        )"
    else
        # if no output is specified, remove per-output options
        _comp_compgen -v options help - <<<"$(
            "$1" --help 2>/dev/null |
                command sed -e '/^  -/!d' -e 's/ or /\n  /g' -e 's/<[^>]*>]//g'
        )"
    fi

    _comp_compgen -- -W '"${options[@]}"'
} &&
    complete -F _comp_cmd_xrandr xrandr

# ex: filetype=sh