git-bug/termui/bug_table.go

487 lines
11 KiB
Go
Raw Normal View History

2018-07-30 18:00:10 +03:00
package termui
import (
2018-08-12 22:09:30 +03:00
"bytes"
2018-07-30 18:00:10 +03:00
"fmt"
2018-08-12 22:09:30 +03:00
2018-07-30 18:00:10 +03:00
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/colors"
"github.com/MichaelMure/git-bug/util/text"
"github.com/dustin/go-humanize"
2018-07-30 18:00:10 +03:00
"github.com/jroimartin/gocui"
)
const bugTableView = "bugTableView"
2018-08-01 03:15:40 +03:00
const bugTableHeaderView = "bugTableHeaderView"
const bugTableFooterView = "bugTableFooterView"
const bugTableInstructionView = "bugTableInstructionView"
2018-09-11 20:28:32 +03:00
const defaultRemote = "origin"
const defaultQuery = "status:open"
2018-08-12 22:09:30 +03:00
2018-07-30 18:00:10 +03:00
type bugTable struct {
repo *cache.RepoCache
2018-09-11 20:28:32 +03:00
queryStr string
query *cache.Query
2018-08-01 03:15:40 +03:00
allIds []string
bugs []*cache.BugCache
2018-08-01 03:15:40 +03:00
pageCursor int
selectCursor int
2018-07-30 18:00:10 +03:00
}
func newBugTable(c *cache.RepoCache) *bugTable {
2018-09-11 20:28:32 +03:00
query, err := cache.ParseQuery(defaultQuery)
if err != nil {
panic(err)
}
2018-07-30 18:00:10 +03:00
return &bugTable{
2018-09-11 20:28:32 +03:00
repo: c,
query: query,
queryStr: defaultQuery,
2018-08-01 03:15:40 +03:00
pageCursor: 0,
selectCursor: 0,
2018-07-30 18:00:10 +03:00
}
}
func (bt *bugTable) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if maxY < 4 {
// window too small !
return nil
}
2018-08-01 03:15:40 +03:00
v, err := g.SetView(bugTableHeaderView, -1, -1, maxX, 3)
2018-07-30 18:00:10 +03:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
}
v.Clear()
bt.renderHeader(v, maxX)
2018-07-30 18:00:10 +03:00
2018-07-31 23:30:50 +03:00
v, err = g.SetView(bugTableView, -1, 1, maxX, maxY-3)
2018-07-30 18:00:10 +03:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.Highlight = true
v.SelBgColor = gocui.ColorWhite
v.SelFgColor = gocui.ColorBlack
2018-08-01 03:15:40 +03:00
// restore the cursor
// window is too small to set the cursor properly, ignoring the error
_ = v.SetCursor(0, bt.selectCursor)
2018-07-30 18:00:10 +03:00
}
_, viewHeight := v.Size()
2018-07-31 23:30:50 +03:00
err = bt.paginate(viewHeight)
if err != nil {
return err
}
err = bt.cursorClamp(v)
2018-07-30 18:00:10 +03:00
if err != nil {
return err
}
v.Clear()
bt.render(v, maxX)
2018-07-30 18:00:10 +03:00
2018-08-01 03:15:40 +03:00
v, err = g.SetView(bugTableFooterView, -1, maxY-4, maxX, maxY)
2018-07-30 18:00:10 +03:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
}
v.Clear()
bt.renderFooter(v, maxX)
2018-07-30 18:00:10 +03:00
2018-08-01 03:15:40 +03:00
v, err = g.SetView(bugTableInstructionView, -1, maxY-2, maxX, maxY)
2018-07-30 18:00:10 +03:00
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.BgColor = gocui.ColorBlue
fmt.Fprintf(v, "[Esc] Quit [←↓↑→,hjkl] Navigation [enter] Open bug [n] New bug [i] Pull [o] Push")
}
_, err = g.SetCurrentView(bugTableView)
2018-08-01 03:15:40 +03:00
return err
}
func (bt *bugTable) keybindings(g *gocui.Gui) error {
// Quit
if err := g.SetKeybinding(bugTableView, gocui.KeyEsc, gocui.ModNone, quit); err != nil {
return err
}
// Down
if err := g.SetKeybinding(bugTableView, 'j', gocui.ModNone,
bt.cursorDown); err != nil {
return err
}
if err := g.SetKeybinding(bugTableView, gocui.KeyArrowDown, gocui.ModNone,
bt.cursorDown); err != nil {
return err
}
// Up
if err := g.SetKeybinding(bugTableView, 'k', gocui.ModNone,
bt.cursorUp); err != nil {
return err
}
if err := g.SetKeybinding(bugTableView, gocui.KeyArrowUp, gocui.ModNone,
bt.cursorUp); err != nil {
return err
}
// Previous page
if err := g.SetKeybinding(bugTableView, 'h', gocui.ModNone,
bt.previousPage); err != nil {
return err
}
if err := g.SetKeybinding(bugTableView, gocui.KeyArrowLeft, gocui.ModNone,
bt.previousPage); err != nil {
return err
}
if err := g.SetKeybinding(bugTableView, gocui.KeyPgup, gocui.ModNone,
bt.previousPage); err != nil {
return err
}
// Next page
if err := g.SetKeybinding(bugTableView, 'l', gocui.ModNone,
bt.nextPage); err != nil {
return err
}
if err := g.SetKeybinding(bugTableView, gocui.KeyArrowRight, gocui.ModNone,
bt.nextPage); err != nil {
return err
}
if err := g.SetKeybinding(bugTableView, gocui.KeyPgdn, gocui.ModNone,
bt.nextPage); err != nil {
return err
}
// New bug
if err := g.SetKeybinding(bugTableView, 'n', gocui.ModNone,
bt.newBug); err != nil {
return err
}
2018-08-01 03:15:40 +03:00
// Open bug
if err := g.SetKeybinding(bugTableView, gocui.KeyEnter, gocui.ModNone,
bt.openBug); err != nil {
return err
}
2018-08-12 22:09:30 +03:00
// Pull
if err := g.SetKeybinding(bugTableView, 'i', gocui.ModNone,
bt.pull); err != nil {
return err
}
// Push
if err := g.SetKeybinding(bugTableView, 'o', gocui.ModNone,
bt.push); err != nil {
return err
}
2018-09-11 20:28:32 +03:00
// Query
if err := g.SetKeybinding(bugTableView, 'q', gocui.ModNone,
bt.changeQuery); err != nil {
return err
}
2018-08-01 03:15:40 +03:00
return nil
}
func (bt *bugTable) disable(g *gocui.Gui) error {
2018-08-17 13:37:58 +03:00
if err := g.DeleteView(bugTableView); err != nil && err != gocui.ErrUnknownView {
2018-08-01 03:15:40 +03:00
return err
}
2018-08-17 13:37:58 +03:00
if err := g.DeleteView(bugTableHeaderView); err != nil && err != gocui.ErrUnknownView {
2018-08-01 03:15:40 +03:00
return err
}
2018-08-17 13:37:58 +03:00
if err := g.DeleteView(bugTableFooterView); err != nil && err != gocui.ErrUnknownView {
2018-08-01 03:15:40 +03:00
return err
}
2018-08-17 13:37:58 +03:00
if err := g.DeleteView(bugTableInstructionView); err != nil && err != gocui.ErrUnknownView {
2018-08-01 03:15:40 +03:00
return err
}
return nil
}
func (bt *bugTable) paginate(max int) error {
bt.allIds = bt.repo.QueryBugs(bt.query)
return bt.doPaginate(max)
}
func (bt *bugTable) doPaginate(max int) error {
// clamp the cursor
2018-08-01 03:15:40 +03:00
bt.pageCursor = maxInt(bt.pageCursor, 0)
bt.pageCursor = minInt(bt.pageCursor, len(bt.allIds))
nb := minInt(len(bt.allIds)-bt.pageCursor, max)
if nb < 0 {
bt.bugs = []*cache.BugCache{}
return nil
}
// slice the data
ids := bt.allIds[bt.pageCursor : bt.pageCursor+nb]
bt.bugs = make([]*cache.BugCache, len(ids))
for i, id := range ids {
b, err := bt.repo.ResolveBug(id)
if err != nil {
return err
}
bt.bugs[i] = b
2018-07-30 18:00:10 +03:00
}
return nil
}
func (bt *bugTable) getTableLength() int {
return len(bt.bugs)
}
func (bt *bugTable) getColumnWidths(maxX int) map[string]int {
m := make(map[string]int)
m["id"] = 10
m["status"] = 8
left := maxX - 5 - m["id"] - m["status"]
m["summary"] = maxInt(11, left/6)
left -= m["summary"]
m["lastEdit"] = maxInt(19, left/6)
left -= m["lastEdit"]
m["author"] = maxInt(left*2/5, 15)
m["title"] = maxInt(left-m["author"], 10)
return m
}
2018-07-30 18:00:10 +03:00
func (bt *bugTable) render(v *gocui.View, maxX int) {
columnWidths := bt.getColumnWidths(maxX)
2018-07-30 18:00:10 +03:00
for _, b := range bt.bugs {
person := bug.Person{}
snap := b.Snapshot()
if len(snap.Comments) > 0 {
create := snap.Comments[0]
person = create.Author
}
id := text.LeftPaddedString(snap.HumanId(), columnWidths["id"], 2)
status := text.LeftPaddedString(snap.Status.String(), columnWidths["status"], 2)
title := text.LeftPaddedString(snap.Title, columnWidths["title"], 2)
author := text.LeftPaddedString(person.Name, columnWidths["author"], 2)
summary := text.LeftPaddedString(snap.Summary(), columnWidths["summary"], 2)
lastEdit := text.LeftPaddedString(humanize.Time(snap.LastEditTime()), columnWidths["lastEdit"], 2)
2018-08-08 23:21:02 +03:00
fmt.Fprintf(v, "%s %s %s %s %s %s\n",
colors.Cyan(id),
colors.Yellow(status),
2018-08-08 23:21:02 +03:00
title,
colors.Magenta(author),
2018-08-08 23:21:02 +03:00
summary,
lastEdit,
)
2018-07-30 18:00:10 +03:00
}
}
func (bt *bugTable) renderHeader(v *gocui.View, maxX int) {
columnWidths := bt.getColumnWidths(maxX)
id := text.LeftPaddedString("ID", columnWidths["id"], 2)
status := text.LeftPaddedString("STATUS", columnWidths["status"], 2)
title := text.LeftPaddedString("TITLE", columnWidths["title"], 2)
author := text.LeftPaddedString("AUTHOR", columnWidths["author"], 2)
summary := text.LeftPaddedString("SUMMARY", columnWidths["summary"], 2)
lastEdit := text.LeftPaddedString("LAST EDIT", columnWidths["lastEdit"], 2)
fmt.Fprintf(v, "\n")
fmt.Fprintf(v, "%s %s %s %s %s %s\n", id, status, title, author, summary, lastEdit)
2018-07-30 18:00:10 +03:00
}
func (bt *bugTable) renderFooter(v *gocui.View, maxX int) {
2018-07-31 23:30:50 +03:00
fmt.Fprintf(v, " \nShowing %d of %d bugs", len(bt.bugs), len(bt.allIds))
2018-07-30 18:00:10 +03:00
}
func (bt *bugTable) cursorDown(g *gocui.Gui, v *gocui.View) error {
_, y := v.Cursor()
y = minInt(y+1, bt.getTableLength()-1)
// window is too small to set the cursor properly, ignoring the error
_ = v.SetCursor(0, y)
2018-08-01 03:15:40 +03:00
bt.selectCursor = y
return nil
}
func (bt *bugTable) cursorUp(g *gocui.Gui, v *gocui.View) error {
_, y := v.Cursor()
y = maxInt(y-1, 0)
// window is too small to set the cursor properly, ignoring the error
_ = v.SetCursor(0, y)
2018-08-01 03:15:40 +03:00
bt.selectCursor = y
return nil
2018-07-30 18:00:10 +03:00
}
func (bt *bugTable) cursorClamp(v *gocui.View) error {
_, y := v.Cursor()
y = minInt(y, bt.getTableLength()-1)
y = maxInt(y, 0)
// window is too small to set the cursor properly, ignoring the error
_ = v.SetCursor(0, y)
2018-08-01 03:15:40 +03:00
bt.selectCursor = y
return nil
}
func (bt *bugTable) nextPage(g *gocui.Gui, v *gocui.View) error {
_, max := v.Size()
if bt.pageCursor+max >= len(bt.allIds) {
return nil
}
2018-08-01 03:15:40 +03:00
bt.pageCursor += max
return bt.doPaginate(max)
}
func (bt *bugTable) previousPage(g *gocui.Gui, v *gocui.View) error {
_, max := v.Size()
2018-08-01 03:15:40 +03:00
bt.pageCursor = maxInt(0, bt.pageCursor-max)
return bt.doPaginate(max)
2018-07-30 18:00:10 +03:00
}
2018-08-01 03:15:40 +03:00
func (bt *bugTable) newBug(g *gocui.Gui, v *gocui.View) error {
return newBugWithEditor(bt.repo)
}
2018-08-01 03:15:40 +03:00
func (bt *bugTable) openBug(g *gocui.Gui, v *gocui.View) error {
_, y := v.Cursor()
ui.showBug.SetBug(bt.bugs[y])
2018-08-01 03:15:40 +03:00
return ui.activateWindow(ui.showBug)
}
2018-08-12 22:09:30 +03:00
func (bt *bugTable) pull(g *gocui.Gui, v *gocui.View) error {
// Note: this is very hacky
2018-09-11 20:28:32 +03:00
ui.msgPopup.Activate("Pull from remote "+defaultRemote, "...")
2018-08-12 22:09:30 +03:00
go func() {
2018-09-11 20:28:32 +03:00
stdout, err := bt.repo.Fetch(defaultRemote)
2018-08-12 22:09:30 +03:00
if err != nil {
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
return nil
})
} else {
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.UpdateMessage(stdout)
return nil
})
}
var buffer bytes.Buffer
beginLine := ""
2018-09-11 20:28:32 +03:00
for merge := range bt.repo.MergeAll(defaultRemote) {
2018-08-12 22:09:30 +03:00
if merge.Status == bug.MsgMergeNothing {
continue
}
if merge.Err != nil {
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
return nil
})
} else {
fmt.Fprintf(&buffer, "%s%s: %s",
beginLine, colors.Cyan(merge.Bug.HumanId()), merge.Status,
2018-08-12 22:09:30 +03:00
)
beginLine = "\n"
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.UpdateMessage(buffer.String())
return nil
})
}
}
fmt.Fprintf(&buffer, "%sdone", beginLine)
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.UpdateMessage(buffer.String())
return nil
})
}()
return nil
}
func (bt *bugTable) push(g *gocui.Gui, v *gocui.View) error {
2018-09-11 20:28:32 +03:00
ui.msgPopup.Activate("Push to remote "+defaultRemote, "...")
2018-08-12 22:09:30 +03:00
go func() {
// TODO: make the remote configurable
2018-09-11 20:28:32 +03:00
stdout, err := bt.repo.Push(defaultRemote)
2018-08-12 22:09:30 +03:00
if err != nil {
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.Activate(msgPopupErrorTitle, err.Error())
return nil
})
} else {
g.Update(func(gui *gocui.Gui) error {
ui.msgPopup.UpdateMessage(stdout)
return nil
})
}
}()
return nil
}
2018-09-11 20:28:32 +03:00
func (bt *bugTable) changeQuery(g *gocui.Gui, v *gocui.View) error {
return editQueryWithEditor(bt)
}