1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/app_status_manager.go

128 lines
2.4 KiB
Go
Raw Normal View History

2018-08-25 07:55:49 +02:00
package gui
import (
2020-11-18 00:08:32 +02:00
"sync"
"time"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/utils"
)
2018-08-25 07:55:49 +02:00
type appStatus struct {
2020-11-18 00:08:32 +02:00
message string
2018-08-25 07:55:49 +02:00
statusType string
2020-11-18 00:08:32 +02:00
id int
2018-08-25 07:55:49 +02:00
}
type statusManager struct {
statuses []appStatus
2020-11-18 00:08:32 +02:00
nextId int
mutex sync.Mutex
2018-08-25 07:55:49 +02:00
}
2020-11-18 00:08:32 +02:00
func (m *statusManager) removeStatus(id int) {
m.mutex.Lock()
defer m.mutex.Unlock()
2018-08-25 07:55:49 +02:00
newStatuses := []appStatus{}
for _, status := range m.statuses {
2020-11-18 00:08:32 +02:00
if status.id != id {
2018-08-25 07:55:49 +02:00
newStatuses = append(newStatuses, status)
}
}
m.statuses = newStatuses
}
2020-11-18 00:08:32 +02:00
func (m *statusManager) addWaitingStatus(message string) int {
m.mutex.Lock()
defer m.mutex.Unlock()
m.nextId += 1
id := m.nextId
2018-08-25 07:55:49 +02:00
newStatus := appStatus{
2020-11-18 00:08:32 +02:00
message: message,
2018-08-25 07:55:49 +02:00
statusType: "waiting",
2020-11-18 00:08:32 +02:00
id: id,
}
m.statuses = append([]appStatus{newStatus}, m.statuses...)
return id
}
func (m *statusManager) addToastStatus(message string) int {
m.mutex.Lock()
defer m.mutex.Unlock()
m.nextId++
id := m.nextId
newStatus := appStatus{
message: message,
statusType: "toast",
id: id,
2018-08-25 07:55:49 +02:00
}
m.statuses = append([]appStatus{newStatus}, m.statuses...)
2020-11-18 00:08:32 +02:00
go func() {
time.Sleep(time.Second * 2)
m.removeStatus(id)
}()
return id
2018-08-25 07:55:49 +02:00
}
func (m *statusManager) getStatusString() string {
if len(m.statuses) == 0 {
return ""
}
topStatus := m.statuses[0]
if topStatus.statusType == "waiting" {
2020-11-18 00:08:32 +02:00
return topStatus.message + " " + utils.Loader()
2018-08-25 07:55:49 +02:00
}
2020-11-18 00:08:32 +02:00
return topStatus.message
}
func (gui *Gui) raiseToast(message string) {
2020-11-18 00:08:32 +02:00
gui.statusManager.addToastStatus(message)
gui.renderAppStatus()
}
func (gui *Gui) renderAppStatus() {
go utils.Safe(func() {
ticker := time.NewTicker(time.Millisecond * 50)
defer ticker.Stop()
for range ticker.C {
appStatus := gui.statusManager.getStatusString()
if appStatus == "" {
2021-04-04 16:31:52 +02:00
gui.renderString(gui.Views.AppStatus, "")
2020-11-18 00:08:32 +02:00
return
}
2021-04-04 16:31:52 +02:00
gui.renderString(gui.Views.AppStatus, appStatus)
2020-11-18 00:08:32 +02:00
}
})
2018-08-25 07:55:49 +02:00
}
// WithWaitingStatus wraps a function and shows a waiting status while the function is still executing
2020-11-18 00:08:32 +02:00
func (gui *Gui) WithWaitingStatus(message string, f func() error) error {
2020-10-07 12:19:38 +02:00
go utils.Safe(func() {
2020-11-18 00:08:32 +02:00
id := gui.statusManager.addWaitingStatus(message)
defer func() {
2020-11-18 00:08:32 +02:00
gui.statusManager.removeStatus(id)
}()
2020-11-18 00:08:32 +02:00
gui.renderAppStatus()
if err := f(); err != nil {
gui.g.Update(func(g *gocui.Gui) error {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
})
}
2020-10-07 12:19:38 +02:00
})
return nil
}