forked from stb-tester/stb-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stbt-completion
444 lines (385 loc) · 13.8 KB
/
stbt-completion
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# Bash tab-completion for stb-tester. -*- shell-script -*-
# Copyright 2012 David Rothlisberger <david@rothlis.net>
# Copyright 2012-2013 YouView TV Ltd.
# License: LGPL v2.1 or (at your option) any later version (see
# https://github.com/drothlis/stb-tester/blob/master/LICENSE for details).
complete -o nospace -F _stbt stbt
complete -o nospace -F _stbt_run stbt-run
complete -o nospace -F _stbt_record stbt-record
_stbt() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local IFS=$'\n'
if [ $COMP_CWORD = 1 ]; then
COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help --version \
run record batch config control lint power \
screenshot templatematch tv)" \
-- "$cur"))
else
case "${COMP_WORDS[1]}" in
run) _stbt_run;;
record) _stbt_record;;
batch) _stbt_batch;;
config) _stbt_config;;
control) _stbt_control;;
lint) _stbt_lint;;
power) _stbt_power;;
templatematch) _stbt_templatematch;;
*) COMPREPLY=();;
esac
fi
}
_stbt_run() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control=*) COMPREPLY=($(_stbt_control_uri));;
--source-pipeline=*) COMPREPLY=();;
--sink-pipeline=*) COMPREPLY=();;
--save-video=*) COMPREPLY=($(compgen -f -- "$cur"));;
*) COMPREPLY=($(compgen \
-f -W "$(_stbt_trailing_space \
--help --verbose --restart-source --save-video \
--control --source-pipeline --sink-pipeline)" \
-- "$cur"));;
esac
}
_stbt_record() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control=*) COMPREPLY=($(_stbt_control_uri));;
--source-pipeline=*) COMPREPLY=();;
--sink-pipeline=*) COMPREPLY=();;
--control-recorder=*) COMPREPLY=($(_stbt_control_recorder_uri));;
-o=*|--output-file=*) COMPREPLY=($(compgen -f -- "$cur"));;
*) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space \
--help --verbose --restart-source \
--control --source-pipeline --sink-pipeline \
--control-recorder --output-file)" \
-- "$cur"));;
esac
}
_stbt_batch() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local subcommand="${COMP_WORDS[2]}"
case "$COMP_CWORD,$subcommand" in
2,*) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help run report instaweb)" \
-- "$cur"));;
3,run) COMPREPLY=($(compgen -W "-h" -f -- "$cur"));;
*,run) COMPREPLY=($(compgen -f -- "$cur"));;
*,report) COMPREPLY=($(compgen -d -S "/" -- "$cur"));;
3,instaweb) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space \
--help localhost:5000 0.0.0.0:5000)" \
-- "$cur"));;
esac
}
_stbt_config() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ $COMP_CWORD -eq 2 ]; then
COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help $(_stbt_config_keys))" \
-- "$cur"))
fi
}
_stbt_control() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--keymap=*) COMPREPLY=($(compgen -f -- "$cur"));;
--control=*) COMPREPLY=($(_stbt_control_uri));;
*) COMPREPLY=($(compgen -W "$(_stbt_trailing_space \
--help --help-keymap --keymap --control)" \
-- "$cur"));;
esac
}
_stbt_lint() {
declare -F _pylint &>/dev/null || return # Provided by bash-completion
_pylint pylint
}
_stbt_power() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--power-outlet=*) COMPREPLY=($(_stbt_power_outlet_uri));;
*) COMPREPLY=($(compgen -W "$(_stbt_trailing_space \
--help --power-outlet on off status)" \
-- "$cur"));;
esac
}
_stbt_templatematch() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
local v
[[ $COMP_CWORD -gt 2 && ${COMP_WORDS[2]} == "-v" ]] && v="-v" || v=
[[ ${COMP_WORDS[2]} == "--help" ]] && return
case "$v,$COMP_CWORD" in
,2) COMPREPLY=($(compgen \
-f -W "$(_stbt_trailing_space --help -v)" \
-- "$cur"));;
*,3|-v,4) COMPREPLY=($(compgen -f -- "$cur"));;
,4|*,*)
case "$prev" in
match_method=) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space \
sqdiff-normed ccorr-normed ccoeff-normed)" \
-- "$cur"));;
confirm_method=) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space none absdiff normed-absdiff)" \
-- "$cur"));;
match_threshold=|erode_passes=|confirm_threshold=)
COMPREPLY=();;
*) COMPREPLY=($(compgen \
-W "$(_stbt_no_space \
match_method= match_threshold= \
confirm_method= erode_passes= confirm_threshold=)" \
-- "$cur"));;
esac
esac
}
## arguments to command-line options
############################################################################
_stbt_control_uri() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control=irnetbox:*:*:) _stbt_irnetbox_config;;
--control=irnetbox:*:) _stbt_irnetbox_output;;
--control=irnetbox:) _stbt_hostname;;
--control=lirc:*:*:) _stbt_lirc_name;;
--control=lirc:*:) _stbt_lirc_port_or_name;;
--control=lirc:) _stbt_lirc_socket_or_port_or_hostname;;
--control=vr:*:) _stbt_vr_port;;
--control=vr:) _stbt_hostname;;
*)
compgen -W "$( \
_stbt_no_space irnetbox: lirc: vr:
_stbt_trailing_space none test)" \
-- "$cur";;
esac
}
_stbt_control_recorder_uri() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control-recorder=lirc:*:*:) _stbt_lirc_name;;
--control-recorder=lirc:*:) _stbt_lirc_port_or_name;;
--control-recorder=lirc:) _stbt_lirc_socket_or_port_or_hostname;;
--control-recorder=vr:*:) _stbt_vr_port;;
--control-recorder=vr:) _stbt_hostname;;
--control-recorder=file:) _stbt_control_recorder_file;;
--control-recorder=stbt-control:) compgen -f -- "$cur";;
*) compgen -W "$( \
_stbt_no_space lirc: vr: file://
_stbt_trailing_space stbt-control)" \
-- "$cur";;
esac
}
_stbt_irnetbox_output() {
local cur="$_stbt_cur"
compgen -S ":" -W "$(
_stbt_no_space 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)" \
-- "$cur"
}
_stbt_irnetbox_config() {
local cur="$_stbt_cur"
compgen -f -- "$cur"
}
_stbt_lirc_socket_or_port_or_hostname() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_no_space $(_lirc_tcp_ports))" -- "$cur"
compgen -f -S ":" -- "$cur"
compgen -A hostname -S ":" -- "$cur"
}
_stbt_lirc_port_or_name() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_no_space $(_lirc_tcp_ports))" -- "$cur"
compgen -W "$(_stbt_trailing_space $(_lirc_remote_names))" -- "$cur"
}
_stbt_lirc_name() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_trailing_space $(_lirc_remote_names))" -- "$cur"
}
_stbt_hostname() {
local cur="$_stbt_cur"
compgen -A hostname -S ":" -- "$cur"
}
_stbt_vr_port() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_trailing_space 2033)" -- "$cur"
}
_stbt_control_recorder_file() {
local cur="$_stbt_cur"
compgen -f -P "//" -- "${cur#//}"
}
_stbt_config_keys() {
_stbt_config_file_contents |
grep -v ^__system_config |
perl -lne '
$section = $1 if /^\s*\[([a-zA-Z0-9_]+)\]\s*$/;
print "$section.$1" if /^\s*([a-zA-Z0-9_]+)\s*=/;'
}
_stbt_config_file_contents() {
[ -n "$in_unit_test" ] && { cat; return; }
local defaults=$(
awk '/stbt-"\$@"/ { print $NF }' "$(which stbt)" |
xargs dirname
)/stbt.conf
local system_config=$(stbt config global.__system_config)
local user_config="${XDG_CONFIG_HOME:-$HOME/.config}/stbt/stbt.conf"
for f in "$defaults" "$system_config" "$user_config" "$STBT_CONFIG_FILE"; do
[ -r "$f" ] && cat "$f"
done
}
_stbt_power_outlet_uri() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--power-outlet=*:*:) _stbt_power_outlet_outlet;;
--power-outlet=*:) _stbt_hostname;;
*) compgen -W "$(_stbt_no_space ipp: pdu:)" -- "$cur";;
esac
}
_stbt_power_outlet_outlet() {
local prev="$_stbt_prev"
case "$prev" in
--power-outlet=pdu:*)
compgen -W "$(_stbt_trailing_space \
1-A1 1-A2 1-A3 1-A4 1-A5 1-A6 1-A7 1-A8)" \
-- "$cur";;
esac
}
## Helper functions
############################################################################
# Walks backward from the current word, collecting the entire preceding
# flag and its argument:
# "stbt run --control lirc::name..." => _stbt_prev="--control=lirc::"
# See test cases in _stbt_test_get_prev, below.
# Note that COMP_WORDBREAKS by default includes "=" and ":".
_stbt_get_prev() {
local c p pp i
c="${COMP_WORDS[COMP_CWORD]}"
p="$c"
i=$((COMP_CWORD-1))
while [ $i -gt 1 ]; do
pp="${COMP_WORDS[i]}" # the word previous to $p
case "$pp,$p" in
[=:],*|::,*|*,[=:]*) p="$pp$p";;
*) _stbt_takes_arg "$pp" || break; p="$pp=$p";;
esac
i=$((i-1))
done
[[ "$c" == ":" || "$c" == "::" || "$c" == "=" ]] && c=""
p="${p%$c}"
_stbt_cur="$c"
_stbt_prev="$p"
}
_stbt_takes_arg() {
case "$1" in
--control|--source-pipeline|--sink-pipeline) true;;
--save-video) true;;
--control-recorder|-o|--output-file) true;;
--keymap) true;;
--power-outlet) true;;
*) false;;
esac
}
_stbt_trailing_space() {
local c
for c in $*; do printf -- "$c \n"; done
}
_stbt_no_space() {
local c
for c in $*; do printf -- "$c\n"; done
}
_lirc_tcp_ports() {
ps ax | grep 'lircd' | \
perl -nle 'print $1 if (/--listen=(\d+)/ || /-l +(\d+)/)' | sort
}
_lirc_remote_names() {
cat /etc/lirc/lircd.conf 2>/dev/null |
awk '/^begin remote/ { inremote = 1 }
inremote && /^ *name / { print $2; inremote = 0 }'
}
## Unit tests
############################################################################
_stbt_fail() { printf "error: $*\n"; exit 1; }
_stbt_assert_get_prev() {
[ "$_stbt_prev" = "$1" ] ||
_stbt_fail "_stbt_prev was '$_stbt_prev' (expected '$1')"
[ "$_stbt_cur" = "$2" ] ||
_stbt_fail "_stbt_cur was '$_stbt_cur' (expected '$2')"
echo _stbt_get_prev: ok
}
_stbt_test_get_prev() {
# User types "--control "
COMP_WORDS=(stbt run --control ""); COMP_CWORD=3; _stbt_get_prev
_stbt_assert_get_prev --control= ""
COMP_WORDS=(stbt run --control lir); COMP_CWORD=3; _stbt_get_prev
_stbt_assert_get_prev --control= lir
COMP_WORDS=(stbt run --control lirc :); COMP_CWORD=4; _stbt_get_prev
_stbt_assert_get_prev --control=lirc: ""
COMP_WORDS=(stbt run --control lirc : /var); COMP_CWORD=5; _stbt_get_prev
_stbt_assert_get_prev --control=lirc: /var
COMP_WORDS=(stbt run --control lirc : :); COMP_CWORD=5; _stbt_get_prev
_stbt_assert_get_prev --control=lirc:: ""
COMP_WORDS=(stbt run --control lirc ::); COMP_CWORD=4; _stbt_get_prev
_stbt_assert_get_prev --control=lirc:: ""
COMP_WORDS=(stbt run --control lirc : : ""); COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev --control=lirc:: ""
COMP_WORDS=(stbt run --control lirc : : abc); COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev --control=lirc:: abc
# User types "--control="
COMP_WORDS=(stbt run --control = ""); COMP_CWORD=4; _stbt_get_prev
_stbt_assert_get_prev --control= ""
COMP_WORDS=(stbt run --control = lir); COMP_CWORD=4; _stbt_get_prev
_stbt_assert_get_prev --control= lir
# _stbt_prev stops at most recent "--" flag
COMP_WORDS=(stbt run -o test.py --control lir); COMP_CWORD=5; _stbt_get_prev
_stbt_assert_get_prev --control= lir
COMP_WORDS=(stbt run -o test.py --control = lirc :); COMP_CWORD=7; _stbt_get_prev
_stbt_assert_get_prev --control=lirc: ""
# --control=file://...
COMP_WORDS=(stbt run --control = file : ""); COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev --control=file: ""
COMP_WORDS=(stbt run --control = file : //); COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev --control=file: //
COMP_WORDS=(stbt run --control = file : ///dev/stdin); COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev --control=file: ///dev/stdin
# User types "match_method="
COMP_WORDS=(stbt templatematch frame.png template.png match_method = "");
COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev match_method= ""
COMP_WORDS=(stbt templatematch frame.png template.png match_method = sqdiff);
COMP_CWORD=6; _stbt_get_prev
_stbt_assert_get_prev match_method= sqdiff
}
_stbt_config_for_tests() {
cat <<-EOF
[global]
key1 = hi there
key2=hi there
key3 = hi there
key4 = hi there
[run]
key5 = hi there
EOF
}
_stbt_test_config_keys() {
diff \
<(_stbt_config_for_tests | in_unit_test=yes _stbt_config_keys) \
<(printf '%s\n' \
global.key1 global.key2 global.key3 global.key4 run.key5) ||
_stbt_fail "_stbt_config_keys unexpected output"
echo _stbt_config_keys: ok
}