1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/popup/popup_handler.go

166 lines
4.2 KiB
Go
Raw Normal View History

2022-01-28 11:44:36 +02:00
package popup
import (
2023-01-03 16:07:16 +02:00
"context"
2022-01-28 11:44:36 +02:00
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/common"
2023-01-03 16:07:16 +02:00
gctx "github.com/jesseduffield/lazygit/pkg/gui/context"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sasha-s/go-deadlock"
2022-01-28 11:44:36 +02:00
)
2022-05-07 07:42:36 +02:00
type PopupHandler struct {
2022-01-28 11:44:36 +02:00
*common.Common
index int
deadlock.Mutex
2023-01-03 16:07:16 +02:00
createPopupPanelFn func(context.Context, types.CreatePopupPanelOpts) error
2022-01-28 11:44:36 +02:00
onErrorFn func() error
2022-08-01 13:58:49 +02:00
popContextFn func() error
currentContextFn func() types.Context
2022-01-29 10:09:20 +02:00
createMenuFn func(types.CreateMenuOptions) error
2023-03-23 09:47:29 +02:00
withWaitingStatusFn func(message string, f func() error)
toastFn func(message string)
getPromptInputFn func() string
2022-01-28 11:44:36 +02:00
}
2022-05-07 07:42:36 +02:00
var _ types.IPopupHandler = &PopupHandler{}
2022-01-28 11:44:36 +02:00
func NewPopupHandler(
common *common.Common,
2023-01-03 16:07:16 +02:00
createPopupPanelFn func(context.Context, types.CreatePopupPanelOpts) error,
2022-01-28 11:44:36 +02:00
onErrorFn func() error,
2022-08-01 13:58:49 +02:00
popContextFn func() error,
currentContextFn func() types.Context,
2022-01-29 10:09:20 +02:00
createMenuFn func(types.CreateMenuOptions) error,
2023-03-23 09:47:29 +02:00
withWaitingStatusFn func(message string, f func() error),
toastFn func(message string),
getPromptInputFn func() string,
2022-05-07 07:42:36 +02:00
) *PopupHandler {
return &PopupHandler{
2022-01-28 11:44:36 +02:00
Common: common,
index: 0,
createPopupPanelFn: createPopupPanelFn,
onErrorFn: onErrorFn,
2022-08-01 13:58:49 +02:00
popContextFn: popContextFn,
currentContextFn: currentContextFn,
2022-01-28 11:44:36 +02:00
createMenuFn: createMenuFn,
withWaitingStatusFn: withWaitingStatusFn,
toastFn: toastFn,
getPromptInputFn: getPromptInputFn,
2022-01-28 11:44:36 +02:00
}
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) Menu(opts types.CreateMenuOptions) error {
2022-01-28 11:44:36 +02:00
return self.createMenuFn(opts)
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) Toast(message string) {
self.toastFn(message)
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) WithWaitingStatus(message string, f func() error) error {
2023-03-23 09:47:29 +02:00
self.withWaitingStatusFn(message, f)
return nil
2022-01-28 11:44:36 +02:00
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) Error(err error) error {
2022-01-28 11:44:36 +02:00
if err == gocui.ErrQuit {
return err
}
return self.ErrorMsg(err.Error())
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) ErrorMsg(message string) error {
2022-01-28 11:44:36 +02:00
self.Lock()
self.index++
self.Unlock()
2022-04-16 07:52:27 +02: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 11:44:36 +02:00
if err := self.onErrorFn(); err != nil {
return err
}
return self.Alert(self.Tr.Error, coloredMessage)
2022-01-28 11:44:36 +02:00
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) Alert(title string, message string) error {
return self.Confirm(types.ConfirmOpts{Title: title, Prompt: message})
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) Confirm(opts types.ConfirmOpts) error {
2022-01-28 11:44:36 +02:00
self.Lock()
self.index++
self.Unlock()
2023-01-03 16:07:16 +02:00
return self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
2022-08-01 13:58:49 +02:00
Title: opts.Title,
Prompt: opts.Prompt,
HandleConfirm: opts.HandleConfirm,
HandleClose: opts.HandleClose,
2022-01-28 11:44:36 +02:00
})
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) Prompt(opts types.PromptOpts) error {
2022-01-28 11:44:36 +02:00
self.Lock()
self.index++
self.Unlock()
2023-01-03 16:07:16 +02:00
return self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
2022-01-28 11:44:36 +02:00
Title: opts.Title,
Prompt: opts.InitialContent,
Editable: true,
HandleConfirmPrompt: opts.HandleConfirm,
2022-02-23 10:44:48 +02:00
HandleClose: opts.HandleClose,
2022-01-28 11:44:36 +02:00
FindSuggestionsFunc: opts.FindSuggestionsFunc,
2022-02-23 10:44:48 +02:00
Mask: opts.Mask,
2022-01-28 11:44:36 +02:00
})
}
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) WithLoaderPanel(message string, f func() error) error {
2022-01-28 11:44:36 +02:00
index := 0
self.Lock()
self.index++
index = self.index
self.Unlock()
2023-01-03 16:07:16 +02:00
ctx, cancel := context.WithCancel(context.Background())
err := self.createPopupPanelFn(ctx, types.CreatePopupPanelOpts{
2022-01-28 11:44:36 +02:00
Prompt: message,
HasLoader: true,
})
if err != nil {
self.Log.Error(err)
2023-01-03 16:07:16 +02:00
cancel()
2022-01-28 11:44:36 +02:00
return nil
}
go utils.Safe(func() {
if err := f(); err != nil {
self.Log.Error(err)
}
2023-01-03 16:07:16 +02:00
cancel()
2022-01-28 11:44:36 +02:00
self.Lock()
2023-01-03 16:07:16 +02:00
if index == self.index && self.currentContextFn().GetKey() == gctx.CONFIRMATION_CONTEXT_KEY {
2022-08-01 13:58:49 +02:00
_ = self.popContextFn()
2022-01-28 11:44:36 +02:00
}
self.Unlock()
})
return nil
}
// returns the content that has currently been typed into the prompt. Useful for
// asynchronously updating the suggestions list under the prompt.
2022-05-07 07:42:36 +02:00
func (self *PopupHandler) GetPromptInput() string {
return self.getPromptInputFn()
}