Skip to content
Myr

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

6 files+112 added−17 removed

modified:bash_completionsV2.go+16 added-2 removed
99 unmodified lines
100 local shellCompDirectiveNoSpace=%[4]d101 local shellCompDirectiveNoFileComp=%[5]d102 local shellCompDirectiveFilterFileExt=%[6]d103 local shellCompDirectiveFilterDirs=%[7]d104 local shellCompDirectiveKeepOrder=%[8]d105106 if (((directive & shellCompDirectiveError) != 0)); then107 # Error code. No completion.108 __%[1]s_debug "Received error from custom completion go code"
6 unmodified lines
115 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 default
60 unmodified lines
196197# 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}202203 while IFS='' read -r comp; do204 if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
168 unmodified lines
373374# ex: ts=4 sw=4 et filetype=sh375`, name, compCmd,376 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,363 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs,377 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,378 activeHelpMarker))379}380381// GenBashCompletionFileV2 generates Bash completion version 2.
15 unmodified lines
modified:completions.go+7 added
75 unmodified lines
76 // which to search. The BashCompSubdirsInDir annotation can be used to77 // obtain the same behavior but only for flags.78 ShellCompDirectiveFilterDirs7980 // ShellCompDirectiveKeepOrder indicates that the shell should preserve the order81 // in which the completions are provided82 ShellCompDirectiveKeepOrder8384 // ===========================================================================8586 // All directives using iota should be above this one.87 // For internal use.
74 unmodified lines
162 }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 lines
modified:fish_completions.go+67 added-9 removed
51 unmodified lines
52 __%[1]s_debug "args: $args"53 __%[1]s_debug "last arg: $lastArg"5455 # 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"5758 __%[1]s_debug "Calling $requestComp"59 set -l results (eval $requestComp 2> /dev/null)60
27 unmodified lines
8889 printf "%%s\n" "$directiveLine"90end9192# this function limits calls to __%[1]s_perform_completion, by caching the result behind $__%[1]s_perform_completion_once_result93function __%[1]s_perform_completion_once94 __%[1]s_debug "Starting __%[1]s_perform_completion_once"9596 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 end100101 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 end106107 __%[1]s_debug "Performed completions and set __%[1]s_perform_completion_once_result"108 return 0109end110111# this function is used to clear the $__%[1]s_perform_completion_once_result variable after completions are run112function __%[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"117end118119function __%[1]s_requires_order_preservation120 __%[1]s_debug ""121 __%[1]s_debug "========= checking if order preservation is required =========="122123 __%[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 end128129 set -l directive (string sub --start 2 $__%[1]s_perform_completion_once_result[-1])130 __%[1]s_debug "Directive is: $directive"131132 set -l shellCompDirectiveKeepOrder %[9]d133 set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2)134 __%[1]s_debug "Keeporder is: $keeporder"135136 if test $keeporder -ne 0137 __%[1]s_debug "This does require order preservation"138 return 0139 end140141 __%[1]s_debug "This doesn't require order preservation"142 return 1143end144145146# 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 performed149function __%[1]s_prepare_completions
2 unmodified lines
152153 # Start fresh154 set --erase __%[1]s_comp_results155102 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"158105 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 end164111 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]167168 __%[1]s_debug "Completions are: $__%[1]s_comp_results"169 __%[1]s_debug "Directive is: $directive"170
87 unmodified lines
258259# Remove any pre-existing completions for the program since we will be handling all of them.260complete -c %[2]s -e261262# this will get called after the two calls below and clear the $__%[1]s_perform_completion_once_result global263complete -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.210complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'211266# If this doesn't require order preservation, we don't use the -k flag267complete -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 flag269complete -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}274275// GenFishCompletion generates fish completion file and writes to the passed writer.276func (c *Command) GenFishCompletion(w io.Writer, includeDesc bool) error {
16 unmodified lines
modified:powershell_completions.go+8 added-2 removed
75 unmodified lines
76 $ShellCompDirectiveNoSpace=%[5]d77 $ShellCompDirectiveNoFileComp=%[6]d78 $ShellCompDirectiveFilterFileExt=%[7]d79 $ShellCompDirectiveFilterDirs=%[8]d80 $ShellCompDirectiveKeepOrder=%[9]d8182 # 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 lines
112 }113114 __%[1]s_debug "Calling $RequestComp"115 # First disable ActiveHelp which is not supported for Powershell115 $env:%[9]s=0116 $env:%[10]s=0117118 #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-Null
61 unmodified lines
182 $_.Name = $Flag + "=" + $_.Name183 }184 }185186 # 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 }190191 if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {192 __%[1]s_debug "ShellCompDirectiveNoFileComp is called"193194 if ($Values.Length -eq 0) {
77 unmodified lines
272273Register-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}278279func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error {280 buf := new(bytes.Buffer)
36 unmodified lines
modified:shell_completions.md+4 added
226 unmodified lines
227// to complete directories within "themes/":228// return []string{"themes"}, ShellCompDirectiveFilterDirs229//230ShellCompDirectiveFilterDirs231232// ShellCompDirectiveKeepOrder indicates that the shell should preserve the order233// in which the completions are provided234ShellCompDirectiveKeepOrder235```236237***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 lines
modified:zsh_completions.go+10 added-4 removed
106 unmodified lines
107 local shellCompDirectiveNoSpace=%[4]d108 local shellCompDirectiveNoFileComp=%[5]d109 local shellCompDirectiveFilterFileExt=%[6]d110 local shellCompDirectiveFilterDirs=%[7]d111 local shellCompDirectiveKeepOrder=%[8]d112112 local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace113 local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder114 local -a completions115116 __%[1]s_debug "\n========= starting completion logic =========="117 __%[1]s_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
59 unmodified lines
177 __%[1]s_debug "Completion received error. Ignoring completions."178 return179 fi180180 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; do
41 unmodified lines
227 __%[1]s_debug "Activating nospace."228 noSpace="-S ''"229 fi230231 if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then232 __%[1]s_debug "Activating keep order."233 keepOrder="-V"234 fi235236 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then237 # File extension filtering238 local filteringCmd239 filteringCmd='_files'
27 unmodified lines
267 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"273274 # Return the success of having called _describe275 return 0
25 unmodified lines
301 _%[1]s302fi303`, name, compCmd,304 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,299 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs,305 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,306 activeHelpMarker))307}