-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.sh
executable file
·232 lines (209 loc) · 6.19 KB
/
completion.sh
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
#!/usr/bin/env bash
# Copyright (c) 2017 Jean-Raphaël Gaglione
# name of integer variable (by value)
function _intvar {
local VAR
COMPREPLY=()
for VAR in $(compgen -v -- "${COMP_WORDS[COMP_CWORD]}"); do
[ "${!VAR}" -eq "${!VAR}" ] 2>/dev/null || continue
COMPREPLY+=("$VAR")
done
}
# name of integer variable (by value or declaration)
function _intvar_abstract {
local VAR
COMPREPLY=($(compgen -W "$(
declare -pi | awk '{print $3}' | awk -F "=" '{print $1}'
)" -- "${COMP_WORDS[COMP_CWORD]}"))
for VAR in $(compgen -v -- "${COMP_WORDS[COMP_CWORD]}"); do
[ "${!VAR}" -eq "${!VAR}" ] 2>/dev/null || continue
COMPREPLY+=("$VAR")
done
}
# arbitrary integer value
function _numval {
COMPREPLY+=("${COMP_WORDS[COMP_CWORD]}"{0..9})
# TODO: -f: accept float
# TODO: -n: accept negative numbers
# TODO: do not complete when incorrect
}
# track completion
function _track {
if [ -z "$(declare -F -- _minimal)" ]; then
# shellcheck source=/dev/null
[ -e "/usr/share/bash-completion/bash_completion" ] &&
source "/usr/share/bash-completion/bash_completion"
fi
local -i i
local arg=""
local CUR
for (( i=1; i <= COMP_CWORD; i++ )); do
CUR="${COMP_WORDS[i]}"
case "$arg" in
t|T ) ((i == COMP_CWORD)) && _numval ;;
esac
if [ -n "$arg" ]; then
arg=""
((i < COMP_CWORD)) && continue || return
fi
case "$CUR" in
--* )
if ((i < COMP_CWORD)); then
case "$CUR" in
-- ) i+=1; break ;;
--timeout ) arg="t" ;;
--delay ) arg="T" ;;
esac
else
COMPREPLY=($(compgen -W "
--or
--and
--glob
--wildcard
--timeout
--delay
--help
" -- "$CUR"))
fi ;;
-*[tT] ) arg="${CUR: -1}" ;;
-* ) ;;
* ) break ;;
esac
((i < COMP_CWORD)) || return
done
_minimal
}
# mill completion
function _mill {
if [ -z "$(declare -F -- _minimal)" ]; then
# shellcheck source=/dev/null
[ -e "/usr/share/bash-completion/bash_completion" ] &&
source "/usr/share/bash-completion/bash_completion"
fi
local -i i
local arg=""
local CUR
for (( i=1; i <= COMP_CWORD; i++ )); do
CUR="${COMP_WORDS[i]}"
case "$arg" in
p ) ((i == COMP_CWORD)) && _numval -f ;;
T ) ((i == COMP_CWORD)) && _numval ;;
F ) ((i == COMP_CWORD)) && _minimal ;;
C ) ((i == COMP_CWORD)) && _command_offset $i ;;
esac
if [ -n "$arg" ]; then
arg=""
((i < COMP_CWORD)) && continue || return
fi
case "$CUR" in
--* )
if ((i < COMP_CWORD)); then
case "$CUR" in
-- ) i+=1; break ;;
--period ) arg="p" ;;
--timeout ) arg="T" ;;
--track-file ) arg="F" ;;
--condition ) arg="C" ;;
esac
else
COMPREPLY=($(compgen -W "
--period
--instant
--manual
--timeout
--track-file
--track-command-files
--condition
--quiet
--unbuffered
--buffered
--help
" -- "$CUR"))
fi ;;
-*[pTFC] ) arg="${CUR: -1}" ;;
-* ) ;;
* ) break ;;
esac
((i < COMP_CWORD)) || return
done
_command_offset $i
}
# scale completion
function _scale {
((COMP_CWORD == 1)) && _intvar_abstract
((COMP_CWORD == 2)) && _numval -n
((COMP_CWORD == 3)) && _numval -n
}
# ++ and -- completion
function _increment {
((COMP_CWORD == 1)) && _intvar
((COMP_CWORD == 2)) && _numval -n
((COMP_CWORD == 3)) && _numval -n
}
# how completion
function _how {
if [ -z "$(declare -F -- _minimal)" ]; then
# shellcheck source=/dev/null
[ -e "/usr/share/bash-completion/bash_completion" ] &&
source "/usr/share/bash-completion/bash_completion"
fi
local -i i
local arg=""
local CUR
local -i __eval__=1
for (( i=1; i <= COMP_CWORD; i++ )); do
CUR="${COMP_WORDS[i]}"
case "$arg" in
p ) ((i == COMP_CWORD)) && {
[ "$2" != "@" ] && _numval -n
COMPREPLY+=("$(compgen -W "@" -- "$CUR")")
}
__eval__=0;;
esac
if [ -n "$arg" ]; then
arg=""
((i < COMP_CWORD)) && continue || return
fi
case "$CUR" in
--* )
if ((i < COMP_CWORD)); then
case "$CUR" in
-- ) i+=1; break ;;
--pipe-status ) arg="p" ;;
esac
else
COMPREPLY=($(compgen -W "
--pipe-status
--pipe-status-all
--help
" -- "$CUR"))
fi ;;
-*[p] ) arg="${CUR: -1}" ;;
-* ) ;;
* ) break ;;
esac
((i < COMP_CWORD)) || return
done
((__eval__)) && _command_offset $i
}
# mmake completion
function _mmake {
local OK_MAKE=0
if [ -z "$(declare -F -- _make)" ]; then
# shellcheck source=/dev/null
[ -e "/usr/share/bash-completion/completions/make" ] &&
source "/usr/share/bash-completion/completions/make" &&
OK_MAKE=1
else
OK_MAKE=1
fi
COMP_WORDS[0]="make"
((OK_MAKE)) && _make make "$2" "$3"
}
[ -n "$(declare -F -- track)" ] && complete -F _track track
[ -n "$(declare -F -- mill)" ] && complete -F _mill mill
[ -n "$(declare -F -- scale)" ] && complete -F _scale scale
[ -n "$(declare -F -- ++)" ] && complete -F _increment -- ++
[ -n "$(declare -F -- --)" ] && complete -F _increment -- --
[ -n "$(declare -F -- how)" ] && complete -F _how how
[ -n "$(declare -F -- mmake)" ] && complete -F _mmake mmake