Skip to content

Commit

Permalink
Merge pull request #960 from rsteube/revert-interspersed
Browse files Browse the repository at this point in the history
fix `_carapace` completion
  • Loading branch information
rsteube authored Dec 17, 2023
2 parents 89fdf67 + 4a1834b commit ac69109
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
37 changes: 28 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -47,10 +48,10 @@ func addCompletionCommand(cmd *cobra.Command) {
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
DisableFlagParsing: true,
}
carapaceCmd.Flags().SetInterspersed(false)

cmd.AddCommand(carapaceCmd)
targetCmd.AddCommand(carapaceCmd)

Carapace{carapaceCmd}.PositionalCompletion(
ActionStyledValues(
Expand All @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/shell/spec/snippet.go → internal/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit ac69109

Please sign in to comment.