1package commands
2
3import (
4 "fmt"
5 "strings"
6
7 text "github.com/MichaelMure/go-term-text"
8 "github.com/spf13/cobra"
9
10 "github.com/MichaelMure/git-bug/bug"
11 "github.com/MichaelMure/git-bug/cache"
12 "github.com/MichaelMure/git-bug/query"
13 "github.com/MichaelMure/git-bug/util/colors"
14 "github.com/MichaelMure/git-bug/util/interrupt"
15)
16
17var (
18 lsQuery query.Query
19
20 lsStatusQuery []string
21 lsNoQuery []string
22 lsSortBy string
23 lsSortDirection string
24)
25
26func runLsBug(cmd *cobra.Command, args []string) error {
27 backend, err := cache.NewRepoCache(repo)
28 if err != nil {
29 return err
30 }
31 defer backend.Close()
32 interrupt.RegisterCleaner(backend.Close)
33
34 var q *query.Query
35 if len(args) >= 1 {
36 q, err = query.Parse(strings.Join(args, " "))
37
38 if err != nil {
39 return err
40 }
41 } else {
42 err = completeQuery()
43 if err != nil {
44 return err
45 }
46 q = &lsQuery
47 }
48
49 allIds := backend.QueryBugs(q)
50
51 for _, id := range allIds {
52 b, err := backend.ResolveBugExcerpt(id)
53 if err != nil {
54 return err
55 }
56
57 var name string
58 if b.AuthorId != "" {
59 author, err := backend.ResolveIdentityExcerpt(b.AuthorId)
60 if err != nil {
61 name = "<missing author data>"
62 } else {
63 name = author.DisplayName()
64 }
65 } else {
66 name = b.LegacyAuthor.DisplayName()
67 }
68
69 var labelsTxt strings.Builder
70 for _, l := range b.Labels {
71 lc256 := l.Color().Term256()
72 labelsTxt.WriteString(lc256.Escape())
73 labelsTxt.WriteString(" ◼")
74 labelsTxt.WriteString(lc256.Unescape())
75 }
76
77 // truncate + pad if needed
78 labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
79 titleFmt := text.LeftPadMaxLine(b.Title, 50-text.Len(labelsFmt), 0)
80 authorFmt := text.LeftPadMaxLine(name, 15, 0)
81
82 comments := fmt.Sprintf("%4d 💬", b.LenComments)
83 if b.LenComments > 9999 {
84 comments = " ∞ 💬"
85 }
86
87 fmt.Printf("%s %s\t%s\t%s\t%s\n",
88 colors.Cyan(b.Id.Human()),
89 colors.Yellow(b.Status),
90 titleFmt+labelsFmt,
91 colors.Magenta(authorFmt),
92 comments,
93 )
94 }
95
96 return nil
97}
98
99// Finish the command flags transformation into the query.Query
100func completeQuery() error {
101 for _, str := range lsStatusQuery {
102 status, err := bug.StatusFromString(str)
103 if err != nil {
104 return err
105 }
106 lsQuery.Status = append(lsQuery.Status, status)
107 }
108
109 for _, no := range lsNoQuery {
110 switch no {
111 case "label":
112 lsQuery.NoLabel = true
113 default:
114 return fmt.Errorf("unknown \"no\" filter %s", no)
115 }
116 }
117
118 switch lsSortBy {
119 case "id":
120 lsQuery.OrderBy = query.OrderById
121 case "creation":
122 lsQuery.OrderBy = query.OrderByCreation
123 case "edit":
124 lsQuery.OrderBy = query.OrderByEdit
125 default:
126 return fmt.Errorf("unknown sort flag %s", lsSortBy)
127 }
128
129 switch lsSortDirection {
130 case "asc":
131 lsQuery.OrderDirection = query.OrderAscending
132 case "desc":
133 lsQuery.OrderDirection = query.OrderDescending
134 default:
135 return fmt.Errorf("unknown sort direction %s", lsSortDirection)
136 }
137
138 return nil
139}
140
141var lsCmd = &cobra.Command{
142 Use: "ls [<query>]",
143 Short: "List bugs.",
144 Long: `Display a summary of each bugs.
145
146You can pass an additional query to filter and order the list. This query can be expressed either with a simple query language or with flags.`,
147 Example: `List open bugs sorted by last edition with a query:
148git bug ls status:open sort:edit-desc
149
150List closed bugs sorted by creation with flags:
151git bug ls --status closed --by creation
152`,
153 PreRunE: loadRepo,
154 RunE: runLsBug,
155}
156
157func init() {
158 RootCmd.AddCommand(lsCmd)
159
160 lsCmd.Flags().SortFlags = false
161
162 lsCmd.Flags().StringSliceVarP(&lsStatusQuery, "status", "s", nil,
163 "Filter by status. Valid values are [open,closed]")
164 lsCmd.Flags().StringSliceVarP(&lsQuery.Author, "author", "a", nil,
165 "Filter by author")
166 lsCmd.Flags().StringSliceVarP(&lsQuery.Participant, "participant", "p", nil,
167 "Filter by participant")
168 lsCmd.Flags().StringSliceVarP(&lsQuery.Actor, "actor", "A", nil,
169 "Filter by actor")
170 lsCmd.Flags().StringSliceVarP(&lsQuery.Label, "label", "l", nil,
171 "Filter by label")
172 lsCmd.Flags().StringSliceVarP(&lsQuery.Title, "title", "t", nil,
173 "Filter by title")
174 lsCmd.Flags().StringSliceVarP(&lsNoQuery, "no", "n", nil,
175 "Filter by absence of something. Valid values are [label]")
176 lsCmd.Flags().StringVarP(&lsSortBy, "by", "b", "creation",
177 "Sort the results by a characteristic. Valid values are [id,creation,edit]")
178 lsCmd.Flags().StringVarP(&lsSortDirection, "direction", "d", "asc",
179 "Select the sorting direction. Valid values are [asc,desc]")
180}