diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-12-30 23:24:24 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-04-30 13:19:52 +1000 |
commit | 8edad826caf2fa48bfad33f9f8c4f3ba49a052da (patch) | |
tree | 0b49145e4f656e72441199b5a5c30176c898d7a7 /pkg/gui/background.go | |
parent | 826128a8e03fb50f7287029ebac93c85712faecb (diff) |
Begin refactoring gui
This begins a big refactor of moving more code out of the Gui struct into contexts, controllers, and helpers. We also move some code into structs in the
gui package purely for the sake of better encapsulation
Diffstat (limited to 'pkg/gui/background.go')
-rw-r--r-- | pkg/gui/background.go | 59 |
1 files changed, 42 insertions, 17 deletions
diff --git a/pkg/gui/background.go b/pkg/gui/background.go index 6deac3c31..14ee70187 100644 --- a/pkg/gui/background.go +++ b/pkg/gui/background.go @@ -4,18 +4,33 @@ import ( "strings" "time" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" + "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" ) -func (gui *Gui) startBackgroundRoutines() { - userConfig := gui.UserConfig +type BackgroundRoutineMgr struct { + gui *Gui + + // if we've suspended the gui (e.g. because we've switched to a subprocess) + // we typically want to pause some things that are running like background + // file refreshes + pauseBackgroundThreads bool +} + +func (self *BackgroundRoutineMgr) PauseBackgroundThreads(pause bool) { + self.pauseBackgroundThreads = pause +} + +func (self *BackgroundRoutineMgr) startBackgroundRoutines() { + userConfig := self.gui.UserConfig if userConfig.Git.AutoFetch { fetchInterval := userConfig.Refresher.FetchInterval if fetchInterval > 0 { - go utils.Safe(gui.startBackgroundFetch) + go utils.Safe(self.startBackgroundFetch) } else { - gui.c.Log.Errorf( + self.gui.c.Log.Errorf( "Value of config option 'refresher.fetchInterval' (%d) is invalid, disabling auto-fetch", fetchInterval) } @@ -24,42 +39,44 @@ func (gui *Gui) startBackgroundRoutines() { if userConfig.Git.AutoRefresh { refreshInterval := userConfig.Refresher.RefreshInterval if refreshInterval > 0 { - gui.goEvery(time.Second*time.Duration(refreshInterval), gui.stopChan, gui.refreshFilesAndSubmodules) + self.goEvery(time.Second*time.Duration(refreshInterval), self.gui.stopChan, func() error { + return self.gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}}) + }) } else { - gui.c.Log.Errorf( + self.gui.c.Log.Errorf( "Value of config option 'refresher.refreshInterval' (%d) is invalid, disabling auto-refresh", refreshInterval) } } } -func (gui *Gui) startBackgroundFetch() { - gui.waitForIntro.Wait() - isNew := gui.IsNewRepo - userConfig := gui.UserConfig +func (self *BackgroundRoutineMgr) startBackgroundFetch() { + self.gui.waitForIntro.Wait() + isNew := self.gui.IsNewRepo + userConfig := self.gui.UserConfig if !isNew { time.After(time.Duration(userConfig.Refresher.FetchInterval) * time.Second) } - err := gui.backgroundFetch() + err := self.backgroundFetch() if err != nil && strings.Contains(err.Error(), "exit status 128") && isNew { - _ = gui.c.Alert(gui.c.Tr.NoAutomaticGitFetchTitle, gui.c.Tr.NoAutomaticGitFetchBody) + _ = self.gui.c.Alert(self.gui.c.Tr.NoAutomaticGitFetchTitle, self.gui.c.Tr.NoAutomaticGitFetchBody) } else { - gui.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), gui.stopChan, func() error { - err := gui.backgroundFetch() - gui.render() + self.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), self.gui.stopChan, func() error { + err := self.backgroundFetch() + self.gui.c.Render() return err }) } } -func (gui *Gui) goEvery(interval time.Duration, stop chan struct{}, function func() error) { +func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan struct{}, function func() error) { go utils.Safe(func() { ticker := time.NewTicker(interval) defer ticker.Stop() for { select { case <-ticker.C: - if gui.PauseBackgroundThreads { + if self.pauseBackgroundThreads { continue } _ = function() @@ -69,3 +86,11 @@ func (gui *Gui) goEvery(interval time.Duration, stop chan struct{}, function fun } }) } + +func (self *BackgroundRoutineMgr) backgroundFetch() (err error) { + err = self.gui.git.Sync.Fetch(git_commands.FetchOptions{Background: true}) + + _ = self.gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.COMMITS, types.REMOTES, types.TAGS}, Mode: types.ASYNC}) + + return err +} |