Skip to content

Commit

Permalink
Add ErrorToast function
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Jan 14, 2024
1 parent 8ca8a43 commit 99a3ccd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
14 changes: 9 additions & 5 deletions pkg/gui/controllers/helpers/app_status_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -23,15 +24,15 @@ 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
// two seconds.
return
}

self.statusMgr().AddToastStatus(message)
self.statusMgr().AddToastStatus(message, kind)

self.renderAppStatus()
}
Expand Down Expand Up @@ -87,15 +88,17 @@ func (self *AppStatusHelper) HasStatus() bool {
}

func (self *AppStatusHelper) GetStatusString() string {
return self.statusMgr().GetStatusString()
appStatus, _ := self.statusMgr().GetStatusString()
return appStatus
}

func (self *AppStatusHelper) renderAppStatus() {
self.c.OnWorker(func(_ gocui.Task) {
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
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
)
Expand Down
12 changes: 8 additions & 4 deletions pkg/gui/popup/popup_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
25 changes: 17 additions & 8 deletions pkg/gui/status/status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}

Expand All @@ -37,6 +39,7 @@ func (self *WaitingStatusHandle) Hide() {
type appStatus struct {
message string
statusType string
color gocui.Attribute
id int
}

Expand All @@ -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)
Expand All @@ -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...)
Expand Down
3 changes: 2 additions & 1 deletion pkg/gui/test_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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})

Expand Down
10 changes: 9 additions & 1 deletion pkg/gui/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 99a3ccd

Please sign in to comment.