diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-27 20:08:10 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-27 20:08:10 +1000 |
commit | 96eef7838ee0402ada15a9509a7f08a811b81cd3 (patch) | |
tree | 9b413fc5eb6775ffcafd29933f7aa4594def6ca4 /pkg/gui | |
parent | 2bf536265a8978b635330eeb61bc598d32fab62f (diff) |
better auto update logic
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/confirmation_panel.go | 1 | ||||
-rw-r--r-- | pkg/gui/gui.go | 42 | ||||
-rw-r--r-- | pkg/gui/status_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/updates.go | 57 |
4 files changed, 61 insertions, 43 deletions
diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 17ca62b01..1702b1df6 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -155,6 +155,7 @@ func (gui *Gui) createMessagePanel(g *gocui.Gui, currentView *gocui.View, title, } func (gui *Gui) createErrorPanel(g *gocui.Gui, message string) error { + gui.Log.Error(message) currentView := g.CurrentView() colorFunction := color.New(color.FgRed).SprintFunc() coloredMessage := colorFunction(strings.TrimSpace(message)) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 8783ecba2..48b806ab6 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -289,7 +289,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { // these are only called once (it's a place to put all the things you want // to happen on startup after the screen is first rendered) - gui.Updater.CheckForNewUpdate(gui.onUpdateCheckFinish, false) + gui.Updater.CheckForNewUpdate(gui.onBackgroundUpdateCheckFinish, false) gui.handleFileSelect(g, filesView) gui.refreshFiles(g) gui.refreshBranches(g) @@ -305,46 +305,6 @@ func (gui *Gui) layout(g *gocui.Gui) error { return nil } -func (gui *Gui) onUpdateFinish(err error) error { - gui.State.Updating = false - gui.statusManager.removeStatus("updating") - if err := gui.renderString(gui.g, "appStatus", ""); err != nil { - return err - } - if err != nil { - return gui.createErrorPanel(gui.g, "Update failed: "+err.Error()) - } - return nil -} - -func (gui *Gui) onUpdateCheckFinish(newVersion string, err error) error { - if err != nil { - // ignoring the error for now so that I'm not annoying users - gui.Log.Error(err.Error()) - return nil - } - if newVersion == "" { - return nil - } - if gui.Config.GetUserConfig().Get("update.method") == "background" { - gui.startUpdating(newVersion) - return nil - } - title := "New version available!" - message := "Download latest version? (enter/esc)" - currentView := gui.g.CurrentView() - return gui.createConfirmationPanel(gui.g, currentView, title, message, func(g *gocui.Gui, v *gocui.View) error { - gui.startUpdating(newVersion) - return nil - }, nil) -} - -func (gui *Gui) startUpdating(newVersion string) { - gui.State.Updating = true - gui.statusManager.addWaitingStatus("updating") - gui.Updater.Update(newVersion, gui.onUpdateFinish) -} - func (gui *Gui) fetch(g *gocui.Gui) error { gui.GitCommand.Fetch() gui.refreshStatus(g) diff --git a/pkg/gui/status_panel.go b/pkg/gui/status_panel.go index 52842246e..a9dee6a7f 100644 --- a/pkg/gui/status_panel.go +++ b/pkg/gui/status_panel.go @@ -50,8 +50,8 @@ func (gui *Gui) renderStatusOptions(g *gocui.Gui) error { } func (gui *Gui) handleCheckForUpdate(g *gocui.Gui, v *gocui.View) error { - gui.Updater.CheckForNewUpdate(gui.onUpdateCheckFinish, true) - return gui.createMessagePanel(gui.g, v, "Checking for updates", "Checking for updates...") + gui.Updater.CheckForNewUpdate(gui.onUserUpdateCheckFinish, true) + return gui.createMessagePanel(gui.g, v, "", gui.Tr.SLocalize("CheckingForUpdates")) } func (gui *Gui) handleStatusSelect(g *gocui.Gui, v *gocui.View) error { diff --git a/pkg/gui/updates.go b/pkg/gui/updates.go new file mode 100644 index 000000000..0065225fd --- /dev/null +++ b/pkg/gui/updates.go @@ -0,0 +1,57 @@ +package gui + +import "github.com/jesseduffield/gocui" + +func (gui *Gui) showUpdatePrompt(newVersion string) error { + title := "New version available!" + message := "Download latest version? (enter/esc)" + currentView := gui.g.CurrentView() + return gui.createConfirmationPanel(gui.g, currentView, title, message, func(g *gocui.Gui, v *gocui.View) error { + gui.startUpdating(newVersion) + return nil + }, nil) +} + +func (gui *Gui) onUserUpdateCheckFinish(newVersion string, err error) error { + if err != nil { + return gui.createErrorPanel(gui.g, err.Error()) + } + if newVersion == "" { + return gui.createErrorPanel(gui.g, "New version not found") + } + return gui.showUpdatePrompt(newVersion) +} + +func (gui *Gui) onBackgroundUpdateCheckFinish(newVersion string, err error) error { + if err != nil { + // ignoring the error for now so that I'm not annoying users + gui.Log.Error(err.Error()) + return nil + } + if newVersion == "" { + return nil + } + if gui.Config.GetUserConfig().Get("update.method") == "background" { + gui.startUpdating(newVersion) + return nil + } + return gui.showUpdatePrompt(newVersion) +} + +func (gui *Gui) startUpdating(newVersion string) { + gui.State.Updating = true + gui.statusManager.addWaitingStatus("updating") + gui.Updater.Update(newVersion, gui.onUpdateFinish) +} + +func (gui *Gui) onUpdateFinish(err error) error { + gui.State.Updating = false + gui.statusManager.removeStatus("updating") + if err := gui.renderString(gui.g, "appStatus", ""); err != nil { + return err + } + if err != nil { + return gui.createErrorPanel(gui.g, "Update failed: "+err.Error()) + } + return nil +} |