Skip to content
Myr

Allow user to add completion for powershell alias (#1621)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When a user has an alias in powershell, she will need to register that alias for completion. To make that possible, we store the completion logic into a scriptblock variable which can easily be accessed by the user to register aliases. For example, if the user defines an alias for `helm`: PS> sal h helm she will need to register the alias like so: PS> Register-ArgumentCompleter -CommandName 'h' -ScriptBlock $__helmCompleterBlock

authored by Marc Khouzam · committed by GitHub · parent 23fc5e0 · Browse files · 2 files+32 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Diff stats

2 files+32 added−9 removed

  • other:+32 added−9 removed

2 files+32 added−9 removed

modified:powershell_completions.go+17 added-9 removed
20 unmodified lines
21 "bytes"22 "fmt"23 "io"24 "os"25 "strings"26)2728func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) {29 // Variables should not contain a '-' or ':' character30 nameForVar := name31 nameForVar = strings.Replace(nameForVar, "-", "_", -1)32 nameForVar = strings.Replace(nameForVar, ":", "_", -1)3334 compCmd := ShellCompRequestCmd35 if !includeDesc {36 compCmd = ShellCompNoDescRequestCmd37 }
8 unmodified lines
46filter __%[1]s_escapeStringWithSpecialChars {47`+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+`48}4944Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {50[scriptblock]$__%[2]sCompleterBlock = {51 param(52 $WordToComplete,53 $CommandAst,54 $CursorPosition
16 unmodified lines
71 $Command=$Command.Substring(0,$CursorPosition)72 }73 __%[1]s_debug "Truncated command: $Command"7469 $ShellCompDirectiveError=%[3]d70 $ShellCompDirectiveNoSpace=%[4]d71 $ShellCompDirectiveNoFileComp=%[5]d72 $ShellCompDirectiveFilterFileExt=%[6]d73 $ShellCompDirectiveFilterDirs=%[7]d75 $ShellCompDirectiveError=%[4]d76 $ShellCompDirectiveNoSpace=%[5]d77 $ShellCompDirectiveNoFileComp=%[6]d78 $ShellCompDirectiveFilterFileExt=%[7]d79 $ShellCompDirectiveFilterDirs=%[8]d8081 # Prepare the command to request completions for the program.82 # Split the command at the first space to separate the program and arguments.83 $Program,$Arguments = $Command.Split(" ",2)8479 $RequestComp="$Program %[2]s $Arguments"85 $RequestComp="$Program %[3]s $Arguments"86 __%[1]s_debug "RequestComp: $RequestComp"8788 # we cannot use $WordToComplete because it89 # has the wrong values if the cursor was moved
21 unmodified lines
111 }112113 __%[1]s_debug "Calling $RequestComp"114 # First disable ActiveHelp which is not supported for Powershell109 $env:%[8]s=0115 $env:%[9]s=0116117 #call the command store the output in $out and redirect stderr and stdout to null118 # $Out is an array contains each line per element119 Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null
142 unmodified lines
262 }263264 }265}266267Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock260`, name, compCmd,268`, name, nameForVar, compCmd,269 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,270 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))271}272
38 unmodified lines
modified:shell_completions.md+15 added
533 unmodified lines
534# Without descriptions535$ helm s[tab]536search show status537```538### Aliases539540You can also configure `powershell` aliases for your program and they will also support completions.541542```543$ sal aliasname origcommand544$ Register-ArgumentCompleter -CommandName 'aliasname' -ScriptBlock $__origcommandCompleterBlock545546# and now when you run `aliasname` completion will make547# suggestions as it did for `origcommand`.548549$ aliasname <tab>550completion firstcommand secondcommand551```552The name of the completer block variable is of the form `$__<programName>CompleterBlock` where every `-` and `:` in the program name have been replaced with `_`, to respect powershell naming syntax.553554### Limitations555556* Custom completions implemented in bash scripting (legacy) are not supported and will be ignored for `powershell` (including the use of the `BashCompCustom` flag annotation).557 * You should instead use `ValidArgsFunction` and `RegisterFlagCompletionFunc()` which are portable to the different shells (`bash`, `zsh`, `fish`, `powershell`).
11 unmodified lines