1# bash completion V2 for git-bug -*- shell-script -*-
2
3__git-bug_debug()
4{
5 if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
6 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
7 fi
8}
9
10# Macs have bash3 for which the bash-completion package doesn't include
11# _init_completion. This is a minimal version of that function.
12__git-bug_init_completion()
13{
14 COMPREPLY=()
15 _get_comp_words_by_ref "$@" cur prev words cword
16}
17
18# This function calls the git-bug program to obtain the completion
19# results and the directive. It fills the 'out' and 'directive' vars.
20__git-bug_get_completion_results() {
21 local requestComp lastParam lastChar args
22
23 # Prepare the command to request completions for the program.
24 # Calling ${words[0]} instead of directly git-bug allows to handle aliases
25 args=("${words[@]:1}")
26 requestComp="${words[0]} __complete ${args[*]}"
27
28 lastParam=${words[$((${#words[@]}-1))]}
29 lastChar=${lastParam:$((${#lastParam}-1)):1}
30 __git-bug_debug "lastParam ${lastParam}, lastChar ${lastChar}"
31
32 if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then
33 # If the last parameter is complete (there is a space following it)
34 # We add an extra empty parameter so we can indicate this to the go method.
35 __git-bug_debug "Adding extra empty parameter"
36 requestComp="${requestComp} ''"
37 fi
38
39 # When completing a flag with an = (e.g., git-bug -n=<TAB>)
40 # bash focuses on the part after the =, so we need to remove
41 # the flag part from $cur
42 if [[ "${cur}" == -*=* ]]; then
43 cur="${cur#*=}"
44 fi
45
46 __git-bug_debug "Calling ${requestComp}"
47 # Use eval to handle any environment variables and such
48 out=$(eval "${requestComp}" 2>/dev/null)
49
50 # Extract the directive integer at the very end of the output following a colon (:)
51 directive=${out##*:}
52 # Remove the directive
53 out=${out%:*}
54 if [ "${directive}" = "${out}" ]; then
55 # There is not directive specified
56 directive=0
57 fi
58 __git-bug_debug "The completion directive is: ${directive}"
59 __git-bug_debug "The completions are: ${out[*]}"
60}
61
62__git-bug_process_completion_results() {
63 local shellCompDirectiveError=1
64 local shellCompDirectiveNoSpace=2
65 local shellCompDirectiveNoFileComp=4
66 local shellCompDirectiveFilterFileExt=8
67 local shellCompDirectiveFilterDirs=16
68
69 if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
70 # Error code. No completion.
71 __git-bug_debug "Received error from custom completion go code"
72 return
73 else
74 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
75 if [[ $(type -t compopt) = "builtin" ]]; then
76 __git-bug_debug "Activating no space"
77 compopt -o nospace
78 else
79 __git-bug_debug "No space directive not supported in this version of bash"
80 fi
81 fi
82 if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
83 if [[ $(type -t compopt) = "builtin" ]]; then
84 __git-bug_debug "Activating no file completion"
85 compopt +o default
86 else
87 __git-bug_debug "No file completion directive not supported in this version of bash"
88 fi
89 fi
90 fi
91
92 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
93 # File extension filtering
94 local fullFilter filter filteringCmd
95
96 # Do not use quotes around the $out variable or else newline
97 # characters will be kept.
98 for filter in ${out[*]}; do
99 fullFilter+="$filter|"
100 done
101
102 filteringCmd="_filedir $fullFilter"
103 __git-bug_debug "File filtering command: $filteringCmd"
104 $filteringCmd
105 elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
106 # File completion for directories only
107
108 # Use printf to strip any trailing newline
109 local subdir
110 subdir=$(printf "%s" "${out[0]}")
111 if [ -n "$subdir" ]; then
112 __git-bug_debug "Listing directories in $subdir"
113 pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
114 else
115 __git-bug_debug "Listing directories in ."
116 _filedir -d
117 fi
118 else
119 __git-bug_handle_completion_types
120 fi
121
122 __git-bug_handle_special_char "$cur" :
123 __git-bug_handle_special_char "$cur" =
124}
125
126__git-bug_handle_completion_types() {
127 __git-bug_debug "__git-bug_handle_completion_types: COMP_TYPE is $COMP_TYPE"
128
129 case $COMP_TYPE in
130 37|42)
131 # Type: menu-complete/menu-complete-backward and insert-completions
132 # If the user requested inserting one completion at a time, or all
133 # completions at once on the command-line we must remove the descriptions.
134 # https://github.com/spf13/cobra/issues/1508
135 local tab comp
136 tab=$(printf '\t')
137 while IFS='' read -r comp; do
138 # Strip any description
139 comp=${comp%%$tab*}
140 # Only consider the completions that match
141 comp=$(compgen -W "$comp" -- "$cur")
142 if [ -n "$comp" ]; then
143 COMPREPLY+=("$comp")
144 fi
145 done < <(printf "%s\n" "${out[@]}")
146 ;;
147
148 *)
149 # Type: complete (normal completion)
150 __git-bug_handle_standard_completion_case
151 ;;
152 esac
153}
154
155__git-bug_handle_standard_completion_case() {
156 local tab comp
157 tab=$(printf '\t')
158
159 local longest=0
160 # Look for the longest completion so that we can format things nicely
161 while IFS='' read -r comp; do
162 # Strip any description before checking the length
163 comp=${comp%%$tab*}
164 # Only consider the completions that match
165 comp=$(compgen -W "$comp" -- "$cur")
166 if ((${#comp}>longest)); then
167 longest=${#comp}
168 fi
169 done < <(printf "%s\n" "${out[@]}")
170
171 local completions=()
172 while IFS='' read -r comp; do
173 if [ -z "$comp" ]; then
174 continue
175 fi
176
177 __git-bug_debug "Original comp: $comp"
178 comp="$(__git-bug_format_comp_descriptions "$comp" "$longest")"
179 __git-bug_debug "Final comp: $comp"
180 completions+=("$comp")
181 done < <(printf "%s\n" "${out[@]}")
182
183 while IFS='' read -r comp; do
184 COMPREPLY+=("$comp")
185 done < <(compgen -W "${completions[*]}" -- "$cur")
186
187 # If there is a single completion left, remove the description text
188 if [ ${#COMPREPLY[*]} -eq 1 ]; then
189 __git-bug_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
190 comp="${COMPREPLY[0]%% *}"
191 __git-bug_debug "Removed description from single completion, which is now: ${comp}"
192 COMPREPLY=()
193 COMPREPLY+=("$comp")
194 fi
195}
196
197__git-bug_handle_special_char()
198{
199 local comp="$1"
200 local char=$2
201 if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
202 local word=${comp%"${comp##*${char}}"}
203 local idx=${#COMPREPLY[*]}
204 while [[ $((--idx)) -ge 0 ]]; do
205 COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"}
206 done
207 fi
208}
209
210__git-bug_format_comp_descriptions()
211{
212 local tab
213 tab=$(printf '\t')
214 local comp="$1"
215 local longest=$2
216
217 # Properly format the description string which follows a tab character if there is one
218 if [[ "$comp" == *$tab* ]]; then
219 desc=${comp#*$tab}
220 comp=${comp%%$tab*}
221
222 # $COLUMNS stores the current shell width.
223 # Remove an extra 4 because we add 2 spaces and 2 parentheses.
224 maxdesclength=$(( COLUMNS - longest - 4 ))
225
226 # Make sure we can fit a description of at least 8 characters
227 # if we are to align the descriptions.
228 if [[ $maxdesclength -gt 8 ]]; then
229 # Add the proper number of spaces to align the descriptions
230 for ((i = ${#comp} ; i < longest ; i++)); do
231 comp+=" "
232 done
233 else
234 # Don't pad the descriptions so we can fit more text after the completion
235 maxdesclength=$(( COLUMNS - ${#comp} - 4 ))
236 fi
237
238 # If there is enough space for any description text,
239 # truncate the descriptions that are too long for the shell width
240 if [ $maxdesclength -gt 0 ]; then
241 if [ ${#desc} -gt $maxdesclength ]; then
242 desc=${desc:0:$(( maxdesclength - 1 ))}
243 desc+="…"
244 fi
245 comp+=" ($desc)"
246 fi
247 fi
248
249 # Must use printf to escape all special characters
250 printf "%q" "${comp}"
251}
252
253__start_git-bug()
254{
255 local cur prev words cword split
256
257 COMPREPLY=()
258
259 # Call _init_completion from the bash-completion package
260 # to prepare the arguments properly
261 if declare -F _init_completion >/dev/null 2>&1; then
262 _init_completion -n "=:" || return
263 else
264 __git-bug_init_completion -n "=:" || return
265 fi
266
267 __git-bug_debug
268 __git-bug_debug "========= starting completion logic =========="
269 __git-bug_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
270
271 # The user could have moved the cursor backwards on the command-line.
272 # We need to trigger completion from the $cword location, so we need
273 # to truncate the command-line ($words) up to the $cword location.
274 words=("${words[@]:0:$cword+1}")
275 __git-bug_debug "Truncated words[*]: ${words[*]},"
276
277 local out directive
278 __git-bug_get_completion_results
279 __git-bug_process_completion_results
280}
281
282if [[ $(type -t compopt) = "builtin" ]]; then
283 complete -o default -F __start_git-bug git-bug
284else
285 complete -o default -o nospace -F __start_git-bug git-bug
286fi
287
288# ex: ts=4 sw=4 et filetype=sh
289
290# Custom bash code to connect the git completion for "git bug" to the
291# git-bug completion for "git-bug"
292_git_bug() {
293 local cur prev words cword split
294
295 COMPREPLY=()
296
297 # Call _init_completion from the bash-completion package
298 # to prepare the arguments properly
299 if declare -F _init_completion >/dev/null 2>&1; then
300 _init_completion -n "=:" || return
301 else
302 __git-bug_init_completion -n "=:" || return
303 fi
304
305 # START PATCH
306 # replace in the array ("git","bug", ...) to ("git-bug", ...) and adjust the index in cword
307 words=("git-bug" "${words[@]:2}")
308 cword=$(($cword-1))
309 # END PATCH
310
311 __git-bug_debug
312 __git-bug_debug "========= starting completion logic =========="
313 __git-bug_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
314
315 # The user could have moved the cursor backwards on the command-line.
316 # We need to trigger completion from the $cword location, so we need
317 # to truncate the command-line ($words) up to the $cword location.
318 words=("${words[@]:0:$cword+1}")
319 __git-bug_debug "Truncated words[*]: ${words[*]},"
320
321 local out directive
322 __git-bug_get_completion_results
323 __git-bug_process_completion_results
324}