Skip to content

Commit 70e49cf

Browse files
authored
Merge pull request #1048 from carapace-sh/powershell-exe
powershell: support exe extension
2 parents 548e451 + ab0f6c0 commit 70e49cf

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

example/cmd/_test/powershell.ps1

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ Function _example_completer {
4242

4343
$completions
4444
}
45-
Register-ArgumentCompleter -Native -CommandName 'example' -ScriptBlock (Get-Item "Function:_example_completer").ScriptBlock
45+
Register-ArgumentCompleter -Native -CommandName 'example' -ScriptBlock (Get-Item "Function:_example_completer").ScriptBlock
46+
# Register-ArgumentCompleter -Native -CommandName 'example.exe' -ScriptBlock (Get-Item "Function:_example_completer").ScriptBlock
4647

internal/shell/powershell/snippet.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
package powershell
33

44
import (
5+
_ "embed"
56
"fmt"
7+
"runtime"
68

79
"github.com/carapace-sh/carapace/pkg/uid"
810
"github.com/spf13/cobra"
911
)
1012

11-
// Snippet creates the powershell completion script.
12-
func Snippet(cmd *cobra.Command) string {
13-
return fmt.Sprintf(`using namespace System.Management.Automation
13+
const snippet = `using namespace System.Management.Automation
1414
using namespace System.Management.Automation.Language
1515
Function _%v_completer {
1616
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
@@ -37,14 +37,14 @@ Function _%v_completer {
3737
if ($t.get_Length() -eq 0){
3838
$t = '""'
3939
}
40-
$elems += $t.replace('`+"`"+`,', ',') # quick fix
40+
$elems += $t.replace('` + "`" + `,', ',') # quick fix
4141
}
4242
4343
$completions = @(
4444
if (!$wordToComplete) {
45-
%v _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`+"`"+`e[', "`+"`"+`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
45+
%v _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
4646
} else {
47-
%v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`+"`"+`e[', "`+"`"+`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
47+
%v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
4848
}
4949
)
5050
@@ -54,6 +54,23 @@ Function _%v_completer {
5454
5555
$completions
5656
}
57-
Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock (Get-Item "Function:_%v_completer").ScriptBlock
58-
`, cmd.Name(), uid.Executable(), uid.Executable(), cmd.Name(), cmd.Name())
57+
Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock (Get-Item "Function:_%v_completer").ScriptBlock
58+
%vRegister-ArgumentCompleter -Native -CommandName '%v.exe' -ScriptBlock (Get-Item "Function:_%v_completer").ScriptBlock
59+
`
60+
61+
// Snippet creates the powershell completion script.
62+
func Snippet(cmd *cobra.Command) string {
63+
prefix := "# "
64+
if runtime.GOOS == "windows" {
65+
prefix = ""
66+
}
67+
return fmt.Sprintf(snippet,
68+
cmd.Name(),
69+
uid.Executable(),
70+
uid.Executable(),
71+
cmd.Name(),
72+
cmd.Name(),
73+
prefix,
74+
cmd.Name(),
75+
cmd.Name())
5976
}

0 commit comments

Comments
 (0)