2
2
package powershell
3
3
4
4
import (
5
+ _ "embed"
5
6
"fmt"
7
+ "runtime"
6
8
7
9
"github.com/carapace-sh/carapace/pkg/uid"
8
10
"github.com/spf13/cobra"
9
11
)
10
12
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
14
14
using namespace System.Management.Automation.Language
15
15
Function _%v_completer {
16
16
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", "", Scope="Function", Target="*")]
@@ -37,14 +37,14 @@ Function _%v_completer {
37
37
if ($t.get_Length() -eq 0){
38
38
$t = '""'
39
39
}
40
- $elems += $t.replace('` + "`" + `,', ',') # quick fix
40
+ $elems += $t.replace('` + "`" + `,', ',') # quick fix
41
41
}
42
42
43
43
$completions = @(
44
44
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) }
46
46
} 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) }
48
48
}
49
49
)
50
50
@@ -54,6 +54,23 @@ Function _%v_completer {
54
54
55
55
$completions
56
56
}
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 ())
59
76
}
0 commit comments