1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package bugcmd
import (
"strings"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/commands/completion"
"github.com/MichaelMure/git-bug/commands/execenv"
_select "github.com/MichaelMure/git-bug/commands/select"
"github.com/MichaelMure/git-bug/entities/bug"
)
// BugCompletion complete a bug id
func BugCompletion(env *execenv.Env) completion.ValidArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if err := execenv.LoadBackend(env)(cmd, args); err != nil {
return completion.HandleError(err)
}
defer func() {
_ = env.Backend.Close()
}()
return bugWithBackend(env.Backend, toComplete)
}
}
func bugWithBackend(backend *cache.RepoCache, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
for _, id := range backend.Bugs().AllIds() {
if strings.Contains(id.String(), strings.TrimSpace(toComplete)) {
excerpt, err := backend.Bugs().ResolveExcerpt(id)
if err != nil {
return completion.HandleError(err)
}
completions = append(completions, id.Human()+"\t"+excerpt.Title)
}
}
return completions, cobra.ShellCompDirectiveNoFileComp
}
// BugAndLabelsCompletion complete either a bug ID or a label if we know about the bug
func BugAndLabelsCompletion(env *execenv.Env, addOrRemove bool) completion.ValidArgsFunction {
return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
if err := execenv.LoadBackend(env)(cmd, args); err != nil {
return completion.HandleError(err)
}
defer func() {
_ = env.Backend.Close()
}()
b, args, err := ResolveSelected(env.Backend, args)
if _select.IsErrNoValidId(err) {
// we need a bug first to complete labels
return bugWithBackend(env.Backend, toComplete)
}
if err != nil {
return completion.HandleError(err)
}
snap := b.Snapshot()
seenLabels := map[bug.Label]bool{}
for _, label := range args {
seenLabels[bug.Label(label)] = addOrRemove
}
var labels []bug.Label
if addOrRemove {
for _, label := range snap.Labels {
seenLabels[label] = true
}
allLabels := env.Backend.Bugs().ValidLabels()
labels = make([]bug.Label, 0, len(allLabels))
for _, label := range allLabels {
if !seenLabels[label] {
labels = append(labels, label)
}
}
} else {
labels = make([]bug.Label, 0, len(snap.Labels))
for _, label := range snap.Labels {
if seenLabels[label] {
labels = append(labels, label)
}
}
}
completions = make([]string, len(labels))
for i, label := range labels {
completions[i] = string(label) + "\t" + "Label"
}
return completions, cobra.ShellCompDirectiveNoFileComp
}
}
|