Skip to content

Commit 20600b9

Browse files
committed
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.
1 parent 1ca5f09 commit 20600b9

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pkg/gui/popup/popup_handler.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ func (self *PopupHandler) Confirm(opts types.ConfirmOpts) {
115115
})
116116
}
117117

118+
func (self *PopupHandler) ConfirmIf(condition bool, opts types.ConfirmOpts) error {
119+
if condition {
120+
self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
121+
Title: opts.Title,
122+
Prompt: opts.Prompt,
123+
HandleConfirm: opts.HandleConfirm,
124+
HandleClose: opts.HandleClose,
125+
})
126+
return nil
127+
}
128+
129+
return opts.HandleConfirm()
130+
}
131+
118132
func (self *PopupHandler) Prompt(opts types.PromptOpts) {
119133
self.createPopupPanelFn(context.Background(), types.CreatePopupPanelOpts{
120134
Title: opts.Title,

pkg/gui/types/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ type IPopupHandler interface {
126126
Alert(title string, message string)
127127
// Shows a popup asking the user for confirmation.
128128
Confirm(opts ConfirmOpts)
129+
// Shows a popup asking the user for confirmation if condition is true; otherwise, the HandleConfirm function is called directly.
130+
ConfirmIf(condition bool, opts ConfirmOpts) error
129131
// Shows a popup prompting the user for input.
130132
Prompt(opts PromptOpts)
131133
WithWaitingStatus(message string, f func(gocui.Task) error) error

0 commit comments

Comments
 (0)