1package termui
2
3import (
4 "bytes"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/bug"
8 "github.com/MichaelMure/git-bug/cache"
9 "github.com/MichaelMure/git-bug/util/colors"
10 "github.com/MichaelMure/git-bug/util/text"
11 "github.com/dustin/go-humanize"
12 "github.com/jesseduffield/gocui"
13)
14
15const bugTableView = "bugTableView"
16const bugTableHeaderView = "bugTableHeaderView"
17const bugTableFooterView = "bugTableFooterView"
18const bugTableInstructionView = "bugTableInstructionView"
19
20const defaultRemote = "origin"
21const defaultQuery = "status:open"
22
23type bugTable struct {
24 repo *cache.RepoCache
25 queryStr string
26 query *cache.Query
27 allIds []string
28 bugs []*cache.BugCache
29 pageCursor int
30 selectCursor int
31}
32
33func newBugTable(c *cache.RepoCache) *bugTable {
34 query, err := cache.ParseQuery(defaultQuery)
35 if err != nil {
36 panic(err)
37 }
38
39 return &bugTable{
40 repo: c,
41 query: query,
42 queryStr: defaultQuery,
43 pageCursor: 0,
44 selectCursor: 0,
45 }
46}
47
48func (bt *bugTable) layout(g *gocui.Gui) error {
49 maxX, maxY := g.Size()
50
51 if maxY < 4 {
52 // window too small !
53 return nil
54 }
55
56 v, err := g.SetView(bugTableHeaderView, -1, -1, maxX, 3, 0)
57
58 if err != nil {
59 if err != gocui.ErrUnknownView {
60 return err
61 }
62
63 v.Frame = false
64 }
65
66 v.Clear()
67 bt.renderHeader(v, maxX)
68
69 v, err = g.SetView(bugTableView, -1, 1, maxX, maxY-3, 0)
70
71 if err != nil {
72 if err != gocui.ErrUnknownView {
73 return err
74 }
75
76 v.Frame = false
77 v.Highlight = true
78 v.SelBgColor = gocui.ColorWhite
79 v.SelFgColor = gocui.ColorBlack
80
81 // restore the cursor
82 // window is too small to set the cursor properly, ignoring the error
83 _ = v.SetCursor(0, bt.selectCursor)
84 }
85
86 _, viewHeight := v.Size()
87 err = bt.paginate(viewHeight)
88 if err != nil {
89 return err
90 }
91
92 err = bt.cursorClamp(v)
93 if err != nil {
94 return err
95 }
96
97 v.Clear()
98 bt.render(v, maxX)
99
100 v, err = g.SetView(bugTableFooterView, -1, maxY-4, maxX, maxY, 0)
101
102 if err != nil {
103 if err != gocui.ErrUnknownView {
104 return err
105 }
106
107 v.Frame = false
108 }
109
110 v.Clear()
111 bt.renderFooter(v, maxX)
112
113 v, err = g.SetView(bugTableInstructionView, -1, maxY-2, maxX, maxY, 0)
114
115 if err != nil {
116 if err != gocui.ErrUnknownView {
117 return err
118 }
119
120 v.Frame = false
121 v.BgColor = gocui.ColorBlue
122
123 _, _ = fmt.Fprintf(v, "[q] Quit [s] Search [←↓↑→,hjkl] Navigation [↵] Open bug [n] New bug [i] Pull [o] Push")
124 }
125
126 _, err = g.SetCurrentView(bugTableView)
127 return err
128}
129
130func (bt *bugTable) keybindings(g *gocui.Gui) error {
131 // Quit
132 if err := g.SetKeybinding(bugTableView, 'q', gocui.ModNone, quit); err != nil {
133 return err
134 }
135
136 // Down
137 if err := g.SetKeybinding(bugTableView, 'j', gocui.ModNone,
138 bt.cursorDown); err != nil {
139 return err
140 }
141 if err := g.SetKeybinding(bugTableView, gocui.KeyArrowDown, gocui.ModNone,
142 bt.cursorDown); err != nil {
143 return err
144 }
145 // Up
146 if err := g.SetKeybinding(bugTableView, 'k', gocui.ModNone,
147 bt.cursorUp); err != nil {
148 return err
149 }
150 if err := g.SetKeybinding(bugTableView, gocui.KeyArrowUp, gocui.ModNone,
151 bt.cursorUp); err != nil {
152 return err
153 }
154
155 // Previous page
156 if err := g.SetKeybinding(bugTableView, 'h', gocui.ModNone,
157 bt.previousPage); err != nil {
158 return err
159 }
160 if err := g.SetKeybinding(bugTableView, gocui.KeyArrowLeft, gocui.ModNone,
161 bt.previousPage); err != nil {
162 return err
163 }
164 if err := g.SetKeybinding(bugTableView, gocui.KeyPgup, gocui.ModNone,
165 bt.previousPage); err != nil {
166 return err
167 }
168 // Next page
169 if err := g.SetKeybinding(bugTableView, 'l', gocui.ModNone,
170 bt.nextPage); err != nil {
171 return err
172 }
173 if err := g.SetKeybinding(bugTableView, gocui.KeyArrowRight, gocui.ModNone,
174 bt.nextPage); err != nil {
175 return err
176 }
177 if err := g.SetKeybinding(bugTableView, gocui.KeyPgdn, gocui.ModNone,
178 bt.nextPage); err != nil {
179 return err
180 }
181
182 // New bug
183 if err := g.SetKeybinding(bugTableView, 'n', gocui.ModNone,
184 bt.newBug); err != nil {
185 return err
186 }
187
188 // Open bug
189 if err := g.SetKeybinding(bugTableView, gocui.KeyEnter, gocui.ModNone,
190 bt.openBug); err != nil {
191 return err
192 }
193
194 // Pull
195 if err := g.SetKeybinding(bugTableView, 'i', gocui.ModNone,
196 bt.pull); err != nil {
197 return err
198 }
199
200 // Push
201 if err := g.SetKeybinding(bugTableView, 'o', gocui.ModNone,
202 bt.push); err != nil {
203 return err
204 }
205
206 // Query
207 if err := g.SetKeybinding(bugTableView, 's', gocui.ModNone,
208 bt.changeQuery); err != nil {
209 return err
210 }
211
212 return nil
213}
214
215func (bt *bugTable) disable(g *gocui.Gui) error {
216 if err := g.DeleteView(bugTableView); err != nil && err != gocui.ErrUnknownView {
217 return err
218 }
219 if err := g.DeleteView(bugTableHeaderView); err != nil && err != gocui.ErrUnknownView {
220 return err
221 }
222 if err := g.DeleteView(bugTableFooterView); err != nil && err != gocui.ErrUnknownView {
223 return err
224 }
225 if err := g.DeleteView(bugTableInstructionView); err != nil && err != gocui.ErrUnknownView {
226 return err
227 }
228 return nil
229}
230
231func (bt *bugTable) paginate(max int) error {
232 bt.allIds = bt.repo.QueryBugs(bt.query)
233
234 return bt.doPaginate(max)
235}
236
237func (bt *bugTable) doPaginate(max int) error {
238 // clamp the cursor
239 bt.pageCursor = maxInt(bt.pageCursor, 0)
240 bt.pageCursor = minInt(bt.pageCursor, len(bt.allIds))
241
242 nb := minInt(len(bt.allIds)-bt.pageCursor, max)
243
244 if nb < 0 {
245 bt.bugs = []*cache.BugCache{}
246 return nil
247 }
248
249 // slice the data
250 ids := bt.allIds[bt.pageCursor : bt.pageCursor+nb]
251
252 bt.bugs = make([]*cache.BugCache, len(ids))
253
254 for i, id := range ids {
255 b, err := bt.repo.ResolveBug(id)
256 if err != nil {
257 return err
258 }
259
260 bt.bugs[i] = b
261 }
262
263 return nil
264}
265
266func (bt *bugTable) getTableLength() int {
267 return len(bt.bugs)
268}
269
270func (bt *bugTable) getColumnWidths(maxX int) map[string]int {
271 m := make(map[string]int)
272 m["id"] = 9
273 m["status"] = 7
274
275 left := maxX - 5 - m["id"] - m["status"]
276
277 m["summary"] = 10
278 left -= m["summary"]
279 m["lastEdit"] = 19
280 left -= m["lastEdit"]
281
282 m["author"] = minInt(maxInt(left/3, 15), 10+left/8)
283 m["title"] = maxInt(left-m["author"], 10)
284
285 return m
286}
287
288func (bt *bugTable) render(v *gocui.View, maxX int) {
289 columnWidths := bt.getColumnWidths(maxX)
290
291 for _, b := range bt.bugs {
292 person := bug.Person{}
293 snap := b.Snapshot()
294 if len(snap.Comments) > 0 {
295 create := snap.Comments[0]
296 person = create.Author
297 }
298
299 summaryTxt := fmt.Sprintf("C:%-2d L:%-2d",
300 len(snap.Comments)-1,
301 len(snap.Labels),
302 )
303
304 id := text.LeftPadMaxLine(snap.HumanId(), columnWidths["id"], 1)
305 status := text.LeftPadMaxLine(snap.Status.String(), columnWidths["status"], 1)
306 title := text.LeftPadMaxLine(snap.Title, columnWidths["title"], 1)
307 author := text.LeftPadMaxLine(person.DisplayName(), columnWidths["author"], 1)
308 summary := text.LeftPadMaxLine(summaryTxt, columnWidths["summary"], 1)
309 lastEdit := text.LeftPadMaxLine(humanize.Time(snap.LastEditTime()), columnWidths["lastEdit"], 1)
310
311 _, _ = fmt.Fprintf(v, "%s %s %s %s %s %s\n",
312 colors.Cyan(id),
313 colors.Yellow(status),
314 title,
315 colors.Magenta(author),
316 summary,
317 lastEdit,
318 )
319 }
320}
321
322func (bt *bugTable) renderHeader(v *gocui.View, maxX int) {
323 columnWidths := bt.getColumnWidths(maxX)
324
325 id := text.LeftPadMaxLine("ID", columnWidths["id"], 1)
326 status := text.LeftPadMaxLine("STATUS", columnWidths["status"], 1)
327 title := text.LeftPadMaxLine("TITLE", columnWidths["title"], 1)
328 author := text.LeftPadMaxLine("AUTHOR", columnWidths["author"], 1)
329 summary := text.LeftPadMaxLine("SUMMARY", columnWidths["summary"], 1)
330 lastEdit := text.LeftPadMaxLine("LAST EDIT", columnWidths["lastEdit"], 1)
331
332 _, _ = fmt.Fprintf(v, "\n")
333 _, _ = fmt.Fprintf(v, "%s %s %s %s %s %s\n", id, status, title, author, summary, lastEdit)
334
335}
336
337func (bt *bugTable) renderFooter(v *gocui.View, maxX int) {
338 _, _ = fmt.Fprintf(v, " \nShowing %d of %d bugs", len(bt.bugs), len(bt.allIds))
339}
340
341func (bt *bugTable) cursorDown(g *gocui.Gui, v *gocui.View) error {
342 _, y := v.Cursor()
343 y = minInt(y+1, bt.getTableLength()-1)
344
345 // window is too small to set the cursor properly, ignoring the error
346 _ = v.SetCursor(0, y)
347 bt.selectCursor = y
348
349 return nil
350}
351
352func (bt *bugTable) cursorUp(g *gocui.Gui, v *gocui.View) error {
353 _, y := v.Cursor()
354 y = maxInt(y-1, 0)
355
356 // window is too small to set the cursor properly, ignoring the error
357 _ = v.SetCursor(0, y)
358 bt.selectCursor = y
359
360 return nil
361}
362
363func (bt *bugTable) cursorClamp(v *gocui.View) error {
364 _, y := v.Cursor()
365
366 y = minInt(y, bt.getTableLength()-1)
367 y = maxInt(y, 0)
368
369 // window is too small to set the cursor properly, ignoring the error
370 _ = v.SetCursor(0, y)
371 bt.selectCursor = y
372
373 return nil
374}
375
376func (bt *bugTable) nextPage(g *gocui.Gui, v *gocui.View) error {
377 _, max := v.Size()
378
379 if bt.pageCursor+max >= len(bt.allIds) {
380 return nil
381 }
382
383 bt.pageCursor += max
384
385 return bt.doPaginate(max)
386}
387
388func (bt *bugTable) previousPage(g *gocui.Gui, v *gocui.View) error {
389 _, max := v.Size()
390
391 bt.pageCursor = maxInt(0, bt.pageCursor-max)
392
393 return bt.doPaginate(max)
394}
395
396func (bt *bugTable) newBug(g *gocui.Gui, v *gocui.View) error {
397 return newBugWithEditor(bt.repo)
398}
399
400func (bt *bugTable) openBug(g *gocui.Gui, v *gocui.View) error {
401 _, y := v.Cursor()
402 ui.showBug.SetBug(bt.bugs[y])
403 return ui.activateWindow(ui.showBug)
404}
405
406func (bt *bugTable) pull(g *gocui.Gui, v *gocui.View) error {
407 // Note: this is very hacky
408
409 ui.msgPopup.Activate("Pull from remote "+defaultRemote, "...")
410
411 go func() {
412 stdout, err := bt.repo.Fetch(defaultRemote)
413
414 if err != nil {
415 g.Update(func(gui *gocui.Gui) error {
416 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
417 return nil
418 })
419 } else {
420 g.Update(func(gui *gocui.Gui) error {
421 ui.msgPopup.UpdateMessage(stdout)
422 return nil
423 })
424 }
425
426 var buffer bytes.Buffer
427 beginLine := ""
428
429 for merge := range bt.repo.MergeAll(defaultRemote) {
430 if merge.Status == bug.MergeStatusNothing {
431 continue
432 }
433
434 if merge.Err != nil {
435 g.Update(func(gui *gocui.Gui) error {
436 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
437 return nil
438 })
439 } else {
440 _, _ = fmt.Fprintf(&buffer, "%s%s: %s",
441 beginLine, colors.Cyan(merge.Bug.HumanId()), merge,
442 )
443
444 beginLine = "\n"
445
446 g.Update(func(gui *gocui.Gui) error {
447 ui.msgPopup.UpdateMessage(buffer.String())
448 return nil
449 })
450 }
451 }
452
453 _, _ = fmt.Fprintf(&buffer, "%sdone", beginLine)
454
455 g.Update(func(gui *gocui.Gui) error {
456 ui.msgPopup.UpdateMessage(buffer.String())
457 return nil
458 })
459
460 }()
461
462 return nil
463}
464
465func (bt *bugTable) push(g *gocui.Gui, v *gocui.View) error {
466 ui.msgPopup.Activate("Push to remote "+defaultRemote, "...")
467
468 go func() {
469 // TODO: make the remote configurable
470 stdout, err := bt.repo.Push(defaultRemote)
471
472 if err != nil {
473 g.Update(func(gui *gocui.Gui) error {
474 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
475 return nil
476 })
477 } else {
478 g.Update(func(gui *gocui.Gui) error {
479 ui.msgPopup.UpdateMessage(stdout)
480 return nil
481 })
482 }
483 }()
484
485 return nil
486}
487
488func (bt *bugTable) changeQuery(g *gocui.Gui, v *gocui.View) error {
489 return editQueryWithEditor(bt)
490}