1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-16 11:37:01 +02:00
lazygit/pkg/gui/popup/fake_popup_handler.go

55 lines
1.3 KiB
Go
Raw Normal View History

2022-05-07 07:42:36 +02:00
package popup
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 03:32:27 +02:00
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
2022-05-07 07:42:36 +02:00
type FakePopupHandler struct {
OnErrorMsg func(message string) error
OnConfirm func(opts types.ConfirmOpts) error
OnPrompt func(opts types.PromptOpts) error
}
var _ types.IPopupHandler = &FakePopupHandler{}
func (self *FakePopupHandler) Error(err error) error {
return self.ErrorMsg(err.Error())
}
func (self *FakePopupHandler) ErrorMsg(message string) error {
return self.OnErrorMsg(message)
}
func (self *FakePopupHandler) Alert(title string, message string) error {
panic("not yet implemented")
}
func (self *FakePopupHandler) Confirm(opts types.ConfirmOpts) error {
2022-05-18 13:33:35 +02:00
return self.OnConfirm(opts)
2022-05-07 07:42:36 +02:00
}
func (self *FakePopupHandler) Prompt(opts types.PromptOpts) error {
return self.OnPrompt(opts)
}
func (self *FakePopupHandler) WithLoaderPanel(message string, f func(gocui.Task) error) error {
return f(gocui.NewFakeTask())
2022-05-07 07:42:36 +02:00
}
func (self *FakePopupHandler) WithWaitingStatus(message string, f func(gocui.Task) error) error {
return f(gocui.NewFakeTask())
2022-05-07 07:42:36 +02:00
}
func (self *FakePopupHandler) Menu(opts types.CreateMenuOptions) error {
panic("not yet implemented")
}
func (self *FakePopupHandler) Toast(message string) {
panic("not yet implemented")
}
func (self *FakePopupHandler) GetPromptInput() string {
panic("not yet implemented")
}