Add keeporder to shell completion (#1903)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This allows programs to request the shell to maintain the order of completions that was returned by the program
authored by Gyanendra Mishra · committed by GitHub · parent a516d41 · Browse files · 6 files·+112 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+112 added−17 removed
- other:+112 added−17 removed
Provenance
- Commit
3daa4b9- Tree
973006a- Author
Gyanendra Mishra <anomaly.the@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a516d41
6 files changed+112 added−17 removed
modified:bash_completionsV2.go+16 added-2 removed
99 unmodified lines100 local shellCompDirectiveNoSpace=%[4]d101 local shellCompDirectiveNoFileComp=%[5]d102 local shellCompDirectiveFilterFileExt=%[6]d103 local shellCompDirectiveFilterDirs=%[7]d104+ local shellCompDirectiveKeepOrder=%[8]d105 106 if (((directive & shellCompDirectiveError) != 0)); then107 # Error code. No completion.108 __%[1]s_debug "Received error from custom completion go code"6 unmodified lines115 else116 __%[1]s_debug "No space directive not supported in this version of bash"117 fi118 fi119+ if (((directive & shellCompDirectiveKeepOrder) != 0)); then120+ 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+ fi132 if (((directive & shellCompDirectiveNoFileComp) != 0)); then133 if [[ $(type -t compopt) == builtin ]]; then134 __%[1]s_debug "Activating no file completion"135 compopt +o default60 unmodified lines196 197 # Separate activeHelp lines from real completions.198 # Fills the $activeHelp and $completions arrays.199 __%[1]s_extract_activeHelp() {186- local activeHelpMarker="%[8]s"200+ local activeHelpMarker="%[9]s"201 local endIndex=${#activeHelpMarker}202 203 while IFS='' read -r comp; do204 if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then168 unmodified lines373 374 # ex: ts=4 sw=4 et filetype=sh375 `, name, compCmd,376 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,363- ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs,377+ ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,378 activeHelpMarker))379 }380 381 // GenBashCompletionFileV2 generates Bash completion version 2.15 unmodified linesmodified:completions.go+7 added
75 unmodified lines76 // which to search. The BashCompSubdirsInDir annotation can be used to77 // obtain the same behavior but only for flags.78 ShellCompDirectiveFilterDirs79 80+ // ShellCompDirectiveKeepOrder indicates that the shell should preserve the order81+ // in which the completions are provided82+ ShellCompDirectiveKeepOrder83+84 // ===========================================================================85 86 // All directives using iota should be above this one.87 // For internal use.74 unmodified lines162 }163 if d&ShellCompDirectiveFilterDirs != 0 {164 directives = append(directives, "ShellCompDirectiveFilterDirs")165 }166+ if d&ShellCompDirectiveKeepOrder != 0 {167+ directives = append(directives, "ShellCompDirectiveKeepOrder")168+ }169 if len(directives) == 0 {170 directives = append(directives, "ShellCompDirectiveDefault")171 }172 706 unmodified linesmodified:fish_completions.go+67 added-9 removed
51 unmodified lines52 __%[1]s_debug "args: $args"53 __%[1]s_debug "last arg: $lastArg"54 55 # Disable ActiveHelp which is not supported for fish shell56- set -l requestComp "%[9]s=0 $args[1] %[3]s $args[2..-1] $lastArg"56+ set -l requestComp "%[10]s=0 $args[1] %[3]s $args[2..-1] $lastArg"57 58 __%[1]s_debug "Calling $requestComp"59 set -l results (eval $requestComp 2> /dev/null)60 27 unmodified lines88 89 printf "%%s\n" "$directiveLine"90 end91 92+# this function limits calls to __%[1]s_perform_completion, by caching the result behind $__%[1]s_perform_completion_once_result93+function __%[1]s_perform_completion_once94+ __%[1]s_debug "Starting __%[1]s_perform_completion_once"95+96+ if test -n "$__%[1]s_perform_completion_once_result"97+ __%[1]s_debug "Seems like a valid result already exists, skipping __%[1]s_perform_completion"98+ return 099+ end100+101+ set --global __%[1]s_perform_completion_once_result (__%[1]s_perform_completion)102+ if test -z "$__%[1]s_perform_completion_once_result"103+ __%[1]s_debug "No completions, probably due to a failure"104+ return 1105+ end106+107+ __%[1]s_debug "Performed completions and set __%[1]s_perform_completion_once_result"108+ return 0109+end110+111+# this function is used to clear the $__%[1]s_perform_completion_once_result variable after completions are run112+function __%[1]s_clear_perform_completion_once_result113+ __%[1]s_debug ""114+ __%[1]s_debug "========= clearing previously set __%[1]s_perform_completion_once_result variable =========="115+ set --erase __%[1]s_perform_completion_once_result116+ __%[1]s_debug "Succesfully erased the variable __%[1]s_perform_completion_once_result"117+end118+119+function __%[1]s_requires_order_preservation120+ __%[1]s_debug ""121+ __%[1]s_debug "========= checking if order preservation is required =========="122+123+ __%[1]s_perform_completion_once124+ if test -z "$__%[1]s_perform_completion_once_result"125+ __%[1]s_debug "Error determining if order preservation is required"126+ return 1127+ end128+129+ set -l directive (string sub --start 2 $__%[1]s_perform_completion_once_result[-1])130+ __%[1]s_debug "Directive is: $directive"131+132+ set -l shellCompDirectiveKeepOrder %[9]d133+ set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2)134+ __%[1]s_debug "Keeporder is: $keeporder"135+136+ if test $keeporder -ne 0137+ __%[1]s_debug "This does require order preservation"138+ return 0139+ end140+141+ __%[1]s_debug "This doesn't require order preservation"142+ return 1143+end144+145+146 # This function does two things:147 # - Obtain the completions and store them in the global __%[1]s_comp_results148 # - Return false if file completion should be performed149 function __%[1]s_prepare_completions2 unmodified lines152 153 # Start fresh154 set --erase __%[1]s_comp_results155 102- set -l results (__%[1]s_perform_completion)103- __%[1]s_debug "Completion results: $results"156+ __%[1]s_perform_completion_once157+ __%[1]s_debug "Completion results: $__%[1]s_perform_completion_once_result"158 105- if test -z "$results"159+ if test -z "$__%[1]s_perform_completion_once_result"160 __%[1]s_debug "No completion, probably due to a failure"161 # Might as well do file completion, in case it helps162 return 1163 end164 111- set -l directive (string sub --start 2 $results[-1])112- set --global __%[1]s_comp_results $results[1..-2]165+ set -l directive (string sub --start 2 $__%[1]s_perform_completion_once_result[-1])166+ set --global __%[1]s_comp_results $__%[1]s_perform_completion_once_result[1..-2]167 168 __%[1]s_debug "Completions are: $__%[1]s_comp_results"169 __%[1]s_debug "Directive is: $directive"170 87 unmodified lines258 259 # Remove any pre-existing completions for the program since we will be handling all of them.260 complete -c %[2]s -e261 262+# this will get called after the two calls below and clear the $__%[1]s_perform_completion_once_result global263+complete -c %[2]s -n '__%[1]s_clear_perform_completion_once_result'264 # The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results265 # which provides the program's completion choices.210-complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'211-266+# If this doesn't require order preservation, we don't use the -k flag267+complete -c %[2]s -n 'not __%[1]s_requires_order_preservation && __%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'268+# otherwise we use the -k flag269+complete -k -c %[2]s -n '__%[1]s_requires_order_preservation && __%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'270 `, nameForVar, name, compCmd,271 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,214- ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))272+ ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name)))273 }274 275 // GenFishCompletion generates fish completion file and writes to the passed writer.276 func (c *Command) GenFishCompletion(w io.Writer, includeDesc bool) error {16 unmodified linesmodified:powershell_completions.go+8 added-2 removed
75 unmodified lines76 $ShellCompDirectiveNoSpace=%[5]d77 $ShellCompDirectiveNoFileComp=%[6]d78 $ShellCompDirectiveFilterFileExt=%[7]d79 $ShellCompDirectiveFilterDirs=%[8]d80+ $ShellCompDirectiveKeepOrder=%[9]d81 82 # Prepare the command to request completions for the program.83 # Split the command at the first space to separate the program and arguments.84 $Program,$Arguments = $Command.Split(" ",2)27 unmodified lines112 }113 114 __%[1]s_debug "Calling $RequestComp"115 # First disable ActiveHelp which is not supported for Powershell115- $env:%[9]s=0116+ $env:%[10]s=0117 118 #call the command store the output in $out and redirect stderr and stdout to null119 # $Out is an array contains each line per element120 Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null61 unmodified lines182 $_.Name = $Flag + "=" + $_.Name183 }184 }185 186+ # we sort the values in ascending order by name if keep order isn't passed187+ if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) {188+ $Values = $Values | Sort-Object -Property Name189+ }190+191 if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {192 __%[1]s_debug "ShellCompDirectiveNoFileComp is called"193 194 if ($Values.Length -eq 0) {77 unmodified lines272 273 Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock274 `, name, nameForVar, compCmd,275 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,270- ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))276+ ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name)))277 }278 279 func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error {280 buf := new(bytes.Buffer)36 unmodified linesmodified:shell_completions.md+4 added
226 unmodified lines227 // to complete directories within "themes/":228 // return []string{"themes"}, ShellCompDirectiveFilterDirs229 //230 ShellCompDirectiveFilterDirs231+232+// ShellCompDirectiveKeepOrder indicates that the shell should preserve the order233+// in which the completions are provided234+ShellCompDirectiveKeepOrder235 ```236 237 ***Note***: When using the `ValidArgsFunction`, Cobra will call your registered function after having parsed all flags and arguments provided in the command-line. You therefore don't need to do this parsing yourself. For example, when a user calls `helm status --namespace my-rook-ns [tab][tab]`, Cobra will call your registered `ValidArgsFunction` after having parsed the `--namespace` flag, as it would have done when calling the `RunE` function.238 338 unmodified linesmodified:zsh_completions.go+10 added-4 removed
106 unmodified lines107 local shellCompDirectiveNoSpace=%[4]d108 local shellCompDirectiveNoFileComp=%[5]d109 local shellCompDirectiveFilterFileExt=%[6]d110 local shellCompDirectiveFilterDirs=%[7]d111+ local shellCompDirectiveKeepOrder=%[8]d112 112- local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace113+ local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder114 local -a completions115 116 __%[1]s_debug "\n========= starting completion logic =========="117 __%[1]s_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"59 unmodified lines177 __%[1]s_debug "Completion received error. Ignoring completions."178 return179 fi180 180- local activeHelpMarker="%[8]s"181+ local activeHelpMarker="%[9]s"182 local endIndex=${#activeHelpMarker}183 local startIndex=$((${#activeHelpMarker}+1))184 local hasActiveHelp=0185 while IFS='\n' read -r comp; do41 unmodified lines227 __%[1]s_debug "Activating nospace."228 noSpace="-S ''"229 fi230 231+ if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then232+ __%[1]s_debug "Activating keep order."233+ keepOrder="-V"234+ fi235+236 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then237 # File extension filtering238 local filteringCmd239 filteringCmd='_files'27 unmodified lines267 fi268 return $result269 else270 __%[1]s_debug "Calling _describe"265- if eval _describe "completions" completions $flagPrefix $noSpace; then271+ if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then272 __%[1]s_debug "_describe found some completions"273 274 # Return the success of having called _describe275 return 025 unmodified lines301 _%[1]s302 fi303 `, name, compCmd,304 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,299- ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs,305+ ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,306 activeHelpMarker))307 }