Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added yargs bridge #1495

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions completers/git_completer/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
carapace.Gen(diffCmd).Standalone()

addDiffFlags(diffCmd)
rootCmd.AddCommand(diffCmd)

carapace.Gen(diffCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/bridge/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// carapace.Gen(rootCmd).Standalone()
//
// carapace.Gen(rootCmd).PositionalAnyCompletion(
// argcomplete.ActionClick("watson"),
// bridge.ActionClick("watson"),
// )
// }
func ActionClick(command string) carapace.Action {
Expand Down
61 changes: 61 additions & 0 deletions pkg/actions/bridge/yargs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package bridge

import (
"os/exec"
"strings"

"github.com/rsteube/carapace"
)

// ActionYargs bridges https://github.com/yargs/yargs
//
// var rootCmd = &cobra.Command{
// Use: "example",
// Run: func(cmd *cobra.Command, args []string) {},
// DisableFlagParsing: true,
// }
//
// func Execute() error {
// return rootCmd.Execute()
// }
//
// func init() {
// carapace.Gen(rootCmd).Standalone()
//
// carapace.Gen(rootCmd).PositionalAnyCompletion(
// bridge.ActionYargs("example"),
// )
// }
func ActionYargs(command string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if _, err := exec.LookPath(command); err != nil {
return carapace.ActionMessage(err.Error())
}

args := c.Args
current := c.CallbackValue

compLine := strings.Join(append(args, current), " ") // TODO escape/quote special characters
return carapace.ActionExecCommand(command, "--get-yargs-completions", compLine)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for _, line := range lines {
if line != "" {
line = strings.Replace(line, `\:`, "\001", 1)
splitted := strings.SplitN(line, ":", 2)
name := strings.Replace(splitted[0], "\001", `\:`, 1)
description := ""
if len(splitted) > 1 {
description = strings.Replace(splitted[1], "\001", `\:`, 1)
}
vals = append(vals, name, description)
}
}
if len(vals) == 0 {
return carapace.ActionFiles()
}
return carapace.ActionValuesDescribed(vals...)
}).Invoke(c).ToA()
})
}