diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-31 16:43:43 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-31 16:44:23 +0200 |
commit | 87669e0f18f282854d340a676834b939e34e5ed3 (patch) | |
tree | c78eaa155d2939d6647ab814c6710d0d3ed69a6e /termui/bug_table.go | |
parent | eb39c5c29bc0e9b5e15a940a1b71bdac688b6535 (diff) | |
download | git-bug-87669e0f18f282854d340a676834b939e34e5ed3.tar.gz |
termui: use the editor to create a new bug
Diffstat (limited to 'termui/bug_table.go')
-rw-r--r-- | termui/bug_table.go | 257 |
1 files changed, 176 insertions, 81 deletions
diff --git a/termui/bug_table.go b/termui/bug_table.go index 89f43c87..264dff2d 100644 --- a/termui/bug_table.go +++ b/termui/bug_table.go @@ -8,6 +8,8 @@ import ( "github.com/jroimartin/gocui" ) +const bugTableView = "bugTableView" + type bugTable struct { cache cache.RepoCacher allIds []string @@ -22,71 +24,6 @@ func newBugTable(cache cache.RepoCacher) *bugTable { } } -func (bt *bugTable) paginate(max int) error { - allIds, err := bt.cache.AllBugIds() - if err != nil { - return err - } - - bt.allIds = allIds - - return bt.doPaginate(allIds, max) -} - -func (bt *bugTable) nextPage(max int) error { - allIds, err := bt.cache.AllBugIds() - if err != nil { - return err - } - - bt.allIds = allIds - - if bt.cursor+max >= len(allIds) { - return nil - } - - bt.cursor += max - - return bt.doPaginate(allIds, max) -} - -func (bt *bugTable) previousPage(max int) error { - allIds, err := bt.cache.AllBugIds() - if err != nil { - return err - } - - bt.allIds = allIds - - bt.cursor = maxInt(0, bt.cursor-max) - - return bt.doPaginate(allIds, max) -} - -func (bt *bugTable) doPaginate(allIds []string, max int) error { - // clamp the cursor - bt.cursor = maxInt(bt.cursor, 0) - bt.cursor = minInt(bt.cursor, len(allIds)-1) - - nb := minInt(len(allIds)-bt.cursor, max) - - // slice the data - ids := allIds[bt.cursor : bt.cursor+nb] - - bt.bugs = make([]*bug.Snapshot, len(ids)) - - for i, id := range ids { - b, err := bt.cache.ResolveBug(id) - if err != nil { - return err - } - - bt.bugs[i] = b.Snapshot() - } - - return nil -} - func (bt *bugTable) layout(g *gocui.Gui) error { maxX, maxY := g.Size() @@ -101,9 +38,9 @@ func (bt *bugTable) layout(g *gocui.Gui) error { } v.Clear() - ui.bugTable.renderHeader(v, maxX) + bt.renderHeader(v, maxX) - v, err = g.SetView("bugTable", -1, 1, maxX, maxY-2) + v, err = g.SetView(bugTableView, -1, 1, maxX, maxY-2) if err != nil { if err != gocui.ErrUnknownView { @@ -115,21 +52,26 @@ func (bt *bugTable) layout(g *gocui.Gui) error { v.SelBgColor = gocui.ColorWhite v.SelFgColor = gocui.ColorBlack - _, err = g.SetCurrentView("bugTable") + _, err = g.SetCurrentView(bugTableView) if err != nil { return err } } - _, tableHeight := v.Size() - err = bt.paginate(tableHeight) + _, viewHeight := v.Size() + err = bt.paginate(viewHeight - 1) + if err != nil { + return err + } + + err = bt.cursorClamp(v) if err != nil { return err } v.Clear() - ui.bugTable.render(v, maxX) + bt.render(v, maxX) v, err = g.SetView("footer", -1, maxY-3, maxX, maxY) @@ -142,7 +84,7 @@ func (bt *bugTable) layout(g *gocui.Gui) error { } v.Clear() - ui.bugTable.renderFooter(v, maxX) + bt.renderFooter(v, maxX) v, err = g.SetView("instructions", -1, maxY-2, maxX, maxY) @@ -154,7 +96,103 @@ func (bt *bugTable) layout(g *gocui.Gui) error { v.Frame = false v.BgColor = gocui.ColorBlue - fmt.Fprintf(v, "[q] Quit [h] Previous page [j] Down [k] Up [l] Next page [enter] Open bug") + fmt.Fprintf(v, "[q] Quit [←,h] Previous page [↓,j] Down [↑,k] Up [→,l] Next page [enter] Open bug [n] New bug") + } + + return nil +} + +func (bt *bugTable) keybindings(g *gocui.Gui) error { + // Quit + if err := g.SetKeybinding(bugTableView, 'q', 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, + newBugWithEditor); err != nil { + return err + } + + return nil +} + +func (bt *bugTable) paginate(max int) error { + allIds, err := bt.cache.AllBugIds() + if err != nil { + return err + } + + bt.allIds = allIds + + return bt.doPaginate(allIds, max) +} + +func (bt *bugTable) doPaginate(allIds []string, max int) error { + // clamp the cursor + bt.cursor = maxInt(bt.cursor, 0) + bt.cursor = minInt(bt.cursor, len(allIds)-1) + + nb := minInt(len(allIds)-bt.cursor, max) + + // slice the data + ids := allIds[bt.cursor : bt.cursor+nb] + + bt.bugs = make([]*bug.Snapshot, len(ids)) + + for i, id := range ids { + b, err := bt.cache.ResolveBug(id) + if err != nil { + return err + } + + bt.bugs[i] = b.Snapshot() } return nil @@ -218,16 +256,73 @@ func (bt *bugTable) renderFooter(v *gocui.View, maxX int) { fmt.Fprintf(v, "Showing %d of %d bugs", len(bt.bugs), len(bt.allIds)) } -func maxInt(a, b int) int { - if a > b { - return a +func (bt *bugTable) cursorDown(g *gocui.Gui, v *gocui.View) error { + _, y := v.Cursor() + y = minInt(y+1, bt.getTableLength()-1) + + err := v.SetCursor(0, y) + if err != nil { + return err } - return b + + return nil +} + +func (bt *bugTable) cursorUp(g *gocui.Gui, v *gocui.View) error { + _, y := v.Cursor() + y = maxInt(y-1, 0) + + err := v.SetCursor(0, y) + if err != nil { + return err + } + + return nil } -func minInt(a, b int) int { - if a > b { - return b +func (bt *bugTable) cursorClamp(v *gocui.View) error { + _, y := v.Cursor() + + y = minInt(y, bt.getTableLength()-1) + y = maxInt(y, 0) + + err := v.SetCursor(0, y) + if err != nil { + return err + } + + return nil +} + +func (bt *bugTable) nextPage(g *gocui.Gui, v *gocui.View) error { + _, max := v.Size() + + allIds, err := bt.cache.AllBugIds() + if err != nil { + return err } - return a + + bt.allIds = allIds + + if bt.cursor+max >= len(allIds) { + return nil + } + + bt.cursor += max + + return bt.doPaginate(allIds, max) +} + +func (bt *bugTable) previousPage(g *gocui.Gui, v *gocui.View) error { + _, max := v.Size() + allIds, err := bt.cache.AllBugIds() + if err != nil { + return err + } + + bt.allIds = allIds + + bt.cursor = maxInt(0, bt.cursor-max) + + return bt.doPaginate(allIds, max) } |