termui: commit the bug when quiting the show bug window

Michael Muré created

Change summary

bug/bug.go         |  7 ++++++-
cache/cache.go     |  8 ++++++++
termui/show_bug.go | 10 +++++++---
util/colors.go     |  1 +
4 files changed, 22 insertions(+), 4 deletions(-)

Detailed changes

bug/bug.go 🔗

@@ -299,11 +299,16 @@ func (bug *Bug) IsValid() bool {
 	return true
 }
 
-// Append an operation into the staging area, to be commited later
+// Append an operation into the staging area, to be committed later
 func (bug *Bug) Append(op Operation) {
 	bug.staging.Append(op)
 }
 
+// Return if the bug need to be committed
+func (bug *Bug) HasPendingOp() bool {
+	return !bug.staging.IsEmpty()
+}
+
 // Write the staging area in Git and move the operations to the packs
 func (bug *Bug) Commit(repo repository.Repo) error {
 	if bug.staging.IsEmpty() {

cache/cache.go 🔗

@@ -43,6 +43,7 @@ type BugCacher interface {
 	SetTitle(title string) error
 
 	Commit() error
+	CommitAsNeeded() error
 }
 
 // Cacher ------------------------
@@ -294,3 +295,10 @@ func (c *BugCache) SetTitle(title string) error {
 func (c *BugCache) Commit() error {
 	return c.bug.Commit(c.repo)
 }
+
+func (c *BugCache) CommitAsNeeded() error {
+	if c.bug.HasPendingOp() {
+		return c.bug.Commit(c.repo)
+	}
+	return nil
+}

termui/show_bug.go 🔗

@@ -76,7 +76,7 @@ func (sb *showBug) layout(g *gocui.Gui) error {
 		v.Frame = false
 		v.BgColor = gocui.ColorBlue
 
-		fmt.Fprintf(v, "[q] Return [c] Comment [t] Change title [↓,j] Down [↑,k] Up")
+		fmt.Fprintf(v, "[q] Save and return [c] Comment [t] Change title [↓,j] Down [↑,k] Up")
 	}
 
 	_, err = g.SetCurrentView(showBugView)
@@ -85,7 +85,7 @@ func (sb *showBug) layout(g *gocui.Gui) error {
 
 func (sb *showBug) keybindings(g *gocui.Gui) error {
 	// Return
-	if err := g.SetKeybinding(showBugView, 'q', gocui.ModNone, sb.back); err != nil {
+	if err := g.SetKeybinding(showBugView, 'q', gocui.ModNone, sb.saveAndBack); err != nil {
 		return err
 	}
 
@@ -263,7 +263,11 @@ func (sb *showBug) renderSidebar(v *gocui.View) {
 	}
 }
 
-func (sb *showBug) back(g *gocui.Gui, v *gocui.View) error {
+func (sb *showBug) saveAndBack(g *gocui.Gui, v *gocui.View) error {
+	err := sb.bug.CommitAsNeeded()
+	if err != nil {
+		return err
+	}
 	ui.activateWindow(ui.bugTable)
 	return nil
 }

util/colors.go 🔗

@@ -3,6 +3,7 @@ package util
 import "github.com/fatih/color"
 
 var (
+	Bold       = color.New(color.Bold).SprintFunc()
 	Black      = color.New(color.FgBlack).SprintFunc()
 	BlackBg    = color.New(color.BgBlack, color.FgWhite).SprintFunc()
 	White      = color.New(color.FgWhite).SprintFunc()