Skip to content

Commit

Permalink
bash: subcommands now completed with ActionValues instead of callback
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Mar 27, 2020
1 parent ef314de commit 8951a0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
18 changes: 17 additions & 1 deletion bash/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,24 @@ func snippetFunctions(cmd *cobra.Command, actions map[string]string) string {
}
})

var positionalAction string
if cmd.HasSubCommands() {
subcommands := make([]string, 0)
for _, c := range cmd.Commands() {
if !c.Hidden {
subcommands = append(subcommands, c.Name())
for _, alias := range c.Aliases {
subcommands = append(subcommands, alias)
}
}
}
positionalAction = ActionValues(subcommands...)
} else {
positionalAction = Callback(cmd.Root().Name(), "_")
}

result := make([]string, 0)
result = append(result, fmt.Sprintf(function_pattern, uid.Command(cmd), snippetFlagList(cmd.LocalFlags()), strings.Join(flags, "\n"), Callback(cmd.Root().Name(), "_")))
result = append(result, fmt.Sprintf(function_pattern, uid.Command(cmd), snippetFlagList(cmd.LocalFlags()), strings.Join(flags, "\n"), positionalAction))
for _, subcmd := range cmd.Commands() {
if !subcmd.Hidden {
result = append(result, snippetFunctions(subcmd, actions))
Expand Down
31 changes: 17 additions & 14 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ func (c Carapace) Zsh() string {
return zsh.Snippet(c.cmd.Root(), actions)
}

func (c Carapace) Snippet(shell string) string {
switch shell {
case "bash":
return c.Bash()
case "fish":
return c.Fish()
case "zsh":
return c.Zsh()
default:
return fmt.Sprintf("expected 'bash', 'fish' or 'zsh' [was: %v]", shell)
}
}

var completions = Completions{
actions: make(map[string]Action),
}
Expand Down Expand Up @@ -118,20 +131,6 @@ func addCompletionCommand(cmd *cobra.Command) {
}
}
if _, ok := completions.actions[callback]; !ok {
if targetCmd.HasSubCommands() && len(targetArgs) <= 1 {
if args[0] == "bash" { // TODO print for other shells as well?
subcommands := make([]string, 0)
for _, c := range targetCmd.Commands() {
if !c.Hidden {
subcommands = append(subcommands, c.Name())
for _, alias := range c.Aliases {
subcommands = append(subcommands, alias)
}
}
}
fmt.Println(ActionValues(subcommands...).Bash)
}
}
os.Exit(0) // ensure no message for missing action on positional completion // TODO this was only for bash, maybe enable for other shells?
}
} else if callback == "state" {
Expand Down Expand Up @@ -162,3 +161,7 @@ func traverse(cmd *cobra.Command, args []string) (*cobra.Command, []string) {
targetCmd.ParseFlags(targetArgs)
return targetCmd, targetCmd.Flags().Args() // TODO check length
}

func IsCallback() bool {
return len(os.Args) > 3 && os.Args[1] == "_carapace" && os.Args[3] != "state"
}
2 changes: 1 addition & 1 deletion example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _example_completions() {
*)
COMPREPLY=($(eval $(_example_callback '_')))
COMPREPLY=($(compgen -W "action alias callback condition" -- $last))
;;
esac
fi
Expand Down

0 comments on commit 8951a0b

Please sign in to comment.