Skip to content

Commit

Permalink
Merge pull request #91 from rsteube/powershell
Browse files Browse the repository at this point in the history
added powershell
  • Loading branch information
rsteube committed Mar 30, 2023
2 parents 13db767 + b1ee516 commit fcd36c0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Supported frameworks:
- [cobra](https://github.com/spf13/cobra)
- [complete](https://github.com/posener/complete)
- [fish](https://fishshell.com/)
- [powershell](https://microsoft.com/powershell)
- [yargs](https://github.com/yargs/yargs)
- [zsh](https://www.zsh.org/)

Expand Down
5 changes: 3 additions & 2 deletions cmd/carapace-bridge/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func init() {
addSubCommand("click", "bridges https://github.com/pallets/click", bridge.ActionClick)
addSubCommand("cobra", "bridges https://github.com/spf13/cobra", bridge.ActionCobra)
addSubCommand("complete", "bridges https://github.com/posener/complete", bridge.ActionComplete)
addSubCommand("fish", "bridges completions registered in fish shell", bridge.ActionFish)
addSubCommand("fish", "bridges completions registered in fish", bridge.ActionFish)
addSubCommand("powershell", "bridges completions registered in powershell", bridge.ActionPowershell)
addSubCommand("yargs", "bridges https://github.com/yargs/yargs", bridge.ActionYargs)
addSubCommand("zsh", "bridges completions registered in zsh shell", bridge.ActionZsh)
addSubCommand("zsh", "bridges completions registered in zsh", bridge.ActionZsh)
}

func addSubCommand(use, short string, f func(s ...string) carapace.Action) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/bridge/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func compline(args ...string) string {
return fmt.Sprintf("%#v", strings.Join(args, " "))
}

// ActionFish bridges completions registered in fish shell
// ActionFish bridges completions registered in fish
// (uses custom `config.fish` in “~/.config/carapace/bridge/fish`)
func ActionFish(command ...string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
75 changes: 75 additions & 0 deletions pkg/actions/bridge/powershell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package bridge

import (
"encoding/json"
"fmt"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)

// ActionPowershell bridges completions registered in powershell
// TODO (uses custom `config.fish` in “~/.config/carapace/bridge/fish`)
func ActionPowershell(command ...string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(command) == 0 {
return carapace.ActionMessage("missing argument [ActionPowershell]")
}

configDir, err := xdg.UserConfigDir()
if err != nil {
return carapace.ActionMessage(err.Error())
}

args := append(command, c.Args...)
args = append(args, c.CallbackValue)

// for index, arg := range args {
// TODO handle different escape character and escapcing in general
// args[index] = strings.Replace(arg, " ", "` ", -1)
// }

line := strings.Join(args, " ")
snippet := fmt.Sprintf(`[System.Management.Automation.CommandCompletion]::CompleteInput("%v", %v, $null).CompletionMatches | ConvertTo-Json `, line, len(line))

c.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%v/carapace/bridge", configDir))
return carapace.ActionExecCommand("pwsh", "-Command", snippet)(func(output []byte) carapace.Action {
if len(output) == 0 {
return carapace.ActionValues()
}

type singleResult struct {
CompletionText string `json:"CompletionText"`
ListItemText string `json:"ListItemText"`
ResultType int `json:"ResultType"`
ToolTip string `json:"ToolTip"`
}
var result []singleResult

if err := json.Unmarshal(output, &result); err != nil {
result = make([]singleResult, 1)
if err := json.Unmarshal(output, &result[0]); err != nil {
carapace.LOG.Println(string(output))
return carapace.ActionMessage(err.Error())
}
}

suffixes := make([]rune, 0)
vals := make([]string, 0)
for _, r := range result {
if _runes := []rune(r.CompletionText); len(_runes) > 2 && strings.HasSuffix(r.CompletionText, " ") {
suffixes = append(suffixes, _runes[len(_runes)-1])
}
r.CompletionText = strings.TrimSuffix(r.CompletionText, " ")

if r.CompletionText == r.ToolTip {
r.ToolTip = ""
}
vals = append(vals, r.CompletionText, r.ToolTip)
}
return carapace.ActionValuesDescribed(vals...).NoSpace(suffixes...).StyleF(style.ForPath)
}).Invoke(c).ToA()
})
}
2 changes: 1 addition & 1 deletion pkg/actions/bridge/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/rsteube/carapace/pkg/xdg"
)

// ActionZsh bridges completions registered in zsh shell
// ActionZsh bridges completions registered in zsh
// (uses custom `.zshrc` in “~/.config/carapace/bridge/zsh`)
func ActionZsh(command ...string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down

0 comments on commit fcd36c0

Please sign in to comment.