Skip to content

Commit d4040ad

Browse files
authored
Allow user to add completion for powershell alias (#1621)
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 Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
1 parent 23fc5e0 commit d4040ad

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

powershell_completions.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ import (
2222
"fmt"
2323
"io"
2424
"os"
25+
"strings"
2526
)
2627

2728
func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) {
29+
// Variables should not contain a '-' or ':' character
30+
nameForVar := name
31+
nameForVar = strings.Replace(nameForVar, "-", "_", -1)
32+
nameForVar = strings.Replace(nameForVar, ":", "_", -1)
33+
2834
compCmd := ShellCompRequestCmd
2935
if !includeDesc {
3036
compCmd = ShellCompNoDescRequestCmd
@@ -41,7 +47,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
4147
`+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+`
4248
}
4349
44-
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
50+
[scriptblock]$__%[2]sCompleterBlock = {
4551
param(
4652
$WordToComplete,
4753
$CommandAst,
@@ -66,17 +72,17 @@ Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
6672
}
6773
__%[1]s_debug "Truncated command: $Command"
6874
69-
$ShellCompDirectiveError=%[3]d
70-
$ShellCompDirectiveNoSpace=%[4]d
71-
$ShellCompDirectiveNoFileComp=%[5]d
72-
$ShellCompDirectiveFilterFileExt=%[6]d
73-
$ShellCompDirectiveFilterDirs=%[7]d
75+
$ShellCompDirectiveError=%[4]d
76+
$ShellCompDirectiveNoSpace=%[5]d
77+
$ShellCompDirectiveNoFileComp=%[6]d
78+
$ShellCompDirectiveFilterFileExt=%[7]d
79+
$ShellCompDirectiveFilterDirs=%[8]d
7480
7581
# Prepare the command to request completions for the program.
7682
# Split the command at the first space to separate the program and arguments.
7783
$Program,$Arguments = $Command.Split(" ",2)
7884
79-
$RequestComp="$Program %[2]s $Arguments"
85+
$RequestComp="$Program %[3]s $Arguments"
8086
__%[1]s_debug "RequestComp: $RequestComp"
8187
8288
# we cannot use $WordToComplete because it
@@ -106,7 +112,7 @@ Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
106112
107113
__%[1]s_debug "Calling $RequestComp"
108114
# First disable ActiveHelp which is not supported for Powershell
109-
$env:%[8]s=0
115+
$env:%[9]s=0
110116
111117
#call the command store the output in $out and redirect stderr and stdout to null
112118
# $Out is an array contains each line per element
@@ -257,7 +263,9 @@ Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
257263
258264
}
259265
}
260-
`, name, compCmd,
266+
267+
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock
268+
`, name, nameForVar, compCmd,
261269
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
262270
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))
263271
}

shell_completions.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,21 @@ search for a keyword in charts
535535
$ helm s[tab]
536536
search show status
537537
```
538+
### Aliases
539+
540+
You can also configure `powershell` aliases for your program and they will also support completions.
541+
542+
```
543+
$ sal aliasname origcommand
544+
$ Register-ArgumentCompleter -CommandName 'aliasname' -ScriptBlock $__origcommandCompleterBlock
545+
546+
# and now when you run `aliasname` completion will make
547+
# suggestions as it did for `origcommand`.
548+
549+
$ aliasname <tab>
550+
completion firstcommand secondcommand
551+
```
552+
The 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.
538553

539554
### Limitations
540555

0 commit comments

Comments
 (0)