Skip to content

Commit

Permalink
Merge pull request #1495 from rsteube/bridge-yargs
Browse files Browse the repository at this point in the history
added yargs bridge
  • Loading branch information
rsteube authored Jan 26, 2023
2 parents 608d04b + c16000a commit a44b315
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
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()
})
}

0 comments on commit a44b315

Please sign in to comment.