forked from liquidctl/liquidctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liquidctl.bash
341 lines (291 loc) · 7.93 KB
/
liquidctl.bash
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env bash
# Bash completions for liquidctl.
#
# Requires bash-completion.[1]
#
# Users can place this file in the `completions` subdir of
# $BASH_COMPLETION_USER_DIR (defaults to `$XDG_DATA_HOME/bash-completion` or
# `~/.local/share/bash-completion` if $XDG_DATA_HOME is not set).
#
# Distros should instead use the directory returned by
# pkg-config --variable=completionsdir bash-completion
#
# See [1] for more information.
#
# [1] https://github.com/scop/bash-completion
#
# Copyright (C) 2020-2022 Marshall Asch
# SPDX-License-Identifier: GPL-3.0-or-later
# logging method
_e() { echo "$1" >> log; }
_list_bus_options () {
liquidctl list -v | grep 'Bus:' | cut -d ':' -f 2 | sort -u
}
_list_vendor_options () {
liquidctl list -v | grep 'Vendor ID:' | cut -d ':' -f 2 | cut -c4-6 | sort -u
}
_list_product_options () {
liquidctl list -v | grep 'Product ID:' | cut -d ':' -f 2 | cut -c4-6 | sort -u
}
_list_device_options () {
liquidctl list | cut -d ' ' -f 3 | cut -d ':' -f 1 | sort -u
}
_list_match_options () {
liquidctl list | cut -d ':' -f 2 | sort -u | awk '{gsub(/\(|\)/,"",$0); print tolower($0)}'
}
_list_pick_options () {
num=$(liquidctl list | wc -l)
seq $num
}
_list_release_options () {
liquidctl list -v | grep 'Release number:' | cut -d ':' -f 2 | sort -u
}
_list_address_options () {
liquidctl list -v | grep 'Address:' | cut -d ':' -f 2 | sort -u
}
_list_port_options () {
liquidctl list -v | grep 'Port:' | cut -d ':' -f 2 | sort -u
}
_list_serial_options () {
liquidctl list -v | grep 'Serial number:' | cut -d ':' -f 2 | sort -u
}
_liquidctl_main() {
local commands="
set
initialize
list
status
"
local boolean_options="
--verbose -v
--debug -g
--json
--version
--help
--single-12v-ocp
--legacy-690lc
--non-volatile
"
local options_with_args="
--match -m
--pick -n
--vendor
--product
--release
--serial
--bus
--address
--usb-port
--device -d
--speed
--time-per-color
--time-off
--alert-threshold
--alert-color
--pump-mode
--unsafe
--direction
--start-led
--maximum-leds
--temperature-sensor
"
# generate options list and remove any flag that has already been given
# note this will note remove the short and long versions
options=($options_with_args $boolean_options)
for i in "${!options[@]}";
do
if [[ "${COMP_WORDS[@]}" =~ "${options[i]}" ]]; then
unset 'options[i]'
fi
done;
options=$(echo "${options[@]}")
# This part will check if it is currently completing a flag
local previous=$3
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$previous" in
--vendor)
COMPREPLY=($(compgen -W "$(_list_vendor_options)" -- "$cur"))
return
;;
--product)
COMPREPLY=($(compgen -W "$(_list_product_options)" -- "$cur"))
return
;;
--bus)
COMPREPLY=($(compgen -W "$(_list_bus_options)" -- "$cur"))
return
;;
--address)
COMPREPLY=($(compgen -W "$(_list_port_options)" -- "$cur"))
return
;;
--match | -m )
COMPREPLY=($(compgen -W "$(_list_match_options)" -- "$cur"))
return
;;
--pick | -n )
COMPREPLY=($(compgen -W "$(_list_pick_options)" -- "$cur"))
return
;;
--device | -d )
COMPREPLY=($(compgen -W "$(_list_device_options)" -- "$cur"))
return
;;
--release)
COMPREPLY=($(compgen -W "$(_list_release_options)" -- "$cur"))
return
;;
--serial)
COMPREPLY=($(compgen -W "$(_list_serial_options)" -- "$cur"))
return
;;
--usb-port)
COMPREPLY=($(compgen -W "$(_list_port_options)" -- "$cur"))
return
;;
--pump-mode)
COMPREPLY=($(compgen -W "balanced quiet extreme" -- "$cur"))
return
;;
--* | -[a-z]*1)
COMPREPLY=()
return
;;
esac
# This will handle auto completing arguments even if they are given at the end of the command
case "$cur" in
-*)
COMPREPLY=($(compgen -W "$options" -- "$cur"))
return
;;
esac
local i=1 cmd
# find the subcommand - first word after the flags
while [[ "$i" -lt "$COMP_CWORD" ]]
do
local s="${COMP_WORDS[i]}"
case "$s" in
--help | --version)
COMPREPLY=()
return
;;
-*) ;;
initialize | list | status | set )
cmd="$s"
break
;;
esac
(( i++ ))
done
if [[ "$i" -eq "$COMP_CWORD" ]]
then
COMPREPLY=($(compgen -W "$commands $options" -- "$cur"))
return # return early if we're still completing the 'current' command
fi
# we've completed the 'current' command and now need to call the next completion function
# subcommands have their own completion functions
case "$cmd" in
list) COMPREPLY="" ;;
initialize) _liquidctl_initialize_command ;;
status) COMPREPLY="" ;;
set) _liquidctl_set_command ;;
*) ;;
esac
}
_liquidctl_initialize_command ()
{
local i=1 subcommand_index
# find the sub command (either a fan or an led to set)
while [[ $i -lt $COMP_CWORD ]]; do
local s="${COMP_WORDS[i]}"
case "$s" in
all)
subcommand_index=$i
break
;;
esac
(( i++ ))
done
if [[ "$i" -eq "$COMP_CWORD" ]]
then
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=($(compgen -W "all" -- "$cur"))
return # return early if we're still completing the 'current' command
fi
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=() #($(compgen -W "all" -- "$cur"))
}
_liquidctl_set_command ()
{
local i=1 subcommand_index is_fan=-1
# find the sub command (either a fan or an led to set)
while [[ $i -lt $COMP_CWORD ]]; do
local s="${COMP_WORDS[i]}"
case "$s" in
fan[0-9] | fan | pump)
subcommand_index=$i
is_fan=1
break
;;
led[0-9] | led | sync | ring | logo | external)
subcommand_index=$i
is_fan=0
break
;;
esac
(( i++ ))
done
# check if it is a fan or an LED that is being set
if [[ "$is_fan" -eq "1" ]]
then
_liquidctl_set_fan
elif [[ "$is_fan" -eq "0" ]]
then
_liquidctl_set_led
else
# no trailing space here so that the fan number can be appended
compopt -o nospace
# possibly use some command here to get a list of all the possible channels from liquidctl
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=($(compgen -W "fan fan1 fan2 fan3 fan4 fan5 fan6 led led1 led2 led3 led4 led5 led6 pump sync ring logo external" -- "$cur"))
fi
}
_liquidctl_set_fan ()
{
local i=1 found=0
# find the sub command (either a fan or an led to set)
while [[ $i -lt $COMP_CWORD ]]; do
local s="${COMP_WORDS[i]}"
if [[ "$s" = "speed" ]]; then
found=1
break
fi
(( i++ ))
done
# check if it is a fan or an LED that is being set
if [[ $found = 1 ]]; then
COMPREPLY=""
else
COMPREPLY="speed"
fi
}
_liquidctl_set_led ()
{
local i=1 found=0
# find the sub command (either a fan or an led to set)
while [[ $i -lt $COMP_CWORD ]]; do
local s="${COMP_WORDS[i]}"
if [[ "$s" = "color" ]]; then
found=1
break
fi
(( i++ ))
done
# check if it is a fan or an LED that is being set
if [[ $found = 1 ]]; then
COMPREPLY=""
else
COMPREPLY="color"
fi
}
complete -F _liquidctl_main liquidctl