-
Notifications
You must be signed in to change notification settings - Fork 0
/
completions.bash5
209 lines (197 loc) · 7.27 KB
/
completions.bash5
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
#
# NOTE: this file cannot even be parsed on 4.1.2(2)-release so we
# only load it on 5+
#
# add these functions to our shell environment, incus completions expect it.
# from /usr/share/bash-completion/bash_completion
#
# TODO fix upstream, these should not be required when incus completions are
# otherwise standalone, there should be barebones versions within incus itself
#
_upvars()
{
if ! (($#)); then
echo "bash_completion: $FUNCNAME: usage: $FUNCNAME" \
"[-v varname value] | [-aN varname [value ...]] ..." >&2
return 2
fi
while (($#)); do
case $1 in
-a*)
# Error checking
[[ ${1#-a} ]] || {
echo "bash_completion: $FUNCNAME:" \
"\`$1': missing number specifier" >&2
return 1
}
printf %d "${1#-a}" &>/dev/null || {
echo bash_completion: \
"$FUNCNAME: \`$1': invalid number specifier" >&2
return 1
}
# Assign array of -aN elements
[[ "$2" ]] && unset -v "$2" && eval $2=\(\"\$"{@:3:${1#-a}}"\"\) &&
shift $((${1#-a} + 2)) || {
echo bash_completion: \
"$FUNCNAME: \`$1${2+ }$2': missing argument(s)" \
>&2
return 1
}
;;
-v)
# Assign single value
[[ "$2" ]] && unset -v "$2" && eval $2=\"\$3\" &&
shift 3 || {
echo "bash_completion: $FUNCNAME: $1:" \
"missing argument(s)" >&2
return 1
}
;;
*)
echo "bash_completion: $FUNCNAME: $1: invalid option" >&2
return 1
;;
esac
done
}
__reassemble_comp_words_by_ref()
{
local exclude i j line ref
# Exclude word separator characters?
if [[ $1 ]]; then
# Yes, exclude word separator characters;
# Exclude only those characters, which were really included
exclude="[${1//[^$COMP_WORDBREAKS]/}]"
fi
# Default to cword unchanged
printf -v "$3" %s "$COMP_CWORD"
# Are characters excluded which were former included?
if [[ -v exclude ]]; then
# Yes, list of word completion separators has shrunk;
line=$COMP_LINE
# Re-assemble words to complete
for ((i = 0, j = 0; i < ${#COMP_WORDS[@]}; i++, j++)); do
# Is current word not word 0 (the command itself) and is word not
# empty and is word made up of just word separator characters to
# be excluded and is current word not preceded by whitespace in
# original line?
while [[ $i -gt 0 && ${COMP_WORDS[i]} == +($exclude) ]]; do
# Is word separator not preceded by whitespace in original line
# and are we not going to append to word 0 (the command
# itself), then append to current word.
[[ $line != [[:blank:]]* ]] && ((j >= 2)) && ((j--))
# Append word separator to current or new word
ref="$2[$j]"
printf -v "$ref" %s "${!ref-}${COMP_WORDS[i]}"
# Indicate new cword
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
# Remove optional whitespace + word separator from line copy
line=${line#*"${COMP_WORDS[i]}"}
# Start new word if word separator in original line is
# followed by whitespace.
[[ $line == [[:blank:]]* ]] && ((j++))
# Indicate next word if available, else end *both* while and
# for loop
((i < ${#COMP_WORDS[@]} - 1)) && ((i++)) || break 2
done
# Append word to current word
ref="$2[$j]"
printf -v "$ref" %s "${!ref-}${COMP_WORDS[i]}"
# Remove optional whitespace + word from line copy
line=${line#*"${COMP_WORDS[i]}"}
# Indicate new cword
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
done
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
else
# No, list of word completions separators hasn't changed;
for i in "${!COMP_WORDS[@]}"; do
printf -v "$2[i]" %s "${COMP_WORDS[i]}"
done
fi
} # __reassemble_comp_words_by_ref()
__get_cword_at_cursor_by_ref()
{
local cword words=()
__reassemble_comp_words_by_ref "$1" words cword
local i cur="" index=$COMP_POINT lead=${COMP_LINE:0:COMP_POINT}
# Cursor not at position 0 and not leaded by just space(s)?
if [[ $index -gt 0 && ($lead && ${lead//[[:space:]]/}) ]]; then
cur=$COMP_LINE
for ((i = 0; i <= cword; ++i)); do
# Current word fits in $cur, and $cur doesn't match cword?
while [[ ${#cur} -ge ${#words[i]} && \
${cur:0:${#words[i]}} != "${words[i]-}" ]]; do
# Strip first character
cur="${cur:1}"
# Decrease cursor position, staying >= 0
((index > 0)) && ((index--))
done
# Does found word match cword?
if ((i < cword)); then
# No, cword lies further;
local old_size=${#cur}
cur="${cur#"${words[i]}"}"
local new_size=${#cur}
((index -= old_size - new_size))
fi
done
# Clear $cur if just space(s)
[[ $cur && ! ${cur//[[:space:]]/} ]] && cur=
# Zero $index if negative
((index < 0)) && index=0
fi
local "$2" "$3" "$4" && _upvars -a${#words[@]} $2 ${words+"${words[@]}"} \
-v $3 "$cword" -v $4 "${cur:0:index}"
}
_get_comp_words_by_ref()
{
local exclude flag i OPTIND=1
local cur cword words=()
local upargs=() upvars=() vcur vcword vprev vwords
while getopts "c:i:n:p:w:" flag "$@"; do
case $flag in
c) vcur=$OPTARG ;;
i) vcword=$OPTARG ;;
n) exclude=$OPTARG ;;
p) vprev=$OPTARG ;;
w) vwords=$OPTARG ;;
*)
echo "bash_completion: $FUNCNAME: usage error" >&2
return 1
;;
esac
done
while [[ $# -ge $OPTIND ]]; do
case ${!OPTIND} in
cur) vcur=cur ;;
prev) vprev=prev ;;
cword) vcword=cword ;;
words) vwords=words ;;
*)
echo "bash_completion: $FUNCNAME: \`${!OPTIND}':" \
"unknown argument" >&2
return 1
;;
esac
((OPTIND += 1))
done
__get_cword_at_cursor_by_ref "${exclude-}" words cword cur
[[ -v vcur ]] && {
upvars+=("$vcur")
upargs+=(-v $vcur "$cur")
}
[[ -v vcword ]] && {
upvars+=("$vcword")
upargs+=(-v $vcword "$cword")
}
[[ -v vprev && $cword -ge 1 ]] && {
upvars+=("$vprev")
upargs+=(-v $vprev "${words[cword - 1]}")
}
[[ -v vwords ]] && {
upvars+=("$vwords")
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
}
((${#upvars[@]})) && local "${upvars[@]}" && _upvars "${upargs[@]}"
}