Skip to content

Commit 8d8cf42

Browse files
committed
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.
1 parent 3d5b22f commit 8d8cf42

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

pkg/gui/controllers/helpers/confirmation_helper.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,40 @@ func NewConfirmationHelper(c *HelperCommon) *ConfirmationHelper {
2424
// This file is for the rendering of confirmation panels along with setting and handling associated
2525
// keybindings.
2626

27-
func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.CancelFunc, function func() error) func() error {
28-
return func() error {
29-
if self.c.GocuiGui().IsPasting {
30-
// The user is pasting multi-line text into a prompt; we don't want to handle the
31-
// line feeds as "confirm" keybindings. Simply ignoring them is the best we can do; this
32-
// will cause the entire pasted text to appear as a single line in the prompt. Hopefully
33-
// the user knows that ctrl-u allows them to delete it again...
34-
return nil
35-
}
27+
func (self *ConfirmationHelper) closeAndCallConfirmationFunction(cancel goContext.CancelFunc, function func() error) error {
28+
if self.c.GocuiGui().IsPasting {
29+
// The user is pasting multi-line text into a prompt; we don't want to handle the
30+
// line feeds as "confirm" keybindings. Simply ignoring them is the best we can do; this
31+
// will cause the entire pasted text to appear as a single line in the prompt. Hopefully
32+
// the user knows that ctrl-u allows them to delete it again...
33+
return nil
34+
}
3635

37-
cancel()
36+
cancel()
3837

39-
self.c.Context().Pop()
38+
self.c.Context().Pop()
4039

41-
if function != nil {
42-
if err := function(); err != nil {
43-
return err
44-
}
40+
if function != nil {
41+
if err := function(); err != nil {
42+
return err
4543
}
44+
}
4645

47-
return nil
46+
return nil
47+
}
48+
49+
func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.CancelFunc, function func() error) func() error {
50+
return func() error {
51+
return self.closeAndCallConfirmationFunction(cancel, function)
4852
}
4953
}
5054

5155
func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(cancel goContext.CancelFunc, function func(string) error, getResponse func() string) func() error {
52-
return self.wrappedConfirmationFunction(cancel, func() error {
53-
return function(getResponse())
54-
})
56+
return func() error {
57+
return self.closeAndCallConfirmationFunction(cancel, func() error {
58+
return function(getResponse())
59+
})
60+
}
5561
}
5662

5763
func (self *ConfirmationHelper) DeactivateConfirmation() {

0 commit comments

Comments
 (0)