Skip to content
Myr

bash_completionsV2.go

This file

Path

bash_completionsV2.go

484 lines · 17.4 KiB · Go

Last changed by

perf: Loop over the bash variable directly instead of starting subprocesses. (#2333)Jeffrey Faerbefore adoption10d4b48

History of this file

Take it

The walk stopped after 200 commits: lines older than 89d7b83 point at it.

update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a

line 1why, one step back
1// Copyright 2013-2023 The Cobra Authors

add missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9

lines 2–14why, one step back
2//
3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 15–40why, one step back
15package cobra
1617import (18 "bytes"19 "fmt"20 "io"21 "os"22)2324func (c *Command) genBashCompletion(w io.Writer, includeDesc bool) error {25 buf := new(bytes.Buffer)26 genBashComp(buf, c.Name(), includeDesc)27 _, err := buf.WriteTo(w)28 return err29}3031func genBashComp(buf io.StringWriter, name string, includeDesc bool) {32 compCmd := ShellCompRequestCmd33 if !includeDesc {34 compCmd = ShellCompNoDescRequestCmd35 }3637 WriteStringAndCheck(buf, fmt.Sprintf(`# bash completion V2 for %-36[1]s -*- shell-script -*-3839__%[1]s_debug()40{

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 41why, one step back
41 if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 42–59why, one step back
42 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
43 fi44}4546# Macs have bash3 for which the bash-completion package doesn't include47# _init_completion. This is a minimal version of that function.48__%[1]s_init_completion()49{50 COMPREPLY=()51 _get_comp_words_by_ref "$@" cur prev words cword52}5354# This function calls the %[1]s program to obtain the completion55# results and the directive. It fills the 'out' and 'directive' vars.56__%[1]s_get_completion_results() {57 local requestComp lastParam lastChar args5859 # Prepare the command to request completions for the program.

Fix grammar: 'allows to' (#1978)Taavi Väänänenbefore adoption2246fa8

line 60why, one step back
60 # Calling ${words[0]} instead of directly %[1]s allows handling aliases

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 61–67why, one step back
61 args=("${words[@]:1}")
62 requestComp="${words[0]} %[2]s ${args[*]}"6364 lastParam=${words[$((${#words[@]}-1))]}65 lastChar=${lastParam:$((${#lastParam}-1)):1}66 __%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}"67

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 68why, one step back
68 if [[ -z ${cur} && ${lastChar} != = ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 69–77why, one step back
69 # If the last parameter is complete (there is a space following it)
70 # We add an extra empty parameter so we can indicate this to the go method.71 __%[1]s_debug "Adding extra empty parameter"72 requestComp="${requestComp} ''"73 fi7475 # When completing a flag with an = (e.g., %[1]s -n=<TAB>)76 # bash focuses on the part after the =, so we need to remove77 # the flag part from $cur

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 78why, one step back
78 if [[ ${cur} == -*=* ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 79–89why, one step back
79 cur="${cur#*=}"
80 fi8182 __%[1]s_debug "Calling ${requestComp}"83 # Use eval to handle any environment variables and such84 out=$(eval "${requestComp}" 2>/dev/null)8586 # Extract the directive integer at the very end of the output following a colon (:)87 directive=${out##*:}88 # Remove the directive89 out=${out%%:*}

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 90why, one step back
90 if [[ ${directive} == "${out}" ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 91–94why, one step back
91 # There is not directive specified
92 directive=093 fi94 __%[1]s_debug "The completion directive is: ${directive}"

style(bash-v2): out is not an array variable, do not refer to it as such (#1681)Ville Skyttäbefore adoption4f0facb

line 95why, one step back
95 __%[1]s_debug "The completions are: ${out}"

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 96–103why, one step back
96}
9798__%[1]s_process_completion_results() {99 local shellCompDirectiveError=%[3]d100 local shellCompDirectiveNoSpace=%[4]d101 local shellCompDirectiveNoFileComp=%[5]d102 local shellCompDirectiveFilterFileExt=%[6]d103 local shellCompDirectiveFilterDirs=%[7]d

Add keeporder to shell completion (#1903)Gyanendra Mishrabefore adoption3daa4b9

line 104why, one step back
104 local shellCompDirectiveKeepOrder=%[8]d

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 105why, one step back
105

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 106why, one step back
106 if (((directive & shellCompDirectiveError) != 0)); then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 107–110why, one step back
107 # Error code. No completion.
108 __%[1]s_debug "Received error from custom completion go code"109 return110 else

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

lines 111–112why, one step back
111 if (((directive & shellCompDirectiveNoSpace) != 0)); then
112 if [[ $(type -t compopt) == builtin ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 113–118why, one step back
113 __%[1]s_debug "Activating no space"
114 compopt -o nospace115 else116 __%[1]s_debug "No space directive not supported in this version of bash"117 fi118 fi

Add keeporder to shell completion (#1903)Gyanendra Mishrabefore adoption3daa4b9

lines 119–131why, one step back
119 if (((directive & shellCompDirectiveKeepOrder) != 0)); then
120 if [[ $(type -t compopt) == builtin ]]; then121 # no sort isn't supported for bash less than < 4.4122 if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then123 __%[1]s_debug "No sort directive not supported in this version of bash"124 else125 __%[1]s_debug "Activating keep order"126 compopt -o nosort127 fi128 else129 __%[1]s_debug "No sort directive not supported in this version of bash"130 fi131 fi

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

lines 132–133why, one step back
132 if (((directive & shellCompDirectiveNoFileComp) != 0)); then
133 if [[ $(type -t compopt) == builtin ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 134–141why, one step back
134 __%[1]s_debug "Activating no file completion"
135 compopt +o default136 else137 __%[1]s_debug "No file completion directive not supported in this version of bash"138 fi139 fi140 fi141

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 142–146why, one step back
142 # Separate activeHelp from normal completions
143 local completions=()144 local activeHelp=()145 __%[1]s_extract_activeHelp146

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 147why, one step back
147 if (((directive & shellCompDirectiveFilterFileExt) != 0)); then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 148why, one step back
148 # File extension filtering

fix(bash): nounset unbound file filter variable on empty extension (#2228)Ville Skyttäbefore adoption4ba5566

line 149why, one step back
149 local fullFilter="" filter filteringCmd

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 150why, one step back
150

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 151why, one step back
151 # Do not use quotes around the $completions variable or else newline

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 152why, one step back
152 # characters will be kept.

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 153why, one step back
153 for filter in ${completions[*]}; do

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 154–159why, one step back
154 fullFilter+="$filter|"
155 done156157 filteringCmd="_filedir $fullFilter"158 __%[1]s_debug "File filtering command: $filteringCmd"159 $filteringCmd

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 160why, one step back
160 elif (((directive & shellCompDirectiveFilterDirs) != 0)); then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 161–163why, one step back
161 # File completion for directories only
162163 local subdir

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

lines 164–165why, one step back
164 subdir=${completions[0]}
165 if [[ -n $subdir ]]; then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 166–177why, one step back
166 __%[1]s_debug "Listing directories in $subdir"
167 pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return168 else169 __%[1]s_debug "Listing directories in ."170 _filedir -d171 fi172 else173 __%[1]s_handle_completion_types174 fi175176 __%[1]s_handle_special_char "$cur" :177 __%[1]s_handle_special_char "$cur" =

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 178–179why, one step back
178
179 # Print the activeHelp statements before we finish

Print ActiveHelp for bash along other completions (#2076)Marc Khouzambefore adoption41b26ec

lines 180–184why, one step back
180 __%[1]s_handle_activeHelp
181}182183__%[1]s_handle_activeHelp() {184 # Print the activeHelp statements

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 185why, one step back
185 if ((${#activeHelp[*]} != 0)); then

Print ActiveHelp for bash along other completions (#2076)Marc Khouzambefore adoption41b26ec

lines 186–193why, one step back
186 if [ -z $COMP_TYPE ]; then
187 # Bash v3 does not set the COMP_TYPE variable.188 printf "\n";189 printf "%%s\n" "${activeHelp[@]}"190 printf "\n"191 __%[1]s_reprint_commandLine192 return193 fi

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 194why, one step back
194

Print ActiveHelp for bash along other completions (#2076)Marc Khouzambefore adoption41b26ec

lines 195–231why, one step back
195 # Only print ActiveHelp on the second TAB press
196 if [ $COMP_TYPE -eq 63 ]; then197 printf "\n"198 printf "%%s\n" "${activeHelp[@]}"199200 if ((${#COMPREPLY[*]} == 0)); then201 # When there are no completion choices from the program, file completion202 # may kick in if the program has not disabled it; in such a case, we want203 # to know if any files will match what the user typed, so that we know if204 # there will be completions presented, so that we know how to handle ActiveHelp.205 # To find out, we actually trigger the file completion ourselves;206 # the call to _filedir will fill COMPREPLY if files match.207 if (((directive & shellCompDirectiveNoFileComp) == 0)); then208 __%[1]s_debug "Listing files"209 _filedir210 fi211 fi212213 if ((${#COMPREPLY[*]} != 0)); then214 # If there are completion choices to be shown, print a delimiter.215 # Re-printing the command-line will automatically be done216 # by the shell when it prints the completion choices.217 printf -- "--"218 else219 # When there are no completion choices at all, we need220 # to re-print the command-line since the shell will221 # not be doing it itself.222 __%[1]s_reprint_commandLine223 fi224 elif [ $COMP_TYPE -eq 37 ] || [ $COMP_TYPE -eq 42 ]; then225 # For completion type: menu-complete/menu-complete-backward and insert-completions226 # the completions are immediately inserted into the command-line, so we first227 # print the activeHelp message and reprint the command-line since the shell won't.228 printf "\n"229 printf "%%s\n" "${activeHelp[@]}"230231 __%[1]s_reprint_commandLine

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 232why, one step back
232 fi

Print ActiveHelp for bash along other completions (#2076)Marc Khouzambefore adoption41b26ec

lines 233–244why, one step back
233 fi
234}235236__%[1]s_reprint_commandLine() {237 # The prompt format is only available from bash 4.4.238 # We test if it is available before using it.239 if (x=${PS1@P}) 2> /dev/null; then240 printf "%%s" "${PS1@P}${COMP_LINE[@]}"241 else242 # Can't print the prompt. Just print the243 # text the user had typed, it is workable enough.244 printf "%%s" "${COMP_LINE[@]}"

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 245why, one step back
245 fi

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 246–247why, one step back
246}
247

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 248–250why, one step back
248# Separate activeHelp lines from real completions.
249# Fills the $activeHelp and $completions arrays.250__%[1]s_extract_activeHelp() {

Add keeporder to shell completion (#1903)Gyanendra Mishrabefore adoption3daa4b9

line 251why, one step back
251 local activeHelpMarker="%[9]s"

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 252–254why, one step back
252 local endIndex=${#activeHelpMarker}
253254 while IFS='' read -r comp; do

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 255–256why, one step back
255 [[ -z $comp ]] && continue
256

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 257why, one step back
257 if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 258–259why, one step back
258 comp=${comp:endIndex}
259 __%[1]s_debug "ActiveHelp found: $comp"

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 260why, one step back
260 if [[ -n $comp ]]; then

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 261–266why, one step back
261 activeHelp+=("$comp")
262 fi263 else264 # Not an activeHelp line but a normal completion265 completions+=("$comp")266 fi

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 267why, one step back
267 done <<<"${out}"

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 268–269why, one step back
268}
269

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 270–278why, one step back
270__%[1]s_handle_completion_types() {
271 __%[1]s_debug "__%[1]s_handle_completion_types: COMP_TYPE is $COMP_TYPE"272273 case $COMP_TYPE in274 37|42)275 # Type: menu-complete/menu-complete-backward and insert-completions276 # If the user requested inserting one completion at a time, or all277 # completions at once on the command-line we must remove the descriptions.278 # https://github.com/spf13/cobra/issues/1508

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 279–293why, one step back
279
280 # If there are no completions, we don't need to do anything281 (( ${#completions[@]} == 0 )) && return 0282283 local tab=$'\t'284285 # Strip any description and escape the completion to handled special characters286 IFS=$'\n' read -ra completions -d '' < <(printf "%%q\n" "${completions[@]%%%%$tab*}")287288 # Only consider the completions that match289 IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${completions[*]}" -- "${cur}")290291 # compgen looses the escaping so we need to escape all completions again since they will292 # all be inserted on the command-line.293 IFS=$'\n' read -ra COMPREPLY -d '' < <(printf "%%q\n" "${COMPREPLY[@]}")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 294–303why, one step back
294 ;;
295296 *)297 # Type: complete (normal completion)298 __%[1]s_handle_standard_completion_case299 ;;300 esac301}302303__%[1]s_handle_standard_completion_case() {

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

line 304why, one step back
304 local tab=$'\t'

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 305why, one step back
305

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 306–308why, one step back
306 # If there are no completions, we don't need to do anything
307 (( ${#completions[@]} == 0 )) && return 0308

perf(bash-v2): short-circuit descriptionless candidate lists (#1686)Ville Skyttäbefore adoption95d7df1

line 309why, one step back
309 # Short circuit to optimize if we don't have descriptions

Fix handling of descriptions for bash v3 (#1735)Marc Khouzambefore adoption7c9831d

line 310why, one step back
310 if [[ "${completions[*]}" != *$tab* ]]; then

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 311–322why, one step back
311 # First, escape the completions to handle special characters
312 IFS=$'\n' read -ra completions -d '' < <(printf "%%q\n" "${completions[@]}")313 # Only consider the completions that match what the user typed314 IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${completions[*]}" -- "${cur}")315316 # compgen looses the escaping so, if there is only a single completion, we need to317 # escape it again because it will be inserted on the command-line. If there are multiple318 # completions, we don't want to escape them because they will be printed in a list319 # and we don't want to show escape characters in that list.320 if (( ${#COMPREPLY[@]} == 1 )); then321 COMPREPLY[0]=$(printf "%%q" "${COMPREPLY[0]}")322 fi

perf(bash-v2): short-circuit descriptionless candidate lists (#1686)Ville Skyttäbefore adoption95d7df1

lines 323–325why, one step back
323 return 0
324 fi325

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 326why, one step back
326 local longest=0

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

line 327why, one step back
327 local compline

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 328why, one step back
328 # Look for the longest completion so that we can format things nicely

perf: Loop over the bash variable directly instead of starting subprocesses. (#2333)Jeffrey Faerbefore adoption10d4b48

line 329why, one step back
329 for compline in "${completions[@]}"; do

fix(bash-v2): skip empty completions when filtering descriptions (#1691)Ville Skyttäbefore adoptione1ded5c

line 330why, one step back
330 [[ -z $compline ]] && continue

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 331–338why, one step back
331
332 # Before checking if the completion matches what the user typed,333 # we need to strip any description and escape the completion to handle special334 # characters because those escape characters are part of what the user typed.335 # Don't call "printf" in a sub-shell because it will be much slower336 # since we are in a loop.337 printf -v comp "%%q" "${compline%%%%$tab*}" &>/dev/null || comp=$(printf "%%q" "${compline%%%%$tab*}")338

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 339why, one step back
339 # Only consider the completions that match

perf(bash-v2): speed up filtering entries with descriptions (#1689)Ville Skyttäbefore adoptionfb80311

line 340why, one step back
340 [[ $comp == "$cur"* ]] || continue

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 341–345why, one step back
341
342 # The completions matches. Add it to the list of full completions including343 # its description. We don't escape the completion because it may get printed344 # in a list if there are more than one and we don't want show escape characters345 # in that list.

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

line 346why, one step back
346 COMPREPLY+=("$compline")

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 347–351why, one step back
347
348 # Strip any description before checking the length, and again, don't escape349 # the completion because this length is only used when printing the completions350 # in a list and we don't want show escape characters in that list.351 comp=${compline%%%%$tab*}

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 352–354why, one step back
352 if ((${#comp}>longest)); then
353 longest=${#comp}354 fi

perf: Loop over the bash variable directly instead of starting subprocesses. (#2333)Jeffrey Faerbefore adoption10d4b48

line 355why, one step back
355 done

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 356why, one step back
356

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

line 357why, one step back
357 # If there is a single completion left, remove the description text and escape any special characters

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 358why, one step back
358 if ((${#COMPREPLY[*]} == 1)); then

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 359why, one step back
359 __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"

Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)Toni Kangasbefore adoption09d5664

lines 360–363why, one step back
360 COMPREPLY[0]=$(printf "%%q" "${COMPREPLY[0]%%%%$tab*}")
361 __%[1]s_debug "Removed description from single completion, which is now: ${COMPREPLY[0]}"362 else363 # Format the descriptions

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

line 364why, one step back
364 __%[1]s_format_comp_descriptions $longest

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 365–374why, one step back
365 fi
366}367368__%[1]s_handle_special_char()369{370 local comp="$1"371 local char=$2372 if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then373 local word=${comp%%"${comp##*${char}}"}374 local idx=${#COMPREPLY[*]}

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

lines 375–376why, one step back
375 while ((--idx >= 0)); do
376 COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 377–382why, one step back
377 done
378 fi379}380381__%[1]s_format_comp_descriptions()382{

perf(bash-v2): use backslash escape string expansion for tab (#1682)Ville Skyttäbefore adoption2722a75

line 383why, one step back
383 local tab=$'\t'

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 384–385why, one step back
384 local comp desc maxdesclength
385 local longest=$1

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 386why, one step back
386

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 387–394why, one step back
387 local i ci
388 for ci in ${!COMPREPLY[*]}; do389 comp=${COMPREPLY[ci]}390 # Properly format the description string which follows a tab character if there is one391 if [[ "$comp" == *$tab* ]]; then392 __%[1]s_debug "Original comp: $comp"393 desc=${comp#*$tab}394 comp=${comp%%%%$tab*}

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 395why, one step back
395

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 396–398why, one step back
396 # $COLUMNS stores the current shell width.
397 # Remove an extra 4 because we add 2 spaces and 2 parentheses.398 maxdesclength=$(( COLUMNS - longest - 4 ))

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 399why, one step back
399

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 400–401why, one step back
400 # Make sure we can fit a description of at least 8 characters
401 # if we are to align the descriptions.

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 402why, one step back
402 if ((maxdesclength > 8)); then

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 403–410why, one step back
403 # Add the proper number of spaces to align the descriptions
404 for ((i = ${#comp} ; i < longest ; i++)); do405 comp+=" "406 done407 else408 # Don't pad the descriptions so we can fit more text after the completion409 maxdesclength=$(( COLUMNS - ${#comp} - 4 ))410 fi

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 411why, one step back
411

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 412–413why, one step back
412 # If there is enough space for any description text,
413 # truncate the descriptions that are too long for the shell width

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

lines 414–415why, one step back
414 if ((maxdesclength > 0)); then
415 if ((${#desc} > maxdesclength)); then

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 416–419why, one step back
416 desc=${desc:0:$(( maxdesclength - 1 ))}
417 desc+="…"418 fi419 comp+=" ($desc)"

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 420why, one step back
420 fi

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

lines 421–422why, one step back
421 COMPREPLY[ci]=$comp
422 __%[1]s_debug "Final comp: $comp"

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 423why, one step back
423 fi

perf(bash-v2): standard completion optimizations (#1683)Ville Skyttäbefore adoption09d6ba6

line 424why, one step back
424 done

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 425–435why, one step back
425}
426427__start_%[1]s()428{429 local cur prev words cword split430431 COMPREPLY=()432433 # Call _init_completion from the bash-completion package434 # to prepare the arguments properly435 if declare -F _init_completion >/dev/null 2>&1; then

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 436why, one step back
436 _init_completion -n =: || return

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 437why, one step back
437 else

style(bash-v2): various cleanups (#1702)Ville Skyttäbefore adoptionbadcce1

line 438why, one step back
438 __%[1]s_init_completion -n =: || return

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 439–464why, one step back
439 fi
440441 __%[1]s_debug442 __%[1]s_debug "========= starting completion logic =========="443 __%[1]s_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"444445 # The user could have moved the cursor backwards on the command-line.446 # We need to trigger completion from the $cword location, so we need447 # to truncate the command-line ($words) up to the $cword location.448 words=("${words[@]:0:$cword+1}")449 __%[1]s_debug "Truncated words[*]: ${words[*]},"450451 local out directive452 __%[1]s_get_completion_results453 __%[1]s_process_completion_results454}455456if [[ $(type -t compopt) = "builtin" ]]; then457 complete -o default -F __start_%[1]s %[1]s458else459 complete -o default -o nospace -F __start_%[1]s %[1]s460fi461462# ex: ts=4 sw=4 et filetype=sh463`, name, compCmd,464 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,

Add keeporder to shell completion (#1903)Gyanendra Mishrabefore adoption3daa4b9

line 465why, one step back
465 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 466why, one step back
466 activeHelpMarker))

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 467–484why, one step back
467}
468469// GenBashCompletionFileV2 generates Bash completion version 2.470func (c *Command) GenBashCompletionFileV2(filename string, includeDesc bool) error {471 outFile, err := os.Create(filename)472 if err != nil {473 return err474 }475 defer outFile.Close()476477 return c.GenBashCompletionV2(outFile, includeDesc)478}479480// GenBashCompletionV2 generates Bash completion file version 2481// and writes it to the passed writer.482func (c *Command) GenBashCompletionV2(w io.Writer, includeDesc bool) error {483 return c.genBashCompletion(w, includeDesc)484}