From 8d8cf42786f2f57f6972a95c34553130cba4f5d2 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 15 Nov 2025 14:04:45 +0100 Subject: [PATCH] Refactor: extract body of wrappedConfirmationFunction into a helper function And call this new helper function from both wrappedConfirmationFunction and wrappedPromptConfirmationFunction; this gives us more flexibility to do different things in each of those. --- .../helpers/confirmation_helper.go | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/pkg/gui/controllers/helpers/confirmation_helper.go b/pkg/gui/controllers/helpers/confirmation_helper.go index 1599d5e86..cc06e03af 100644 --- a/pkg/gui/controllers/helpers/confirmation_helper.go +++ b/pkg/gui/controllers/helpers/confirmation_helper.go @@ -24,34 +24,40 @@ func NewConfirmationHelper(c *HelperCommon) *ConfirmationHelper { // This file is for the rendering of confirmation panels along with setting and handling associated // keybindings. +func (self *ConfirmationHelper) closeAndCallConfirmationFunction(cancel goContext.CancelFunc, function func() error) error { + if self.c.GocuiGui().IsPasting { + // The user is pasting multi-line text into a prompt; we don't want to handle the + // line feeds as "confirm" keybindings. Simply ignoring them is the best we can do; this + // will cause the entire pasted text to appear as a single line in the prompt. Hopefully + // the user knows that ctrl-u allows them to delete it again... + return nil + } + + cancel() + + self.c.Context().Pop() + + if function != nil { + if err := function(); err != nil { + return err + } + } + + return nil +} + func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.CancelFunc, function func() error) func() error { return func() error { - if self.c.GocuiGui().IsPasting { - // The user is pasting multi-line text into a prompt; we don't want to handle the - // line feeds as "confirm" keybindings. Simply ignoring them is the best we can do; this - // will cause the entire pasted text to appear as a single line in the prompt. Hopefully - // the user knows that ctrl-u allows them to delete it again... - return nil - } - - cancel() - - self.c.Context().Pop() - - if function != nil { - if err := function(); err != nil { - return err - } - } - - return nil + return self.closeAndCallConfirmationFunction(cancel, function) } } func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(cancel goContext.CancelFunc, function func(string) error, getResponse func() string) func() error { - return self.wrappedConfirmationFunction(cancel, func() error { - return function(getResponse()) - }) + return func() error { + return self.closeAndCallConfirmationFunction(cancel, func() error { + return function(getResponse()) + }) + } } func (self *ConfirmationHelper) DeactivateConfirmation() {