-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from rsteube/powershell
added powershell
- Loading branch information
Showing
5 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters