diff --git a/README.md b/README.md index 36f63a7..5e72360 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Supported frameworks: - [cobra](https://github.com/spf13/cobra) - [complete](https://github.com/posener/complete) - [fish](https://fishshell.com/) +- [inshellisense](https://github.com/microsoft/inshellisense) - [kingpin](https://github.com/alecthomas/kingpin) - [powershell](https://microsoft.com/powershell) - [urfavecli](https://github.com/urfave/cli) diff --git a/cmd/carapace-bridge/cmd/root.go b/cmd/carapace-bridge/cmd/root.go index 69a4bf3..e62f7b4 100644 --- a/cmd/carapace-bridge/cmd/root.go +++ b/cmd/carapace-bridge/cmd/root.go @@ -47,6 +47,7 @@ func init() { addSubCommand("cobra", "bridges https://github.com/spf13/cobra", bridge.ActionCobra) addSubCommand("complete", "bridges https://github.com/posener/complete", bridge.ActionComplete) 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("powershell", "bridges completions registered in powershell", bridge.ActionPowershell) addSubCommand("urfavecli", "bridges https://github.com/urfave/cli", bridge.ActionUrfavecli) diff --git a/pkg/actions/bridge/inshellisense.go b/pkg/actions/bridge/inshellisense.go new file mode 100644 index 0000000..16a5deb --- /dev/null +++ b/pkg/actions/bridge/inshellisense.go @@ -0,0 +1,51 @@ +package bridge + +import ( + "encoding/json" + "strings" + + "github.com/rsteube/carapace" +) + +// ActionInshellisense bridges https://github.com/microsoft/inshellisense +func ActionInshellisense(command ...string) carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + if len(command) == 0 { + return carapace.ActionMessage("missing argument [ActionInshellisense]") + } + + args := []string{"complete", "--"} + args = append(args, command...) + args = append(args, c.Args...) + args = append(args, c.Value) + return carapace.ActionExecCommand("inshellisense", args...)(func(output []byte) carapace.Action { + var r struct { + Suggestions []struct { + Name string + Description string + } + } + + if err := json.Unmarshal(output, &r); err != nil { + return carapace.ActionMessage(err.Error()) + } + + vals := make([]string, 0) + for _, s := range r.Suggestions { + if !strings.HasPrefix(c.Value, "-") && strings.HasPrefix(s.Name, "-") { + continue + } + + if strings.HasPrefix(s.Name, c.Value) || + (strings.HasPrefix(c.Value, "-") && strings.Contains(c.Value, "=")) { + vals = append(vals, s.Name, s.Description) + } + } + a := carapace.ActionValuesDescribed(vals...) + if strings.HasPrefix(c.Value, "-") && strings.Contains(c.Value, "=") { + a = a.Prefix(strings.SplitAfterN(c.Value, "=", 2)[0]) + } + return a + }) + }) +}