added keep order to bash, zsh, powershell & fish

This commit is contained in:
Gyanendra Mishra 2023-01-30 11:07:56 +00:00
parent d022c0fe2b
commit a3adbf86de
6 changed files with 68 additions and 17 deletions

View file

@ -101,6 +101,7 @@ __%[1]s_process_completion_results() {
local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveNoFileComp=%[5]d
local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterFileExt=%[6]d
local shellCompDirectiveFilterDirs=%[7]d local shellCompDirectiveFilterDirs=%[7]d
local shellCompDirectiveKeepOrder=%[8]d
if (((directive & shellCompDirectiveError) != 0)); then if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion. # Error code. No completion.
@ -115,6 +116,14 @@ __%[1]s_process_completion_results() {
__%[1]s_debug "No space directive not supported in this version of bash" __%[1]s_debug "No space directive not supported in this version of bash"
fi fi
fi fi
if (((directive & shellCompDirectiveKeepOrder) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__%[1]s_debug "Activating keep order"
compopt -o nosort
else
__%[1]s_debug "No sort directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveNoFileComp) != 0)); then if (((directive & shellCompDirectiveNoFileComp) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then if [[ $(type -t compopt) == builtin ]]; then
__%[1]s_debug "Activating no file completion" __%[1]s_debug "Activating no file completion"
@ -183,7 +192,7 @@ __%[1]s_process_completion_results() {
# Separate activeHelp lines from real completions. # Separate activeHelp lines from real completions.
# Fills the $activeHelp and $completions arrays. # Fills the $activeHelp and $completions arrays.
__%[1]s_extract_activeHelp() { __%[1]s_extract_activeHelp() {
local activeHelpMarker="%[8]s" local activeHelpMarker="%[9]s"
local endIndex=${#activeHelpMarker} local endIndex=${#activeHelpMarker}
while IFS='' read -r comp; do while IFS='' read -r comp; do
@ -351,16 +360,13 @@ __start_%[1]s()
__%[1]s_process_completion_results __%[1]s_process_completion_results
} }
if [[ $(type -t compopt) = "builtin" ]]; then # compopts are already set, we don't have to specify them here
complete -o default -F __start_%[1]s %[1]s complete -F __start_%[1]s %[1]s
else
complete -o default -o nospace -F __start_%[1]s %[1]s
fi
# ex: ts=4 sw=4 et filetype=sh # ex: ts=4 sw=4 et filetype=sh
`, name, compCmd, `, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
activeHelpMarker)) activeHelpMarker))
} }

View file

@ -77,6 +77,10 @@ const (
// obtain the same behavior but only for flags. // obtain the same behavior but only for flags.
ShellCompDirectiveFilterDirs ShellCompDirectiveFilterDirs
// ShellCompDirectiveKeepOrder indicates that the shell should preserve the order
// in which the completions are provided
ShellCompDirectiveKeepOrder
// =========================================================================== // ===========================================================================
// All directives using iota should be above this one. // All directives using iota should be above this one.
@ -159,6 +163,9 @@ func (d ShellCompDirective) string() string {
if d&ShellCompDirectiveFilterDirs != 0 { if d&ShellCompDirectiveFilterDirs != 0 {
directives = append(directives, "ShellCompDirectiveFilterDirs") directives = append(directives, "ShellCompDirectiveFilterDirs")
} }
if d&ShellCompDirectiveKeepOrder != 0 {
directives = append(directives, "ShellCompDirectiveKeepOrder")
}
if len(directives) == 0 { if len(directives) == 0 {
directives = append(directives, "ShellCompDirectiveDefault") directives = append(directives, "ShellCompDirectiveDefault")
} }

View file

@ -53,7 +53,7 @@ function __%[1]s_perform_completion
__%[1]s_debug "last arg: $lastArg" __%[1]s_debug "last arg: $lastArg"
# Disable ActiveHelp which is not supported for fish shell # Disable ActiveHelp which is not supported for fish shell
set -l requestComp "%[9]s=0 $args[1] %[3]s $args[2..-1] $lastArg" set -l requestComp "%[10]s=0 $args[1] %[3]s $args[2..-1] $lastArg"
__%[1]s_debug "Calling $requestComp" __%[1]s_debug "Calling $requestComp"
set -l results (eval $requestComp 2> /dev/null) set -l results (eval $requestComp 2> /dev/null)
@ -89,6 +89,25 @@ function __%[1]s_perform_completion
printf "%%s\n" "$directiveLine" printf "%%s\n" "$directiveLine"
end end
function __%[1]s_doesnt_requires_order_preservation
__%[1]s_debug ""
__%[1]s_debug "========= checking if order preservation is required =========="
set -l results (__%[1]s_perform_completion)
if test -z "$results"
__%[1]s_debug "Error determining if order preservation is required"
return 1
end
set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2)
if test "$keeporder" -ne 0
__%[1]s_debug "This does require order preservation"
return 1
end
end
# This function does two things: # This function does two things:
# - Obtain the completions and store them in the global __%[1]s_comp_results # - Obtain the completions and store them in the global __%[1]s_comp_results
# - Return false if file completion should be performed # - Return false if file completion should be performed
@ -119,6 +138,7 @@ function __%[1]s_prepare_completions
set -l shellCompDirectiveNoFileComp %[6]d set -l shellCompDirectiveNoFileComp %[6]d
set -l shellCompDirectiveFilterFileExt %[7]d set -l shellCompDirectiveFilterFileExt %[7]d
set -l shellCompDirectiveFilterDirs %[8]d set -l shellCompDirectiveFilterDirs %[8]d
set -l shellCompDirectiveKeepOrder %[9]d
if test -z "$directive" if test -z "$directive"
set directive 0 set directive 0
@ -207,11 +227,13 @@ complete -c %[2]s -e
# The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results # The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results
# which provides the program's completion choices. # which provides the program's completion choices.
complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' # if this doesn't require order preservation, we dont use the -k flag
complete -c %[2]s -n '__%[1]s_doesnt_requires_order_preservation' -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'
# otherwise we use the -k flag
complete -k -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'
`, nameForVar, name, compCmd, `, nameForVar, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name)))
} }
// GenFishCompletion generates fish completion file and writes to the passed writer. // GenFishCompletion generates fish completion file and writes to the passed writer.

View file

@ -77,6 +77,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
$ShellCompDirectiveNoFileComp=%[6]d $ShellCompDirectiveNoFileComp=%[6]d
$ShellCompDirectiveFilterFileExt=%[7]d $ShellCompDirectiveFilterFileExt=%[7]d
$ShellCompDirectiveFilterDirs=%[8]d $ShellCompDirectiveFilterDirs=%[8]d
$ShellCompDirectiveKeepOrder=%[9]d
# Prepare the command to request completions for the program. # Prepare the command to request completions for the program.
# Split the command at the first space to separate the program and arguments. # Split the command at the first space to separate the program and arguments.
@ -112,7 +113,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
__%[1]s_debug "Calling $RequestComp" __%[1]s_debug "Calling $RequestComp"
# First disable ActiveHelp which is not supported for Powershell # First disable ActiveHelp which is not supported for Powershell
$env:%[9]s=0 $env:%[10]s=0
#call the command store the output in $out and redirect stderr and stdout to null #call the command store the output in $out and redirect stderr and stdout to null
# $Out is an array contains each line per element # $Out is an array contains each line per element
@ -182,6 +183,11 @@ filter __%[1]s_escapeStringWithSpecialChars {
} }
} }
# we sort the values in ascending order by name if keep order isn't passed
if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) {
$Values = $Values | Sort-Object -Property Name
}
if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) { if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
__%[1]s_debug "ShellCompDirectiveNoFileComp is called" __%[1]s_debug "ShellCompDirectiveNoFileComp is called"
@ -267,7 +273,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock
`, name, nameForVar, compCmd, `, name, nameForVar, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name)))
} }
func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error { func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error {

View file

@ -237,6 +237,10 @@ ShellCompDirectiveFilterFileExt
// return []string{"themes"}, ShellCompDirectiveFilterDirs // return []string{"themes"}, ShellCompDirectiveFilterDirs
// //
ShellCompDirectiveFilterDirs ShellCompDirectiveFilterDirs
// ShellCompDirectiveKeepOrder indicates that the shell should preserve the order
// in which the completions are provided
ShellCompDirectiveKeepOrder
``` ```
***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. ***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.

View file

@ -108,8 +108,9 @@ _%[1]s()
local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveNoFileComp=%[5]d
local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterFileExt=%[6]d
local shellCompDirectiveFilterDirs=%[7]d local shellCompDirectiveFilterDirs=%[7]d
local shellCompDirectiveKeepOrder=%[8]d
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
local -a completions local -a completions
__%[1]s_debug "\n========= starting completion logic ==========" __%[1]s_debug "\n========= starting completion logic =========="
@ -177,7 +178,7 @@ _%[1]s()
return return
fi fi
local activeHelpMarker="%[8]s" local activeHelpMarker="%[9]s"
local endIndex=${#activeHelpMarker} local endIndex=${#activeHelpMarker}
local startIndex=$((${#activeHelpMarker}+1)) local startIndex=$((${#activeHelpMarker}+1))
local hasActiveHelp=0 local hasActiveHelp=0
@ -227,6 +228,11 @@ _%[1]s()
noSpace="-S ''" noSpace="-S ''"
fi fi
if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
__%[1]s_debug "Activating keep order."
keepOrder="-V"
fi
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
# File extension filtering # File extension filtering
local filteringCmd local filteringCmd
@ -262,7 +268,7 @@ _%[1]s()
return $result return $result
else else
__%[1]s_debug "Calling _describe" __%[1]s_debug "Calling _describe"
if eval _describe "completions" completions $flagPrefix $noSpace; then if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then
__%[1]s_debug "_describe found some completions" __%[1]s_debug "_describe found some completions"
# Return the success of having called _describe # Return the success of having called _describe
@ -296,6 +302,6 @@ if [ "$funcstack[1]" = "_%[1]s" ]; then
fi fi
`, name, compCmd, `, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder,
activeHelpMarker)) activeHelpMarker))
} }