From 77a19b3f7e20f0efdec4204c6f1161c32242551f Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 17 Dec 2023 16:08:01 +0100 Subject: [PATCH 1/2] Revert "Merge pull request #959 from rsteube/command-interspersed" This reverts commit 89fdf671a5bbe6afeba4b804006bc85ab9bf187f, reversing changes made to f8d4b4730ea8041315207c45e55b63c51968885f. --- command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command.go b/command.go index 860a98e7e..c6c1cc295 100644 --- a/command.go +++ b/command.go @@ -47,8 +47,8 @@ func addCompletionCommand(cmd *cobra.Command) { FParseErrWhitelist: cobra.FParseErrWhitelist{ UnknownFlags: true, }, + DisableFlagParsing: true, } - carapaceCmd.Flags().SetInterspersed(false) cmd.AddCommand(carapaceCmd) From 4a1834b3f3f790958c6744f53c51164e061c82b4 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 17 Dec 2023 21:00:51 +0100 Subject: [PATCH 2/2] fix _carapace completion --- command.go | 35 ++++++++++++++----- internal/shell/shell.go | 2 -- internal/{shell => }/spec/command.go | 0 .../{shell/spec/snippet.go => spec/spec.go} | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) rename internal/{shell => }/spec/command.go (100%) rename internal/{shell/spec/snippet.go => spec/spec.go} (96%) diff --git a/command.go b/command.go index c6c1cc295..4bd1ea8dc 100644 --- a/command.go +++ b/command.go @@ -6,12 +6,13 @@ import ( "os" "strings" + "github.com/rsteube/carapace/internal/spec" "github.com/rsteube/carapace/pkg/style" "github.com/spf13/cobra" ) -func addCompletionCommand(cmd *cobra.Command) { - for _, c := range cmd.Commands() { +func addCompletionCommand(targetCmd *cobra.Command) { + for _, c := range targetCmd.Commands() { if c.Name() == "_carapace" { return } @@ -50,7 +51,7 @@ func addCompletionCommand(cmd *cobra.Command) { DisableFlagParsing: true, } - cmd.AddCommand(carapaceCmd) + targetCmd.AddCommand(carapaceCmd) Carapace{carapaceCmd}.PositionalCompletion( ActionStyledValues( @@ -63,21 +64,39 @@ func addCompletionCommand(cmd *cobra.Command) { "nushell", "#29d866", "oil", "#373a36", "powershell", "#e8a16f", - "spec", style.Default, "tcsh", "#412f09", "xonsh", "#a8ffa9", "zsh", "#efda53", ), - ActionValues(cmd.Root().Name()), + ActionValues(targetCmd.Root().Name()), ) Carapace{carapaceCmd}.PositionalAnyCompletion( ActionCallback(func(c Context) Action { - cmd.RemoveCommand(carapaceCmd) - action, _ := traverse(cmd, append(c.Args[2:], c.Value)) - return action + args := []string{"_carapace", "export", ""} + args = append(args, c.Args[2:]...) + args = append(args, c.Value) + + executable, err := os.Executable() + if err != nil { + return ActionMessage(err.Error()) + } + return ActionExecCommand(executable, args...)(func(output []byte) Action { // TODO does not work with sandbox tests for `example _carapace ...` + if string(output) == "" { + return ActionValues() + } + return ActionImport(output) + }) }), ) + specCmd := &cobra.Command{ + Use: "spec", + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprint(cmd.OutOrStdout(), spec.Spec(targetCmd)) + }, + } + carapaceCmd.AddCommand(specCmd) + styleCmd := &cobra.Command{ Use: "style", Args: cobra.ExactArgs(1), diff --git a/internal/shell/shell.go b/internal/shell/shell.go index f578122f1..2c8b717dc 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -16,7 +16,6 @@ import ( "github.com/rsteube/carapace/internal/shell/nushell" "github.com/rsteube/carapace/internal/shell/oil" "github.com/rsteube/carapace/internal/shell/powershell" - "github.com/rsteube/carapace/internal/shell/spec" "github.com/rsteube/carapace/internal/shell/tcsh" "github.com/rsteube/carapace/internal/shell/xonsh" "github.com/rsteube/carapace/internal/shell/zsh" @@ -40,7 +39,6 @@ func Snippet(cmd *cobra.Command, shell string) (string, error) { "nushell": nushell.Snippet, "oil": oil.Snippet, "powershell": powershell.Snippet, - "spec": spec.Snippet, "tcsh": tcsh.Snippet, "xonsh": xonsh.Snippet, "zsh": zsh.Snippet, diff --git a/internal/shell/spec/command.go b/internal/spec/command.go similarity index 100% rename from internal/shell/spec/command.go rename to internal/spec/command.go diff --git a/internal/shell/spec/snippet.go b/internal/spec/spec.go similarity index 96% rename from internal/shell/spec/snippet.go rename to internal/spec/spec.go index 2d8325eee..25bb037e0 100644 --- a/internal/shell/spec/snippet.go +++ b/internal/spec/spec.go @@ -9,7 +9,7 @@ import ( ) // Snippet generates the spec file. -func Snippet(cmd *cobra.Command) string { +func Spec(cmd *cobra.Command) string { m, _ := yaml.Marshal(command(cmd)) return string(m) }