1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-19 19:44:17 +02:00
lazygit/pkg/gui/popup/popup_handler.go

147 lines
4.3 KiB
Go
Raw Normal View History

2022-01-28 20:44:36 +11:00
package popup
import (
2023-01-03 23:07:16 +09:00
"context"
2022-01-28 20:44:36 +11:00
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/sasha-s/go-deadlock"
2022-01-28 20:44:36 +11:00
)
2022-05-07 15:42:36 +10:00
type PopupHandler struct {
2022-01-28 20:44:36 +11:00
*common.Common
index int
deadlock.Mutex
2023-08-27 16:29:14 +02:00
createPopupPanelFn func(context.Context, types.CreatePopupPanelOpts) error
onErrorFn func() error
popContextFn func() error
currentContextFn func() types.Context
createMenuFn func(types.CreateMenuOptions) error
withWaitingStatusFn func(message string, f func(gocui.Task) error)
withWaitingStatusSyncFn func(message string, f func() error)
2023-12-22 17:31:13 +01:00
toastFn func(message string, kind types.ToastKind)
2023-08-27 16:29:14 +02:00
getPromptInputFn func() string
inDemo func() bool
2022-01-28 20:44:36 +11:00
}
2022-05-07 15:42:36 +10:00
var _ types.IPopupHandler = &PopupHandler{}
2022-01-28 20:44:36 +11:00
func NewPopupHandler(
common *common.Common,
2023-01-03 23:07:16 +09:00
createPopupPanelFn func(context.Context, types.CreatePopupPanelOpts) error,
2022-01-28 20:44:36 +11:00
onErrorFn func() error,
2022-08-01 21:58:49 +10:00
popContextFn func() error,
currentContextFn func() types.Context,
2022-01-29 19:09:20 +11:00
createMenuFn func(types.CreateMenuOptions) error,
withWaitingStatusFn func(message string, f func(gocui.Task) error),
2023-08-27 16:29:14 +02:00
withWaitingStatusSyncFn func(message string, f func() error),
2023-12-22 17:31:13 +01:00
toastFn func(message string, kind types.ToastKind),
getPromptInputFn func() string,
inDemo func() bool,
2022-05-07 15:42:36 +10:00
) *PopupHandler {
return &PopupHandler{
2023-08-27 16:29:14 +02:00
Common: common,
index: 0,
createPopupPanelFn: createPopupPanelFn,
onErrorFn: onErrorFn,
popContextFn: popContextFn,
currentContextFn: currentContextFn,
createMenuFn: createMenuFn,
withWaitingStatusFn: withWaitingStatusFn,
withWaitingStatusSyncFn: withWaitingStatusSyncFn,
toastFn: toastFn,
getPromptInputFn: getPromptInputFn,
inDemo: inDemo,
2022-01-28 20:44:36 +11:00
}
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) Menu(opts types.CreateMenuOptions) error {
2022-01-28 20:44:36 +11:00
return self.createMenuFn(opts)
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) Toast(message string) {
2023-12-22 17:31:13 +01:00
self.toastFn(message, types.ToastKindStatus)
}
2023-12-22 17:31:13 +01:00
func (self *PopupHandler) ErrorToast(message string) {
self.toastFn(message, types.ToastKindError)
}
func (self *PopupHandler) SetToastFunc(f func(string, types.ToastKind)) {
self.toastFn = f
}
func (self *PopupHandler) WithWaitingStatus(message string, f func(gocui.Task) error) error {
2023-03-23 18:47:29 +11:00
self.withWaitingStatusFn(message, f)
return nil
2022-01-28 20:44:36 +11:00
}
2023-08-27 16:29:14 +02:00
func (self *PopupHandler) WithWaitingStatusSync(message string, f func() error) error {
self.withWaitingStatusSyncFn(message, f)
return nil
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) Error(err error) error {
2022-01-28 20:44:36 +11:00
if err == gocui.ErrQuit {
return err
}
return self.ErrorMsg(err.Error())
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) ErrorMsg(message string) error {
2022-01-28 20:44:36 +11:00
self.Lock()
self.index++
self.Unlock()
2022-04-16 15:52:27 +10:00
// Need to set bold here explicitly; otherwise it gets cancelled by the red colouring.
coloredMessage := style.FgRed.SetBold().Sprint(strings.TrimSpace(message))
2022-01-28 20:44:36 +11:00
if err := self.onErrorFn(); err != nil {
return err
}
return self.Alert(self.Tr.Error, coloredMessage)
2022-01-28 20:44:36 +11:00
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) Alert(title string, message string) error {
return self.Confirm(types.ConfirmOpts{Title: title, Prompt: message})
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) Confirm(opts types.ConfirmOpts) error {
2022-01-28 20:44:36 +11:00
self.Lock()
self.index++
self.Unlock()
2023-01-03 23:07:16 +09:00
return self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
2022-08-01 21:58:49 +10:00
Title: opts.Title,
Prompt: opts.Prompt,
HandleConfirm: opts.HandleConfirm,
HandleClose: opts.HandleClose,
2022-01-28 20:44:36 +11:00
})
}
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) Prompt(opts types.PromptOpts) error {
2022-01-28 20:44:36 +11:00
self.Lock()
self.index++
self.Unlock()
2023-01-03 23:07:16 +09:00
return self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
2022-01-28 20:44:36 +11:00
Title: opts.Title,
Prompt: opts.InitialContent,
Editable: true,
HandleConfirmPrompt: opts.HandleConfirm,
2022-02-23 19:44:48 +11:00
HandleClose: opts.HandleClose,
2022-01-28 20:44:36 +11:00
FindSuggestionsFunc: opts.FindSuggestionsFunc,
2022-02-23 19:44:48 +11:00
Mask: opts.Mask,
2022-01-28 20:44:36 +11:00
})
}
// returns the content that has currently been typed into the prompt. Useful for
// asynchronously updating the suggestions list under the prompt.
2022-05-07 15:42:36 +10:00
func (self *PopupHandler) GetPromptInput() string {
return self.getPromptInputFn()
}