1package termui
2
3import (
4 "bytes"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/cache"
8 "github.com/MichaelMure/git-bug/entity"
9 "github.com/MichaelMure/git-bug/util/colors"
10 "github.com/MichaelMure/git-bug/util/text"
11 "github.com/MichaelMure/gocui"
12 "github.com/dustin/go-humanize"
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)
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)
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)
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)
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 snap := b.Snapshot()
293 create := snap.Comments[0]
294 authorIdentity := create.Author
295
296 summaryTxt := fmt.Sprintf("C:%-2d L:%-2d",
297 len(snap.Comments)-1,
298 len(snap.Labels),
299 )
300
301 id := text.LeftPadMaxLine(snap.HumanId(), columnWidths["id"], 1)
302 status := text.LeftPadMaxLine(snap.Status.String(), columnWidths["status"], 1)
303 title := text.LeftPadMaxLine(snap.Title, columnWidths["title"], 1)
304 author := text.LeftPadMaxLine(authorIdentity.DisplayName(), columnWidths["author"], 1)
305 summary := text.LeftPadMaxLine(summaryTxt, columnWidths["summary"], 1)
306 lastEdit := text.LeftPadMaxLine(humanize.Time(snap.LastEditTime()), columnWidths["lastEdit"], 1)
307
308 _, _ = fmt.Fprintf(v, "%s %s %s %s %s %s\n",
309 colors.Cyan(id),
310 colors.Yellow(status),
311 title,
312 colors.Magenta(author),
313 summary,
314 lastEdit,
315 )
316 }
317}
318
319func (bt *bugTable) renderHeader(v *gocui.View, maxX int) {
320 columnWidths := bt.getColumnWidths(maxX)
321
322 id := text.LeftPadMaxLine("ID", columnWidths["id"], 1)
323 status := text.LeftPadMaxLine("STATUS", columnWidths["status"], 1)
324 title := text.LeftPadMaxLine("TITLE", columnWidths["title"], 1)
325 author := text.LeftPadMaxLine("AUTHOR", columnWidths["author"], 1)
326 summary := text.LeftPadMaxLine("SUMMARY", columnWidths["summary"], 1)
327 lastEdit := text.LeftPadMaxLine("LAST EDIT", columnWidths["lastEdit"], 1)
328
329 _, _ = fmt.Fprintf(v, "\n")
330 _, _ = fmt.Fprintf(v, "%s %s %s %s %s %s\n", id, status, title, author, summary, lastEdit)
331
332}
333
334func (bt *bugTable) renderFooter(v *gocui.View, maxX int) {
335 _, _ = fmt.Fprintf(v, " \nShowing %d of %d bugs", len(bt.bugs), len(bt.allIds))
336}
337
338func (bt *bugTable) cursorDown(g *gocui.Gui, v *gocui.View) error {
339 _, y := v.Cursor()
340
341 // If we are at the bottom of the page, switch to the next one.
342 if y+1 > bt.getTableLength()-1 {
343 _, max := v.Size()
344
345 if bt.pageCursor+max >= len(bt.allIds) {
346 return nil
347 }
348
349 bt.pageCursor += max
350 bt.selectCursor = 0
351 _ = v.SetCursor(0, bt.selectCursor)
352
353 return bt.doPaginate(max)
354 }
355
356 y = minInt(y+1, bt.getTableLength()-1)
357 // window is too small to set the cursor properly, ignoring the error
358 _ = v.SetCursor(0, y)
359 bt.selectCursor = y
360
361 return nil
362}
363
364func (bt *bugTable) cursorUp(g *gocui.Gui, v *gocui.View) error {
365 _, y := v.Cursor()
366
367 // If we are at the top of the page, switch to the previous one.
368 if y-1 < 0 {
369 _, max := v.Size()
370
371 if bt.pageCursor == 0 {
372 return nil
373 }
374
375 bt.pageCursor = maxInt(0, bt.pageCursor-max)
376 bt.selectCursor = max - 1
377 _ = v.SetCursor(0, bt.selectCursor)
378
379 return bt.doPaginate(max)
380 }
381
382 y = maxInt(y-1, 0)
383 // window is too small to set the cursor properly, ignoring the error
384 _ = v.SetCursor(0, y)
385 bt.selectCursor = y
386
387 return nil
388}
389
390func (bt *bugTable) cursorClamp(v *gocui.View) error {
391 _, y := v.Cursor()
392
393 y = minInt(y, bt.getTableLength()-1)
394 y = maxInt(y, 0)
395
396 // window is too small to set the cursor properly, ignoring the error
397 _ = v.SetCursor(0, y)
398 bt.selectCursor = y
399
400 return nil
401}
402
403func (bt *bugTable) nextPage(g *gocui.Gui, v *gocui.View) error {
404 _, max := v.Size()
405
406 if bt.pageCursor+max >= len(bt.allIds) {
407 return nil
408 }
409
410 bt.pageCursor += max
411
412 return bt.doPaginate(max)
413}
414
415func (bt *bugTable) previousPage(g *gocui.Gui, v *gocui.View) error {
416 _, max := v.Size()
417
418 if bt.pageCursor == 0 {
419 return nil
420 }
421
422 bt.pageCursor = maxInt(0, bt.pageCursor-max)
423
424 return bt.doPaginate(max)
425}
426
427func (bt *bugTable) newBug(g *gocui.Gui, v *gocui.View) error {
428 return newBugWithEditor(bt.repo)
429}
430
431func (bt *bugTable) openBug(g *gocui.Gui, v *gocui.View) error {
432 _, y := v.Cursor()
433 ui.showBug.SetBug(bt.bugs[y])
434 return ui.activateWindow(ui.showBug)
435}
436
437func (bt *bugTable) pull(g *gocui.Gui, v *gocui.View) error {
438 ui.msgPopup.Activate("Pull from remote "+defaultRemote, "...")
439
440 go func() {
441 stdout, err := bt.repo.Fetch(defaultRemote)
442
443 if err != nil {
444 g.Update(func(gui *gocui.Gui) error {
445 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
446 return nil
447 })
448 } else {
449 g.Update(func(gui *gocui.Gui) error {
450 ui.msgPopup.UpdateMessage(stdout)
451 return nil
452 })
453 }
454
455 var buffer bytes.Buffer
456 beginLine := ""
457
458 for result := range bt.repo.MergeAll(defaultRemote) {
459 if result.Status == entity.MergeStatusNothing {
460 continue
461 }
462
463 if result.Err != nil {
464 g.Update(func(gui *gocui.Gui) error {
465 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
466 return nil
467 })
468 } else {
469 _, _ = fmt.Fprintf(&buffer, "%s%s: %s",
470 beginLine, colors.Cyan(result.Entity.HumanId()), result,
471 )
472
473 beginLine = "\n"
474
475 g.Update(func(gui *gocui.Gui) error {
476 ui.msgPopup.UpdateMessage(buffer.String())
477 return nil
478 })
479 }
480 }
481
482 _, _ = fmt.Fprintf(&buffer, "%sdone", beginLine)
483
484 g.Update(func(gui *gocui.Gui) error {
485 ui.msgPopup.UpdateMessage(buffer.String())
486 return nil
487 })
488
489 }()
490
491 return nil
492}
493
494func (bt *bugTable) push(g *gocui.Gui, v *gocui.View) error {
495 ui.msgPopup.Activate("Push to remote "+defaultRemote, "...")
496
497 go func() {
498 // TODO: make the remote configurable
499 stdout, err := bt.repo.Push(defaultRemote)
500
501 if err != nil {
502 g.Update(func(gui *gocui.Gui) error {
503 ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
504 return nil
505 })
506 } else {
507 g.Update(func(gui *gocui.Gui) error {
508 ui.msgPopup.UpdateMessage(stdout)
509 return nil
510 })
511 }
512 }()
513
514 return nil
515}
516
517func (bt *bugTable) changeQuery(g *gocui.Gui, v *gocui.View) error {
518 return editQueryWithEditor(bt)
519}