From 5675299c8dd9488c3b60142d8da8b112473a0cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 9 Aug 2018 14:45:02 +0200 Subject: [PATCH] termui: commit the bug when quiting the show bug window --- bug/bug.go | 7 ++++++- cache/cache.go | 8 ++++++++ termui/show_bug.go | 10 +++++++--- util/colors.go | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bug/bug.go b/bug/bug.go index b90beaa7cefe41ab5f0411ad57a7090bbe2b0abe..3c52adc8304dcbedcebc471e8c4e4d3cb9d26b1e 100644 --- a/bug/bug.go +++ b/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() { diff --git a/cache/cache.go b/cache/cache.go index b6f47c6de6904d239851fdd9bb211ab2be83f3cd..da0a2681e0d6b9997c49c752b86fa87e95513242 100644 --- a/cache/cache.go +++ b/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 +} diff --git a/termui/show_bug.go b/termui/show_bug.go index 0189b2b3438cd9c52ccc314d6a5f09d8b73e89cd..aecdce4bc33164f7c8c6258bd56032c6a6609680 100644 --- a/termui/show_bug.go +++ b/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 } diff --git a/util/colors.go b/util/colors.go index 5169bef18675a0b295529d5b009af556eecfb68d..5aa343634a35e9767846ef3a2318ba78d3a3bf72 100644 --- a/util/colors.go +++ b/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()