mirror of
				https://github.com/jesseduffield/lazygit.git
				synced 2025-10-30 23:57:43 +02:00 
			
		
		
		
	Add ErrorToast function
This commit is contained in:
		| @@ -5,6 +5,7 @@ import ( | ||||
|  | ||||
| 	"github.com/jesseduffield/gocui" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/gui/status" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/gui/types" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/utils" | ||||
| ) | ||||
|  | ||||
| @@ -23,7 +24,7 @@ func NewAppStatusHelper(c *HelperCommon, statusMgr func() *status.StatusManager, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (self *AppStatusHelper) Toast(message string) { | ||||
| func (self *AppStatusHelper) Toast(message string, kind types.ToastKind) { | ||||
| 	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 | ||||
| @@ -31,7 +32,7 @@ func (self *AppStatusHelper) Toast(message string) { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	self.statusMgr().AddToastStatus(message) | ||||
| 	self.statusMgr().AddToastStatus(message, kind) | ||||
|  | ||||
| 	self.renderAppStatus() | ||||
| } | ||||
| @@ -87,7 +88,8 @@ func (self *AppStatusHelper) HasStatus() bool { | ||||
| } | ||||
|  | ||||
| func (self *AppStatusHelper) GetStatusString() string { | ||||
| 	return self.statusMgr().GetStatusString() | ||||
| 	appStatus, _ := self.statusMgr().GetStatusString() | ||||
| 	return appStatus | ||||
| } | ||||
|  | ||||
| func (self *AppStatusHelper) renderAppStatus() { | ||||
| @@ -95,7 +97,8 @@ func (self *AppStatusHelper) renderAppStatus() { | ||||
| 		ticker := time.NewTicker(time.Millisecond * utils.LoaderAnimationInterval) | ||||
| 		defer ticker.Stop() | ||||
| 		for range ticker.C { | ||||
| 			appStatus := self.statusMgr().GetStatusString() | ||||
| 			appStatus, color := self.statusMgr().GetStatusString() | ||||
| 			self.c.Views().AppStatus.FgColor = color | ||||
| 			self.c.OnUIThread(func() error { | ||||
| 				self.c.SetViewContent(self.c.Views().AppStatus, appStatus) | ||||
| 				return nil | ||||
| @@ -127,7 +130,8 @@ func (self *AppStatusHelper) renderAppStatusSync(stop chan struct{}) { | ||||
| 		for { | ||||
| 			select { | ||||
| 			case <-ticker.C: | ||||
| 				appStatus := self.statusMgr().GetStatusString() | ||||
| 				appStatus, color := self.statusMgr().GetStatusString() | ||||
| 				self.c.Views().AppStatus.FgColor = color | ||||
| 				self.c.SetViewContent(self.c.Views().AppStatus, appStatus) | ||||
| 				// Redraw all views of the bottom line: | ||||
| 				bottomLineViews := []*gocui.View{ | ||||
|   | ||||
| @@ -517,7 +517,7 @@ func NewGui( | ||||
| 		func(message string, f func() error) { | ||||
| 			gui.helpers.AppStatus.WithWaitingStatusSync(message, f) | ||||
| 		}, | ||||
| 		func(message string) { gui.helpers.AppStatus.Toast(message) }, | ||||
| 		func(message string, kind types.ToastKind) { gui.helpers.AppStatus.Toast(message, kind) }, | ||||
| 		func() string { return gui.Views.Confirmation.TextArea.GetContent() }, | ||||
| 		func() bool { return gui.c.InDemo() }, | ||||
| 	) | ||||
|   | ||||
| @@ -22,7 +22,7 @@ type PopupHandler struct { | ||||
| 	createMenuFn            func(types.CreateMenuOptions) error | ||||
| 	withWaitingStatusFn     func(message string, f func(gocui.Task) error) | ||||
| 	withWaitingStatusSyncFn func(message string, f func() error) | ||||
| 	toastFn                 func(message string) | ||||
| 	toastFn                 func(message string, kind types.ToastKind) | ||||
| 	getPromptInputFn        func() string | ||||
| 	inDemo                  func() bool | ||||
| } | ||||
| @@ -38,7 +38,7 @@ func NewPopupHandler( | ||||
| 	createMenuFn func(types.CreateMenuOptions) error, | ||||
| 	withWaitingStatusFn func(message string, f func(gocui.Task) error), | ||||
| 	withWaitingStatusSyncFn func(message string, f func() error), | ||||
| 	toastFn func(message string), | ||||
| 	toastFn func(message string, kind types.ToastKind), | ||||
| 	getPromptInputFn func() string, | ||||
| 	inDemo func() bool, | ||||
| ) *PopupHandler { | ||||
| @@ -63,10 +63,14 @@ func (self *PopupHandler) Menu(opts types.CreateMenuOptions) error { | ||||
| } | ||||
|  | ||||
| func (self *PopupHandler) Toast(message string) { | ||||
| 	self.toastFn(message) | ||||
| 	self.toastFn(message, types.ToastKindStatus) | ||||
| } | ||||
|  | ||||
| func (self *PopupHandler) SetToastFunc(f func(string)) { | ||||
| func (self *PopupHandler) ErrorToast(message string) { | ||||
| 	self.toastFn(message, types.ToastKindError) | ||||
| } | ||||
|  | ||||
| func (self *PopupHandler) SetToastFunc(f func(string, types.ToastKind)) { | ||||
| 	self.toastFn = f | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -3,6 +3,8 @@ package status | ||||
| import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/jesseduffield/gocui" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/gui/types" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/utils" | ||||
| 	"github.com/samber/lo" | ||||
| 	"github.com/sasha-s/go-deadlock" | ||||
| @@ -26,7 +28,7 @@ type WaitingStatusHandle struct { | ||||
| } | ||||
|  | ||||
| func (self *WaitingStatusHandle) Show() { | ||||
| 	self.id = self.statusManager.addStatus(self.message, "waiting") | ||||
| 	self.id = self.statusManager.addStatus(self.message, "waiting", types.ToastKindStatus) | ||||
| 	self.renderFunc() | ||||
| } | ||||
|  | ||||
| @@ -37,6 +39,7 @@ func (self *WaitingStatusHandle) Hide() { | ||||
| type appStatus struct { | ||||
| 	message    string | ||||
| 	statusType string | ||||
| 	color      gocui.Attribute | ||||
| 	id         int | ||||
| } | ||||
|  | ||||
| @@ -53,8 +56,8 @@ func (self *StatusManager) WithWaitingStatus(message string, renderFunc func(), | ||||
| 	handle.Hide() | ||||
| } | ||||
|  | ||||
| func (self *StatusManager) AddToastStatus(message string) int { | ||||
| 	id := self.addStatus(message, "toast") | ||||
| func (self *StatusManager) AddToastStatus(message string, kind types.ToastKind) int { | ||||
| 	id := self.addStatus(message, "toast", kind) | ||||
|  | ||||
| 	go func() { | ||||
| 		time.Sleep(time.Second * 2) | ||||
| @@ -65,31 +68,37 @@ func (self *StatusManager) AddToastStatus(message string) int { | ||||
| 	return id | ||||
| } | ||||
|  | ||||
| func (self *StatusManager) GetStatusString() string { | ||||
| func (self *StatusManager) GetStatusString() (string, gocui.Attribute) { | ||||
| 	if len(self.statuses) == 0 { | ||||
| 		return "" | ||||
| 		return "", gocui.ColorDefault | ||||
| 	} | ||||
| 	topStatus := self.statuses[0] | ||||
| 	if topStatus.statusType == "waiting" { | ||||
| 		return topStatus.message + " " + utils.Loader(time.Now()) | ||||
| 		return topStatus.message + " " + utils.Loader(time.Now()), topStatus.color | ||||
| 	} | ||||
| 	return topStatus.message | ||||
| 	return topStatus.message, topStatus.color | ||||
| } | ||||
|  | ||||
| func (self *StatusManager) HasStatus() bool { | ||||
| 	return len(self.statuses) > 0 | ||||
| } | ||||
|  | ||||
| func (self *StatusManager) addStatus(message string, statusType string) int { | ||||
| 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...) | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import ( | ||||
|  | ||||
| 	"github.com/jesseduffield/gocui" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/gui/popup" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/gui/types" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/integration/components" | ||||
| 	"github.com/jesseduffield/lazygit/pkg/utils" | ||||
| ) | ||||
| @@ -35,7 +36,7 @@ func (gui *Gui) handleTestMode() { | ||||
|  | ||||
| 			toastChan := make(chan string, 100) | ||||
| 			gui.PopupHandler.(*popup.PopupHandler).SetToastFunc( | ||||
| 				func(message string) { toastChan <- message }) | ||||
| 				func(message string, kind types.ToastKind) { toastChan <- message }) | ||||
|  | ||||
| 			test.Run(&GuiDriver{gui: gui, isIdleChan: isIdleChan, toastChan: toastChan}) | ||||
|  | ||||
|   | ||||
| @@ -144,10 +144,18 @@ type IPopupHandler interface { | ||||
| 	WithWaitingStatusSync(message string, f func() error) error | ||||
| 	Menu(opts CreateMenuOptions) error | ||||
| 	Toast(message string) | ||||
| 	SetToastFunc(func(string)) | ||||
| 	ErrorToast(message string) | ||||
| 	SetToastFunc(func(string, ToastKind)) | ||||
| 	GetPromptInput() string | ||||
| } | ||||
|  | ||||
| type ToastKind int | ||||
|  | ||||
| const ( | ||||
| 	ToastKindStatus ToastKind = iota | ||||
| 	ToastKindError | ||||
| ) | ||||
|  | ||||
| type CreateMenuOptions struct { | ||||
| 	Title           string | ||||
| 	Items           []*MenuItem | ||||
|   | ||||
		Reference in New Issue
	
	Block a user