bug_table.go

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