2018-08-01 03:15:40 +03:00
|
|
|
package termui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-08 23:22:35 +03:00
|
|
|
|
2018-08-01 03:15:40 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug/operations"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/util"
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
)
|
|
|
|
|
|
|
|
const showBugView = "showBugView"
|
|
|
|
const showBugSidebarView = "showBugSidebarView"
|
|
|
|
const showBugInstructionView = "showBugInstructionView"
|
2018-08-09 02:35:31 +03:00
|
|
|
const showBugHeaderView = "showBugHeaderView"
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
const timeLayout = "Jan _2 2006"
|
|
|
|
|
|
|
|
type showBug struct {
|
2018-08-08 23:22:35 +03:00
|
|
|
cache cache.RepoCacher
|
|
|
|
bug cache.BugCacher
|
|
|
|
childViews []string
|
|
|
|
selectableView []string
|
|
|
|
selected string
|
2018-08-09 02:35:31 +03:00
|
|
|
scroll int
|
2018-08-01 03:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newShowBug(cache cache.RepoCacher) *showBug {
|
|
|
|
return &showBug{
|
|
|
|
cache: cache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) layout(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
v, err := g.SetView(showBugView, 0, 0, maxX*2/3, maxY-2)
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
sb.childViews = append(sb.childViews, showBugView)
|
2018-08-01 03:15:40 +03:00
|
|
|
v.Frame = false
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Clear()
|
2018-08-03 18:29:53 +03:00
|
|
|
err = sb.renderMain(g, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
v, err = g.SetView(showBugSidebarView, maxX*2/3+1, 0, maxX-1, maxY-2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
sb.childViews = append(sb.childViews, showBugSidebarView)
|
|
|
|
v.Frame = true
|
2018-08-01 03:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
v.Clear()
|
|
|
|
sb.renderSidebar(v)
|
|
|
|
|
|
|
|
v, err = g.SetView(showBugInstructionView, -1, maxY-2, maxX, maxY)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
sb.childViews = append(sb.childViews, showBugInstructionView)
|
2018-08-01 03:15:40 +03:00
|
|
|
v.Frame = false
|
|
|
|
v.BgColor = gocui.ColorBlue
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
fmt.Fprintf(v, "[q] Return [c] Comment [t] Change title [↓,j] Down [↑,k] Up")
|
2018-08-01 03:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = g.SetCurrentView(showBugView)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) keybindings(g *gocui.Gui) error {
|
|
|
|
// Return
|
|
|
|
if err := g.SetKeybinding(showBugView, 'q', gocui.ModNone, sb.back); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:35:13 +03:00
|
|
|
// Scrolling
|
2018-08-01 03:15:40 +03:00
|
|
|
if err := g.SetKeybinding(showBugView, gocui.KeyPgup, gocui.ModNone,
|
|
|
|
sb.scrollUp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding(showBugView, gocui.KeyPgdn, gocui.ModNone,
|
|
|
|
sb.scrollDown); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
// Down
|
|
|
|
if err := g.SetKeybinding(showBugView, 'j', gocui.ModNone,
|
|
|
|
sb.selectNext); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding(showBugView, gocui.KeyArrowDown, gocui.ModNone,
|
|
|
|
sb.selectNext); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Up
|
|
|
|
if err := g.SetKeybinding(showBugView, 'k', gocui.ModNone,
|
|
|
|
sb.selectPrevious); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding(showBugView, gocui.KeyArrowUp, gocui.ModNone,
|
|
|
|
sb.selectPrevious); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:35:13 +03:00
|
|
|
// Comment
|
|
|
|
if err := g.SetKeybinding(showBugView, 'c', gocui.ModNone,
|
|
|
|
sb.comment); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title
|
|
|
|
if err := g.SetKeybinding(showBugView, 't', gocui.ModNone,
|
2018-08-09 02:35:31 +03:00
|
|
|
sb.setTitle); err != nil {
|
2018-08-02 17:35:13 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels
|
|
|
|
|
2018-08-01 03:15:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) disable(g *gocui.Gui) error {
|
2018-08-03 18:29:53 +03:00
|
|
|
for _, view := range sb.childViews {
|
|
|
|
if err := g.DeleteView(view); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-01 03:15:40 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
func (sb *showBug) renderMain(g *gocui.Gui, mainView *gocui.View) error {
|
|
|
|
maxX, _ := mainView.Size()
|
|
|
|
x0, y0, _, _, _ := g.ViewPosition(mainView.Name())
|
2018-08-09 02:35:31 +03:00
|
|
|
|
|
|
|
y0 -= sb.scroll
|
|
|
|
|
2018-08-02 17:35:13 +03:00
|
|
|
snap := sb.bug.Snapshot()
|
2018-08-01 03:15:40 +03:00
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
sb.childViews = nil
|
|
|
|
sb.selectableView = nil
|
|
|
|
|
|
|
|
v, err := g.SetView(showBugHeaderView, x0, y0, maxX+1, y0+4)
|
2018-08-03 18:29:53 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Frame = false
|
|
|
|
}
|
2018-08-09 02:35:31 +03:00
|
|
|
sb.childViews = append(sb.childViews, showBugHeaderView)
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
y0 += 4
|
|
|
|
|
|
|
|
v.Clear()
|
2018-08-02 17:35:13 +03:00
|
|
|
header1 := fmt.Sprintf("[%s] %s", snap.HumanId(), snap.Title)
|
2018-08-08 23:22:35 +03:00
|
|
|
fmt.Fprintf(v, util.LeftPaddedString(header1, maxX-1, 0)+"\n\n")
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
header2 := fmt.Sprintf("[%s] %s opened this bug on %s",
|
2018-08-02 17:35:13 +03:00
|
|
|
snap.Status, snap.Author.Name, snap.CreatedAt.Format(timeLayout))
|
2018-08-08 23:22:35 +03:00
|
|
|
fmt.Fprintf(v, util.LeftPaddedString(header2, maxX-1, 0))
|
2018-08-03 18:29:53 +03:00
|
|
|
|
|
|
|
for i, op := range snap.Operations {
|
|
|
|
viewName := fmt.Sprintf("op%d", i)
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
switch op.(type) {
|
|
|
|
|
|
|
|
case operations.CreateOperation:
|
|
|
|
create := op.(operations.CreateOperation)
|
2018-08-03 18:29:53 +03:00
|
|
|
content, lines := util.TextWrap(create.Message, maxX)
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true)
|
2018-08-03 18:29:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprint(v, content)
|
|
|
|
y0 += lines + 2
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
case operations.AddCommentOperation:
|
|
|
|
comment := op.(operations.AddCommentOperation)
|
2018-08-03 18:29:53 +03:00
|
|
|
|
2018-08-01 03:15:40 +03:00
|
|
|
header := fmt.Sprintf("%s commented on %s",
|
|
|
|
comment.Author.Name, comment.Time().Format(timeLayout))
|
2018-08-03 18:29:53 +03:00
|
|
|
header = util.LeftPaddedString(header, maxX, 6)
|
|
|
|
message, lines := util.TextWrap(comment.Message, maxX)
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines+2, true)
|
2018-08-03 18:29:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprint(v, header, "\n\n", message)
|
|
|
|
y0 += lines + 3
|
2018-08-01 03:15:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
func (sb *showBug) createOpView(g *gocui.Gui, name string, x0 int, y0 int, maxX int, height int, selectable bool) (*gocui.View, error) {
|
|
|
|
v, err := g.SetView(name, x0, y0, maxX, y0+height+1)
|
2018-08-03 18:29:53 +03:00
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
if err != nil && err != gocui.ErrUnknownView {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-03 18:29:53 +03:00
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
sb.childViews = append(sb.childViews, name)
|
2018-08-08 23:22:35 +03:00
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
if selectable {
|
|
|
|
sb.selectableView = append(sb.selectableView, name)
|
2018-08-03 18:29:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
v.Frame = sb.selected == name
|
|
|
|
|
2018-08-03 18:29:53 +03:00
|
|
|
v.Clear()
|
|
|
|
|
|
|
|
return v, nil
|
2018-08-01 03:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) renderSidebar(v *gocui.View) {
|
|
|
|
maxX, _ := v.Size()
|
2018-08-02 17:35:13 +03:00
|
|
|
snap := sb.bug.Snapshot()
|
2018-08-01 03:15:40 +03:00
|
|
|
|
|
|
|
title := util.LeftPaddedString("LABEL", maxX, 2)
|
|
|
|
fmt.Fprintf(v, title+"\n\n")
|
|
|
|
|
2018-08-02 17:35:13 +03:00
|
|
|
for _, label := range snap.Labels {
|
2018-08-01 03:15:40 +03:00
|
|
|
fmt.Fprintf(v, util.LeftPaddedString(label.String(), maxX, 2))
|
|
|
|
fmt.Fprintln(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) back(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
ui.activateWindow(ui.bugTable)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) scrollUp(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) scrollDown(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-02 17:35:13 +03:00
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
func (sb *showBug) selectPrevious(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if len(sb.selectableView) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
defer sb.focusView(g)
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
for i, name := range sb.selectableView {
|
|
|
|
if name == sb.selected {
|
2018-08-09 02:35:31 +03:00
|
|
|
// special case to scroll up to the top
|
|
|
|
if i == 0 {
|
|
|
|
sb.scroll = 0
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
sb.selected = sb.selectableView[maxInt(i-1, 0)]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sb.selected == "" {
|
|
|
|
sb.selected = sb.selectableView[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *showBug) selectNext(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if len(sb.selectableView) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
defer sb.focusView(g)
|
|
|
|
|
2018-08-08 23:22:35 +03:00
|
|
|
for i, name := range sb.selectableView {
|
|
|
|
if name == sb.selected {
|
2018-08-09 02:35:31 +03:00
|
|
|
sb.selected = sb.selectableView[minInt(i+1, len(sb.selectableView)-1)]
|
2018-08-08 23:22:35 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sb.selected == "" {
|
|
|
|
sb.selected = sb.selectableView[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
func (sb *showBug) focusView(g *gocui.Gui) error {
|
|
|
|
mainView, err := g.View(showBugView)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, maxY := mainView.Size()
|
|
|
|
|
|
|
|
_, vy0, _, _, err := g.ViewPosition(sb.selected)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := g.View(sb.selected)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, vMaxY := v.Size()
|
|
|
|
|
|
|
|
vy1 := vy0 + vMaxY
|
|
|
|
|
|
|
|
if vy0 < 0 {
|
|
|
|
sb.scroll += vy0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if vy1 > maxY {
|
|
|
|
sb.scroll -= maxY - vy1
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:35:13 +03:00
|
|
|
func (sb *showBug) comment(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return addCommentWithEditor(sb.bug)
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:35:31 +03:00
|
|
|
func (sb *showBug) setTitle(g *gocui.Gui, v *gocui.View) error {
|
2018-08-02 17:35:13 +03:00
|
|
|
return setTitleWithEditor(sb.bug)
|
|
|
|
}
|