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
344 // If we are at the bottom of the page, switch to the next one.
345 if y+1 > bt.getTableLength()-1 {
346 _, max := v.Size()
347
348 if bt.pageCursor+max >= len(bt.allIds) {
349 return nil
350 }
351
352 bt.pageCursor += max
353 bt.selectCursor = 0
354 _ = v.SetCursor(0, bt.selectCursor)
355
356 return bt.doPaginate(max)
357 }
358
359 y = minInt(y+1, bt.getTableLength()-1)
360 // window is too small to set the cursor properly, ignoring the error
361 _ = v.SetCursor(0, y)
362 bt.selectCursor = y
363
364 return nil
365}
366
367func (bt *bugTable) cursorUp(g *gocui.Gui, v *gocui.View) error {
368 _, y := v.Cursor()
369
370 // If we are at the top of the page, switch to the previous one.
371 if y-1 < 0 {
372 _, max := v.Size()
373
374 if bt.pageCursor == 0 {
375 return nil
376 }
377
378 bt.pageCursor = maxInt(0, bt.pageCursor-max)
379 bt.selectCursor = max - 1
380 _ = v.SetCursor(0, bt.selectCursor)
381
382 return bt.doPaginate(max)
383 }
384
385 y = maxInt(y-1, 0)
386 // window is too small to set the cursor properly, ignoring the error
387 _ = v.SetCursor(0, y)
388 bt.selectCursor = y
389
390 return nil
391}
392
393func (bt *bugTable) cursorClamp(v *gocui.View) error {
394 _, y := v.Cursor()
395
396 y = minInt(y, bt.getTableLength()-1)
397 y = maxInt(y, 0)
398
399 // window is too small to set the cursor properly, ignoring the error
400 _ = v.SetCursor(0, y)
401 bt.selectCursor = y
402
403 return nil
404}
405
406func (bt *bugTable) nextPage(g *gocui.Gui, v *gocui.View) error {
407 _, max := v.Size()
408
409 if bt.pageCursor+max >= len(bt.allIds) {
410 return nil
411 }
412
413 bt.pageCursor += max
414
415 return bt.doPaginate(max)
416}
417
418func (bt *bugTable) previousPage(g *gocui.Gui, v *gocui.View) error {
419 _, max := v.Size()
420
421 if bt.pageCursor == 0 {
422 return nil
423 }
424
425 bt.pageCursor = maxInt(0, bt.pageCursor-max)
426
427 return bt.doPaginate(max)
428}
429
430func (bt *bugTable) newBug(g *gocui.Gui, v *gocui.View) error {
431 return newBugWithEditor(bt.repo)
432}
433
434func (bt *bugTable) openBug(g *gocui.Gui, v *gocui.View) error {
435 _, y := v.Cursor()
436 ui.showBug.SetBug(bt.bugs[y])
437 return ui.activateWindow(ui.showBug)
438}
439
440func (bt *bugTable) pull(g *gocui.Gui, v *gocui.View) error {
441 // Note: this is very hacky
442
443 ui.msgPopup.Activate("Pull from remote "+defaultRemote, "...")
444
445 go func() {
446 stdout, err := bt.repo.Fetch(defaultRemote)
447
448 if err != nil {
449 g.Update(func(gui *gocui.Gui) error {
450 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
451 return nil
452 })
453 } else {
454 g.Update(func(gui *gocui.Gui) error {
455 ui.msgPopup.UpdateMessage(stdout)
456 return nil
457 })
458 }
459
460 var buffer bytes.Buffer
461 beginLine := ""
462
463 for merge := range bt.repo.MergeAll(defaultRemote) {
464 if merge.Status == bug.MergeStatusNothing {
465 continue
466 }
467
468 if merge.Err != nil {
469 g.Update(func(gui *gocui.Gui) error {
470 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
471 return nil
472 })
473 } else {
474 _, _ = fmt.Fprintf(&buffer, "%s%s: %s",
475 beginLine, colors.Cyan(merge.Bug.HumanId()), merge,
476 )
477
478 beginLine = "\n"
479
480 g.Update(func(gui *gocui.Gui) error {
481 ui.msgPopup.UpdateMessage(buffer.String())
482 return nil
483 })
484 }
485 }
486
487 _, _ = fmt.Fprintf(&buffer, "%sdone", beginLine)
488
489 g.Update(func(gui *gocui.Gui) error {
490 ui.msgPopup.UpdateMessage(buffer.String())
491 return nil
492 })
493
494 }()
495
496 return nil
497}
498
499func (bt *bugTable) push(g *gocui.Gui, v *gocui.View) error {
500 ui.msgPopup.Activate("Push to remote "+defaultRemote, "...")
501
502 go func() {
503 // TODO: make the remote configurable
504 stdout, err := bt.repo.Push(defaultRemote)
505
506 if err != nil {
507 g.Update(func(gui *gocui.Gui) error {
508 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
509 return nil
510 })
511 } else {
512 g.Update(func(gui *gocui.Gui) error {
513 ui.msgPopup.UpdateMessage(stdout)
514 return nil
515 })
516 }
517 }()
518
519 return nil
520}
521
522func (bt *bugTable) changeQuery(g *gocui.Gui, v *gocui.View) error {
523 return editQueryWithEditor(bt)
524}