summaryrefslogtreecommitdiffstats
path: root/pkg/gui/status/status_manager.go
blob: 5cee4edc284e9596fba1a026ca984537873bc4d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package status

import (
	"time"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/config"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
	"github.com/jesseduffield/lazygit/pkg/utils"
	"github.com/samber/lo"
	"github.com/sasha-s/go-deadlock"
)

// StatusManager's job is to handle queuing of loading states and toast notifications
// that you see at the bottom left of the screen.
type StatusManager struct {
	statuses []appStatus
	nextId   int
	mutex    deadlock.Mutex
}

// Can be used to manipulate a waiting status while it is running (e.g. pause
// and resume it)
type WaitingStatusHandle struct {
	statusManager *StatusManager
	message       string
	renderFunc    func()
	id            int
}

func (self *WaitingStatusHandle) Show() {
	self.id = self.statusManager.addStatus(self.message, "waiting", types.ToastKindStatus)
	self.renderFunc()
}

func (self *WaitingStatusHandle) Hide() {
	self.statusManager.removeStatus(self.id)
}

type appStatus struct {
	message    string
	statusType string
	color      gocui.Attribute
	id         int
}

func NewStatusManager() *StatusManager {
	return &StatusManager{}
}

func (self *StatusManager) WithWaitingStatus(message string, renderFunc func(), f func(*WaitingStatusHandle) error) error {
	handle := &WaitingStatusHandle{statusManager: self, message: message, renderFunc: renderFunc, id: -1}
	handle.Show()
	defer handle.Hide()

	return f(handle)
}

func (self *StatusManager) AddToastStatus(message string, kind types.ToastKind) int {
	id := self.addStatus(message, "toast", kind)

	go func() {
		delay := lo.Ternary(kind == types.ToastKindError, time.Second*4, time.Second*2)
		time.Sleep(delay)

		self.removeStatus(id)
	}()

	return id
}

func (self *StatusManager) GetStatusString(userConfig *config.UserConfig) (string, gocui.Attribute) {
	if len(self.statuses) == 0 {
		return "", gocui.ColorDefault
	}
	topStatus := self.statuses[0]
	if topStatus.statusType == "waiting" {
		return topStatus.message + " " + utils.Loader(time.Now(), userConfig.Gui.Spinner), topStatus.color
	}
	return topStatus.message, topStatus.color
}

func (self *StatusManager) HasStatus() bool {
	return len(self.statuses) > 0
}

func (self *StatusManager) addStatus(message string, statusType string, kind types.ToastKind) int {
	self.mutex.Lock()
	defer self.mutex.Unlock()

	self.nextId++
	id := self.nextId

	color := gocui.ColorCyan
	if kind == types.ToastKindError {
		color = gocui.ColorRed
	}

	newStatus := appStatus{
		message:    message,
		statusType: statusType,
		color:      color,
		id:         id,
	}
	self.statuses = append([]appStatus{newStatus}, self.statuses...)

	return id
}

func (self *StatusManager) removeStatus(id int) {
	self.mutex.Lock()
	defer self.mutex.Unlock()

	self.statuses = lo.Filter(self.statuses, func(status appStatus, _ int) bool {
		return status.id != id
	})
}