Skip to content

Commit

Permalink
Merge pull request #1496 from rsteube/fix-yargs
Browse files Browse the repository at this point in the history
fix yargs
  • Loading branch information
rsteube authored Jan 26, 2023
2 parents a44b315 + 068eff1 commit 9111192
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions docs/src/spec/bride.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ completion:
positionalany: ["$_bridge.Posener(vault)"]
````
## Yargs
[yargs/yargs] based commands can be bridged with the [`bridge.Yargs`] macro:

```yaml
name: example
description: example description
completion:
positionalany: ["$_bridge.Yargs(example)"]
````
[lazycomplete]:https://github.com/rsteube/lazycomplete
[shell startup delay]:https://jzelinskie.com/posts/dont-recommend-sourcing-shell-completion/
Expand All @@ -109,3 +118,6 @@ completion:

[posener/complete]:https://github.com/posener/complete
[`bridge.Posener`]:https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/actions/bridge#ActionPosener

[yargs/yargs]:https://github.com/yargs/yargs
[`bridge.Yargs`]:https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/actions/bridge#ActionYargs
14 changes: 7 additions & 7 deletions pkg/actions/bridge/yargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ func ActionYargs(command string) carapace.Action {
return carapace.ActionMessage(err.Error())
}

args := c.Args
current := c.CallbackValue
c.Setenv("SHELL", "zsh")

compLine := strings.Join(append(args, current), " ") // TODO escape/quote special characters
return carapace.ActionExecCommand(command, "--get-yargs-completions", compLine)(func(output []byte) carapace.Action {
args := []string{"--get-yargs-completions"}
args = append(args, c.Args...)
args = append(args, c.CallbackValue)
return carapace.ActionExecCommand(command, args...)(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)
name := splitted[0]
description := ""
if len(splitted) > 1 {
description = strings.Replace(splitted[1], "\001", `\:`, 1)
description = splitted[1]
}
vals = append(vals, name, description)
}
Expand Down

0 comments on commit 9111192

Please sign in to comment.