Skip to content

Commit

Permalink
completion: lots of small ironing
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed May 1, 2022
1 parent b9991d8 commit beffeaa
Showing 1 changed file with 42 additions and 52 deletions.
94 changes: 42 additions & 52 deletions commands/helper_completion.go
@@ -1,6 +1,7 @@
package commands

import (
"fmt"
"sort"
"strings"

Expand All @@ -21,9 +22,6 @@ func completionHandlerError(err error) (completions []string, directives cobra.S

func completeBridge(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -41,15 +39,12 @@ func completeBridge(env *Env) validArgsFunction {
completions[i] = bridge + "\t" + "Bridge"
}

return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

func completeBridgeAuth(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -74,46 +69,43 @@ func completeBridgeAuth(env *Env) validArgsFunction {
completions[i] = cred.ID().Human() + "\t" + cred.Target() + " " + string(cred.Kind()) + " " + metaFmt
}

return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

func completeBug(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
defer func() {
_ = env.backend.Close()
}()

allIds := env.backend.AllBugsIds()
bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
for i, id := range allIds {
var err error
bugExcerpt[i], err = env.backend.ResolveBugExcerpt(id)
if err != nil {
return completionHandlerError(err)
}
}
return completeBugWithBackend(env.backend)
}
}

completions = make([]string, len(allIds))
for i, id := range allIds {
completions[i] = id.Human() + "\t" + bugExcerpt[i].Title
func completeBugWithBackend(backend *cache.RepoCache) (completions []string, directives cobra.ShellCompDirective) {
allIds := backend.AllBugsIds()
bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
for i, id := range allIds {
var err error
bugExcerpt[i], err = backend.ResolveBugExcerpt(id)
if err != nil {
return completionHandlerError(err)
}
return completions, cobra.ShellCompDirectiveDefault
}

completions = make([]string, len(allIds))
for i, id := range allIds {
completions[i] = id.Human() + "\t" + bugExcerpt[i].Title
}
return completions, cobra.ShellCompDirectiveNoFileComp
}

func completeBugAndLabels(env *Env, addOrRemove bool) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) == 0 {
return completeBug(env)(cmd, args, toComplete)
}

if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -122,6 +114,10 @@ func completeBugAndLabels(env *Env, addOrRemove bool) validArgsFunction {
}()

b, args, err := _select.ResolveBug(env.backend, args)
if err == _select.ErrNoValidId {
// we need a bug first to complete labels
return completeBugWithBackend(env.backend)
}
if err != nil {
return completionHandlerError(err)
}
Expand Down Expand Up @@ -160,21 +156,18 @@ func completeBugAndLabels(env *Env, addOrRemove bool) validArgsFunction {
completions[i] = string(label) + "\t" + "Label"
}

return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

func completeFrom(choices []string) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return choices, cobra.ShellCompDirectiveDefault
return choices, cobra.ShellCompDirectiveNoFileComp
}
}

func completeGitRemote(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -191,15 +184,12 @@ func completeGitRemote(env *Env) validArgsFunction {
completions = append(completions, remote+"\t"+"Remote: "+url)
}
sort.Strings(completions)
return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

func completeLabel(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -210,9 +200,13 @@ func completeLabel(env *Env) validArgsFunction {
labels := env.backend.ValidLabels()
completions = make([]string, len(labels))
for i, label := range labels {
completions[i] = string(label) + "\t" + "Label"
if strings.Contains(label.String(), " ") {
completions[i] = fmt.Sprintf("\"%s\"\tLabel", label.String())
} else {
completions[i] = fmt.Sprintf("%s\tLabel", label.String())
}
}
return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

Expand Down Expand Up @@ -262,7 +256,7 @@ func completeLs(env *Env) validArgsFunction {
}
completions[i] = key + handle + "\t" + user.DisplayName()
}
return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}

for _, key := range byLabel {
Expand All @@ -272,9 +266,13 @@ func completeLs(env *Env) validArgsFunction {
labels := env.backend.ValidLabels()
completions = make([]string, len(labels))
for i, label := range labels {
completions[i] = key + string(label)
if strings.Contains(label.String(), " ") {
completions[i] = key + "\"" + string(label) + "\""
} else {
completions[i] = key + string(label)
}
}
return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}

completions = []string{
Expand All @@ -292,10 +290,6 @@ func completeLs(env *Env) validArgsFunction {

func completeUser(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -312,16 +306,12 @@ func completeUser(env *Env) validArgsFunction {
}
completions[i] = user.Id.Human() + "\t" + user.DisplayName()
}
return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

func completeUserForQuery(env *Env) validArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

if err := loadBackend(env)(cmd, args); err != nil {
return completionHandlerError(err)
}
Expand All @@ -345,6 +335,6 @@ func completeUserForQuery(env *Env) validArgsFunction {
}
completions[i] = handle + "\t" + user.DisplayName()
}
return completions, cobra.ShellCompDirectiveDefault
return completions, cobra.ShellCompDirectiveNoFileComp
}
}

0 comments on commit beffeaa

Please sign in to comment.