Skip to content

Commit

Permalink
Complete bug IDs where appropriate
Browse files Browse the repository at this point in the history
This completes bug IDs in bash and fish.  I haven't gotten this to work in zsh.
In fish, the bug titles are shown as completion label, and can be searched for.
I don't know if/how other shells can show the completion label.
Maybe they don't use "\t" as separator but it seems to do no harm.

The interface does not yet seem ideal because sometimes subcommands and
bug IDs are both valid. Since there can be lots of bug IDs, they overshadow
the subcommands.
I wonder if instead of

	git bug status open 0123acd

we should use

	git bug status --open 0123acd

or if there's some other way to avoid ambiguity.

If that doesn't work out, fish has a -k/--keep-order flag, so subcommand
completions can be listed before bug IDs, but that's neither portable nor
a pretty solution.

Part of #493
  • Loading branch information
krobelus committed Jan 24, 2021
1 parent 078c85b commit 7fa0ce0
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions commands/comment.go
Expand Up @@ -19,6 +19,7 @@ func newCommentCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runComment(env, args)
},
ValidArgsFunction: completeBugs(env),
}

cmd.AddCommand(newCommentAddCommand())
Expand Down
54 changes: 54 additions & 0 deletions commands/helper_completion.go
@@ -0,0 +1,54 @@
package commands

import (
"log"

"github.com/spf13/cobra"

"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/query"
)

type validArgsFunction func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

func completeBugs(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Currently, the commands accept at most one bug ID.
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

if err := loadBackend(env)(cmd, args); err != nil {
log.Println(err)
return nil, cobra.ShellCompDirectiveError
}
defer func() {
err := closeBackend(env)(cmd, args)
if err != nil {
log.Println(err)
}
}()

q := query.NewQuery()
allIds, err := env.backend.QueryBugs(q)
if err != nil {
log.Println(err)
return nil, cobra.ShellCompDirectiveError
}

bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
for i, id := range allIds {
bugExcerpt[i], err = env.backend.ResolveBugExcerpt(id)
if err != nil {
log.Println(err)
return nil, cobra.ShellCompDirectiveError
}
}

completions := make([]string, len(allIds))
for i, id := range allIds {
completions[i] = id.Human() + "\t" + bugExcerpt[i].Title
}
return completions, cobra.ShellCompDirectiveDefault
}
}
1 change: 1 addition & 0 deletions commands/label.go
Expand Up @@ -17,6 +17,7 @@ func newLabelCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runLabel(env, args)
},
ValidArgsFunction: completeBugs(env),
}

cmd.AddCommand(newLabelAddCommand())
Expand Down
1 change: 1 addition & 0 deletions commands/rm.go
Expand Up @@ -18,6 +18,7 @@ func newRmCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runRm(env, args)
},
ValidArgsFunction: completeBugs(env),
}

flags := cmd.Flags()
Expand Down
1 change: 1 addition & 0 deletions commands/select.go
Expand Up @@ -32,6 +32,7 @@ The complementary command is "git bug deselect" performing the opposite operatio
RunE: func(cmd *cobra.Command, args []string) error {
return runSelect(env, args)
},
ValidArgsFunction: completeBugs(env),
}

return cmd
Expand Down
1 change: 1 addition & 0 deletions commands/show.go
Expand Up @@ -30,6 +30,7 @@ func newShowCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runShow(env, options, args)
},
ValidArgsFunction: completeBugs(env),
}

flags := cmd.Flags()
Expand Down
1 change: 1 addition & 0 deletions commands/status.go
Expand Up @@ -16,6 +16,7 @@ func newStatusCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runStatus(env, args)
},
ValidArgsFunction: completeBugs(env),
}

cmd.AddCommand(newStatusCloseCommand())
Expand Down
1 change: 1 addition & 0 deletions commands/title.go
Expand Up @@ -16,6 +16,7 @@ func newTitleCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runTitle(env, args)
},
ValidArgsFunction: completeBugs(env),
}

cmd.AddCommand(newTitleEditCommand())
Expand Down
7 changes: 7 additions & 0 deletions misc/bash_completion/git-bug
Expand Up @@ -740,6 +740,7 @@ _git-bug_comment()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand Down Expand Up @@ -822,6 +823,7 @@ _git-bug_label()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand Down Expand Up @@ -1002,6 +1004,7 @@ _git-bug_rm()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand All @@ -1022,6 +1025,7 @@ _git-bug_select()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand Down Expand Up @@ -1052,6 +1056,7 @@ _git-bug_show()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand Down Expand Up @@ -1114,6 +1119,7 @@ _git-bug_status()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand Down Expand Up @@ -1181,6 +1187,7 @@ _git-bug_title()

must_have_one_flag=()
must_have_one_noun=()
has_completion_function=1
noun_aliases=()
}

Expand Down

0 comments on commit 7fa0ce0

Please sign in to comment.