diff --git a/cmd/carapace-bridge/cmd/root.go b/cmd/carapace-bridge/cmd/root.go index e62f7b4..b3e6e81 100644 --- a/cmd/carapace-bridge/cmd/root.go +++ b/cmd/carapace-bridge/cmd/root.go @@ -49,6 +49,19 @@ func init() { addSubCommand("fish", "bridges completions registered in fish", bridge.ActionFish) addSubCommand("inshellisense", "bridges https://github.com/microsoft/inshellisense", bridge.ActionInshellisense) addSubCommand("kingpin", "bridges https://github.com/alecthomas/kingpin", bridge.ActionKingpin) + addSubCommand("macro", "bridges macros exposed with https://github.com/rsteube/carapace-spec", func(s ...string) carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + switch len(c.Args) { + case 0: + return carapace.ActionExecCommand(s[0], "_carapace", "macro")(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + return carapace.ActionValues(lines[:len(lines)-1]...).MultiParts(".") + }) + default: + return bridge.ActionMacro(s[0], c.Args[0]).Shift(1) + } + }) + }) addSubCommand("powershell", "bridges completions registered in powershell", bridge.ActionPowershell) addSubCommand("urfavecli", "bridges https://github.com/urfave/cli", bridge.ActionUrfavecli) addSubCommand("yargs", "bridges https://github.com/yargs/yargs", bridge.ActionYargs) diff --git a/pkg/actions/bridge/carapace.go b/pkg/actions/bridge/carapace.go index a789809..fc73b83 100644 --- a/pkg/actions/bridge/carapace.go +++ b/pkg/actions/bridge/carapace.go @@ -76,3 +76,19 @@ func actionCommand(command ...string) func(f func(command ...string) carapace.Ac }) } } + +// ActionCarapace bridges macros exposed with https://github.com/rsteube/carapace-bin +func ActionMacro(command ...string) carapace.Action { + return actionCommand(command...)(func(command ...string) carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + args := []string{"_carapace", "macro"} + args = append(args, command[1:]...) + args = append(args, c.Args...) + args = append(args, c.Value) + + return carapace.ActionExecCommand(command[0], args...)(func(output []byte) carapace.Action { + return carapace.ActionImport(output) + }) + }) + }) +}