-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbb-completion.bash
111 lines (89 loc) · 2.2 KB
/
bb-completion.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
#!/bin/bash
# Prints "$1" if "$1" is not in the current arguments
_bb_arg() {
local is_seen=false
for word in "${COMP_WORDS[@]}"; do
if [[ "$word" = "$1" ]]; then
is_seen=true
break
fi
done
$is_seen || printf -- "$1"
}
# Automatically adds -v and -h to the option list if they haven't already been
# given
_bb_set_options() {
COMPREPLY=( $(compgen -W "$1 $(_bb_arg "-v") $(_bb_arg "-h")" -- \
"${COMP_WORDS[$COMP_CWORD]}") )
}
_bb_use_files() {
COMPREPLY=( $(compgen -f "${COMP_WORDS[$COMP_CWORD]}") )
}
_bb_no_options() {
COMPREPLY=()
}
_bb_announcements_complete() {
_bb_no_options
}
_bb_balance_complete() {
_bb_set_options "$(_bb_arg "-u") $(_bb_arg "-d") $(_bb_arg "-h")"
}
_bb_bill_complete() {
case "${COMP_WORDS[$COMP_CWORD-1]}" in
--pdf)
_bb_use_files
;;
*)
_bb_set_options "$(_bb_arg "--pdf") $(_bb_arg "-h")"
esac
}
_bb_courses_complete() {
_bb_no_options
}
_bb_grades_complete() {
_bb_set_options "$(_bb_arg "-h")"
}
_bb_materials_complete() {
_bb_set_options "$(_bb_arg "-h")"
}
_bb_submit_complete() {
case "${COMP_WORDS[$COMP_CWORD-1]}" in
-f)
_bb_use_files
;;
-t|-c)
_bb_no_options
;;
*)
_bb_set_options "$(_bb_arg "-f") $(_bb_arg "-t") $(_bb_arg "-c")"
esac
}
_bb_pay_complete() {
_bb_set_options "$(_bb_arg "-m")"
}
_bb_payments_complete() {
_bb_no_options
}
_bb_help_complete() {
_bb_set_options "$(_bb_arg "balance") $(_bb_arg "materials") $(_bb_arg \
"submit") $(_bb_arg "bill") $(_bb_arg "pay") $(_bb_arg "grades") \
$(_bb_arg "announcements")"
}
_bb_cmd() {
printf -- "${COMP_WORDS[1]}"
}
_bb_complete() {
local commands="announcements balance bill courses grades materials submit pay payments help -h"
if [[ $COMP_CWORD -le 1 ]]; then
_bb_set_options "$commands"
return $?
fi
local cmd="$(_bb_cmd)"
[[ "$cmd" != "-h" ]] || cmd=help
if ! hash _bb_${cmd}_complete 2>/dev/null; then
_bb_set_options ""
return $?
fi
_bb_${cmd}_complete
}
complete -F _bb_complete bb