2023-03-23 18:47:29 +11:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-07-09 11:32:27 +10:00
|
|
|
"github.com/jesseduffield/gocui"
|
2023-03-23 18:47:29 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/status"
|
2023-08-29 14:15:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2023-03-23 18:47:29 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppStatusHelper struct {
|
|
|
|
c *HelperCommon
|
|
|
|
|
|
|
|
statusMgr func() *status.StatusManager
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAppStatusHelper(c *HelperCommon, statusMgr func() *status.StatusManager) *AppStatusHelper {
|
|
|
|
return &AppStatusHelper{
|
|
|
|
c: c,
|
|
|
|
statusMgr: statusMgr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *AppStatusHelper) Toast(message string) {
|
2023-08-30 10:54:32 +02:00
|
|
|
if self.c.RunningIntegrationTest() {
|
|
|
|
// Don't bother showing toasts in integration tests. You can't check for
|
|
|
|
// them anyway, and they would only slow down the test unnecessarily by
|
|
|
|
// two seconds.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:47:29 +11:00
|
|
|
self.statusMgr().AddToastStatus(message)
|
|
|
|
|
|
|
|
self.renderAppStatus()
|
|
|
|
}
|
|
|
|
|
2023-09-20 08:43:42 +02:00
|
|
|
// A custom task for WithWaitingStatus calls; it wraps the original one and
|
|
|
|
// hides the status whenever the task is paused, and shows it again when
|
|
|
|
// continued.
|
|
|
|
type appStatusHelperTask struct {
|
|
|
|
gocui.Task
|
|
|
|
waitingStatusHandle *status.WaitingStatusHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
// poor man's version of explicitly saying that struct X implements interface Y
|
|
|
|
var _ gocui.Task = appStatusHelperTask{}
|
|
|
|
|
|
|
|
func (self appStatusHelperTask) Pause() {
|
|
|
|
self.waitingStatusHandle.Hide()
|
|
|
|
self.Task.Pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self appStatusHelperTask) Continue() {
|
|
|
|
self.Task.Continue()
|
|
|
|
self.waitingStatusHandle.Show()
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:47:29 +11:00
|
|
|
// withWaitingStatus wraps a function and shows a waiting status while the function is still executing
|
2023-07-09 21:09:52 +10:00
|
|
|
func (self *AppStatusHelper) WithWaitingStatus(message string, f func(gocui.Task) error) {
|
|
|
|
self.c.OnWorker(func(task gocui.Task) {
|
2023-09-20 08:43:42 +02:00
|
|
|
self.statusMgr().WithWaitingStatus(message, self.renderAppStatus, func(waitingStatusHandle *status.WaitingStatusHandle) {
|
|
|
|
if err := f(appStatusHelperTask{task, waitingStatusHandle}); err != nil {
|
2023-03-23 18:47:29 +11:00
|
|
|
self.c.OnUIThread(func() error {
|
|
|
|
return self.c.Error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *AppStatusHelper) HasStatus() bool {
|
|
|
|
return self.statusMgr().HasStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *AppStatusHelper) GetStatusString() string {
|
|
|
|
return self.statusMgr().GetStatusString()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *AppStatusHelper) renderAppStatus() {
|
2023-07-09 21:09:52 +10:00
|
|
|
self.c.OnWorker(func(_ gocui.Task) {
|
2023-08-29 14:15:36 +02:00
|
|
|
ticker := time.NewTicker(time.Millisecond * utils.LoaderAnimationInterval)
|
2023-03-23 18:47:29 +11:00
|
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
|
|
|
appStatus := self.statusMgr().GetStatusString()
|
|
|
|
self.c.OnUIThread(func() error {
|
|
|
|
self.c.SetViewContent(self.c.Views().AppStatus, appStatus)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if appStatus == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|