mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-07 01:09:45 +02:00
Add convenience function ConfirmIf
It's a very common pattern in the code base to have some code that we want to run either directly, or with a confirmation, depending on some condition. In most cases this is solved by creating a local helper function that we call either directly or from within the HandleConfirm of the confirmation; provide a convenience helper that makes this easier.
This commit is contained in:
@ -115,6 +115,20 @@ func (self *PopupHandler) Confirm(opts types.ConfirmOpts) {
|
||||
})
|
||||
}
|
||||
|
||||
func (self *PopupHandler) ConfirmIf(condition bool, opts types.ConfirmOpts) error {
|
||||
if condition {
|
||||
self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
|
||||
Title: opts.Title,
|
||||
Prompt: opts.Prompt,
|
||||
HandleConfirm: opts.HandleConfirm,
|
||||
HandleClose: opts.HandleClose,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
return opts.HandleConfirm()
|
||||
}
|
||||
|
||||
func (self *PopupHandler) Prompt(opts types.PromptOpts) {
|
||||
self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
|
||||
Title: opts.Title,
|
||||
|
@ -126,6 +126,8 @@ type IPopupHandler interface {
|
||||
Alert(title string, message string)
|
||||
// Shows a popup asking the user for confirmation.
|
||||
Confirm(opts ConfirmOpts)
|
||||
// Shows a popup asking the user for confirmation if condition is true; otherwise, the HandleConfirm function is called directly.
|
||||
ConfirmIf(condition bool, opts ConfirmOpts) error
|
||||
// Shows a popup prompting the user for input.
|
||||
Prompt(opts PromptOpts)
|
||||
WithWaitingStatus(message string, f func(gocui.Task) error) error
|
||||
|
Reference in New Issue
Block a user