From c35f907f9ff611b12a2c336e1069c53e65efc7ad Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 6 Jul 2025 13:50:33 +0200 Subject: [PATCH] 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. --- pkg/gui/popup/popup_handler.go | 14 ++++++++++++++ pkg/gui/types/common.go | 2 ++ 2 files changed, 16 insertions(+) diff --git a/pkg/gui/popup/popup_handler.go b/pkg/gui/popup/popup_handler.go index 7fe77a8da..d8ce654b4 100644 --- a/pkg/gui/popup/popup_handler.go +++ b/pkg/gui/popup/popup_handler.go @@ -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, diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index fb472ab24..bf7267a8c 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -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