2022-12-24 00:56:27 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-07-09 03:32:27 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2022-12-30 14:24:24 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2022-12-24 00:56:27 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
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
|
2023-07-08 07:23:15 +02:00
|
|
|
pauseBackgroundRefreshes bool
|
2022-12-30 14:24:24 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 07:23:15 +02:00
|
|
|
func (self *BackgroundRoutineMgr) PauseBackgroundRefreshes(pause bool) {
|
|
|
|
self.pauseBackgroundRefreshes = pause
|
2022-12-30 14:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BackgroundRoutineMgr) startBackgroundRoutines() {
|
|
|
|
userConfig := self.gui.UserConfig
|
2022-12-24 00:56:27 +02:00
|
|
|
|
|
|
|
if userConfig.Git.AutoFetch {
|
|
|
|
fetchInterval := userConfig.Refresher.FetchInterval
|
|
|
|
if fetchInterval > 0 {
|
2022-12-30 14:24:24 +02:00
|
|
|
go utils.Safe(self.startBackgroundFetch)
|
2022-12-24 00:56:27 +02:00
|
|
|
} else {
|
2022-12-30 14:24:24 +02:00
|
|
|
self.gui.c.Log.Errorf(
|
2022-12-24 00:56:27 +02:00
|
|
|
"Value of config option 'refresher.fetchInterval' (%d) is invalid, disabling auto-fetch",
|
|
|
|
fetchInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userConfig.Git.AutoRefresh {
|
|
|
|
refreshInterval := userConfig.Refresher.RefreshInterval
|
|
|
|
if refreshInterval > 0 {
|
2023-07-08 07:23:15 +02:00
|
|
|
go utils.Safe(func() { self.startBackgroundFilesRefresh(refreshInterval) })
|
2022-12-24 00:56:27 +02:00
|
|
|
} else {
|
2022-12-30 14:24:24 +02:00
|
|
|
self.gui.c.Log.Errorf(
|
2022-12-24 00:56:27 +02:00
|
|
|
"Value of config option 'refresher.refreshInterval' (%d) is invalid, disabling auto-refresh",
|
|
|
|
refreshInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
func (self *BackgroundRoutineMgr) startBackgroundFetch() {
|
|
|
|
self.gui.waitForIntro.Wait()
|
2023-07-08 07:23:15 +02:00
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
isNew := self.gui.IsNewRepo
|
|
|
|
userConfig := self.gui.UserConfig
|
2022-12-24 00:56:27 +02:00
|
|
|
if !isNew {
|
|
|
|
time.After(time.Duration(userConfig.Refresher.FetchInterval) * time.Second)
|
|
|
|
}
|
2022-12-30 14:24:24 +02:00
|
|
|
err := self.backgroundFetch()
|
2022-12-24 00:56:27 +02:00
|
|
|
if err != nil && strings.Contains(err.Error(), "exit status 128") && isNew {
|
2022-12-30 14:24:24 +02:00
|
|
|
_ = self.gui.c.Alert(self.gui.c.Tr.NoAutomaticGitFetchTitle, self.gui.c.Tr.NoAutomaticGitFetchBody)
|
2022-12-24 00:56:27 +02:00
|
|
|
} else {
|
2022-12-30 14:24:24 +02:00
|
|
|
self.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), self.gui.stopChan, func() error {
|
|
|
|
err := self.backgroundFetch()
|
|
|
|
self.gui.c.Render()
|
2022-12-24 00:56:27 +02:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 07:23:15 +02:00
|
|
|
func (self *BackgroundRoutineMgr) startBackgroundFilesRefresh(refreshInterval int) {
|
|
|
|
self.gui.waitForIntro.Wait()
|
|
|
|
|
|
|
|
self.goEvery(time.Second*time.Duration(refreshInterval), self.gui.stopChan, func() error {
|
|
|
|
return self.gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES}})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan struct{}, function func() error) {
|
2023-07-10 09:30:44 +02:00
|
|
|
done := make(chan struct{})
|
2022-12-24 00:56:27 +02:00
|
|
|
go utils.Safe(func() {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2023-07-08 07:23:15 +02:00
|
|
|
if self.pauseBackgroundRefreshes {
|
2022-12-24 00:56:27 +02:00
|
|
|
continue
|
|
|
|
}
|
2023-07-10 09:30:44 +02:00
|
|
|
self.gui.c.OnWorker(func(gocui.Task) {
|
|
|
|
_ = function()
|
|
|
|
done <- struct{}{}
|
|
|
|
})
|
|
|
|
// waiting so that we don't bunch up refreshes if the refresh takes longer than the interval
|
|
|
|
<-done
|
2022-12-24 00:56:27 +02:00
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-12-30 14:24:24 +02:00
|
|
|
|
|
|
|
func (self *BackgroundRoutineMgr) backgroundFetch() (err error) {
|
2023-07-09 03:32:27 +02:00
|
|
|
err = self.gui.git.Sync.FetchBackground()
|
2022-12-30 14:24:24 +02:00
|
|
|
|
|
|
|
_ = self.gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.COMMITS, types.REMOTES, types.TAGS}, Mode: types.ASYNC})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|