Skip to content

Commit 933573f

Browse files
authored
Don't allow empty input in most prompts (#5043)
Most of our prompts don't (shouldn't) allow empty input, but most callers didn't check, and would run into cryptic errors when the user pressed enter at an empty prompt (e.g. when creating a new branch). Now we simply don't allow hitting enter in this case, and show an error toast instead. This behavior is opt-out, because there are a few cases where empty input is supported (e.g. creating a stash).
2 parents 3d5b22f + 95e5ed6 commit 933573f

File tree

12 files changed

+60
-27
lines changed

12 files changed

+60
-27
lines changed

pkg/gui/controllers/diffing_menu_action.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controllers
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
87
"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -38,7 +37,7 @@ func (self *DiffingMenuAction) Call() error {
3837
Title: self.c.Tr.EnterRefName,
3938
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetRefsSuggestionsFunc(),
4039
HandleConfirm: func(response string) error {
41-
self.c.Modes().Diffing.Ref = strings.TrimSpace(response)
40+
self.c.Modes().Diffing.Ref = response
4241
self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
4342
return nil
4443
},

pkg/gui/controllers/files_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ func (self *FilesController) handleStashSave(stashFunc func(message string) erro
12111211
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
12121212
return nil
12131213
},
1214+
AllowEmptyInput: true,
12141215
})
12151216

12161217
return nil

pkg/gui/controllers/filtering_menu_action.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controllers
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
87
"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -66,7 +65,7 @@ func (self *FilteringMenuAction) Call() error {
6665
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetFilePathSuggestionsFunc(),
6766
Title: self.c.Tr.EnterFileName,
6867
HandleConfirm: func(response string) error {
69-
return self.setFilteringPath(strings.TrimSpace(response))
68+
return self.setFilteringPath(response)
7069
},
7170
})
7271

@@ -82,7 +81,7 @@ func (self *FilteringMenuAction) Call() error {
8281
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetAuthorsSuggestionsFunc(),
8382
Title: self.c.Tr.EnterAuthor,
8483
HandleConfirm: func(response string) error {
85-
return self.setFilteringAuthor(strings.TrimSpace(response))
84+
return self.setFilteringAuthor(response)
8685
},
8786
})
8887

pkg/gui/controllers/helpers/confirmation_helper.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,33 @@ 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) closeAndCallConfirmationFunction(cancel goContext.CancelFunc, function func() error) error {
28+
cancel()
29+
30+
self.c.Context().Pop()
31+
32+
if function != nil {
33+
if err := function(); err != nil {
34+
return err
35+
}
36+
}
37+
38+
return nil
39+
}
40+
2741
func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.CancelFunc, function func() error) func() error {
42+
return func() error {
43+
return self.closeAndCallConfirmationFunction(cancel, function)
44+
}
45+
}
46+
47+
func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
48+
cancel goContext.CancelFunc,
49+
function func(string) error,
50+
getResponse func() string,
51+
allowEmptyInput bool,
52+
preserveWhitespace bool,
53+
) func() error {
2854
return func() error {
2955
if self.c.GocuiGui().IsPasting {
3056
// The user is pasting multi-line text into a prompt; we don't want to handle the
@@ -34,26 +60,22 @@ func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.Can
3460
return nil
3561
}
3662

37-
cancel()
38-
39-
self.c.Context().Pop()
63+
response := getResponse()
64+
if !preserveWhitespace {
65+
response = strings.TrimSpace(response)
66+
}
4067

41-
if function != nil {
42-
if err := function(); err != nil {
43-
return err
44-
}
68+
if response == "" && !allowEmptyInput {
69+
self.c.ErrorToast(self.c.Tr.PromptInputCannotBeEmptyToast)
70+
return nil
4571
}
4672

47-
return nil
73+
return self.closeAndCallConfirmationFunction(cancel, func() error {
74+
return function(response)
75+
})
4876
}
4977
}
5078

51-
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-
})
55-
}
56-
5779
func (self *ConfirmationHelper) DeactivateConfirmation() {
5880
self.c.Mutexes().PopupMutex.Lock()
5981
self.c.State().GetRepoState().SetCurrentPopupOpts(nil)
@@ -229,12 +251,15 @@ func (self *ConfirmationHelper) setConfirmationKeyBindings(cancel goContext.Canc
229251

230252
func (self *ConfirmationHelper) setPromptKeyBindings(cancel goContext.CancelFunc, opts types.CreatePopupPanelOpts) {
231253
onConfirm := self.wrappedPromptConfirmationFunction(cancel, opts.HandleConfirmPrompt,
232-
func() string { return self.c.Views().Prompt.TextArea.GetContent() })
254+
func() string { return self.c.Views().Prompt.TextArea.GetContent() },
255+
opts.AllowEmptyInput, opts.PreserveWhitespace)
233256

234257
onSuggestionConfirm := self.wrappedPromptConfirmationFunction(
235258
cancel,
236259
opts.HandleConfirmPrompt,
237260
self.getSelectedSuggestionValue,
261+
opts.AllowEmptyInput,
262+
opts.PreserveWhitespace,
238263
)
239264

240265
onClose := self.wrappedConfirmationFunction(cancel, opts.HandleClose)

pkg/gui/controllers/helpers/credentials_helper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.Cr
4141

4242
return nil
4343
},
44+
AllowEmptyInput: true,
4445
})
4546

4647
return nil

pkg/gui/controllers/helpers/worktree_helper.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,21 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
130130

131131
return f()
132132
},
133+
AllowEmptyInput: true,
133134
})
134135

135136
return nil
136137
}
137138

138-
// prompt for the new branch name where a blank means we just check out the branch
139+
// prompt for the new branch name
139140
self.c.Prompt(types.PromptOpts{
140141
Title: self.c.Tr.NewBranchName,
141142
HandleConfirm: func(branchName string) error {
142-
if branchName == "" {
143-
return errors.New(self.c.Tr.BranchNameCannotBeBlank)
144-
}
145-
146143
opts.Branch = branchName
147144

148145
return f()
149146
},
147+
AllowEmptyInput: false,
150148
})
151149

152150
return nil

pkg/gui/controllers/shell_command_action.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (self *ShellCommandAction) Call() error {
1919
Title: self.c.Tr.ShellCommand,
2020
FindSuggestionsFunc: self.GetShellCommandsHistorySuggestionsFunc(),
2121
AllowEditSuggestion: true,
22+
PreserveWhitespace: true,
2223
HandleConfirm: func(command string) error {
2324
if self.shouldSaveCommand(command) {
2425
self.c.GetAppState().ShellCommandsHistory = utils.Limit(

pkg/gui/controllers/stash_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func (self *StashController) handleRenameStashEntry(stashEntry *models.StashEntr
209209
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
210210
return nil
211211
},
212+
AllowEmptyInput: true,
212213
})
213214

214215
return nil

pkg/gui/popup/popup_handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func (self *PopupHandler) Prompt(opts types.PromptOpts) {
139139
HandleDeleteSuggestion: opts.HandleDeleteSuggestion,
140140
FindSuggestionsFunc: opts.FindSuggestionsFunc,
141141
AllowEditSuggestion: opts.AllowEditSuggestion,
142+
AllowEmptyInput: opts.AllowEmptyInput,
143+
PreserveWhitespace: opts.PreserveWhitespace,
142144
Mask: opts.Mask,
143145
})
144146
}

pkg/gui/services/custom_commands/handler_creator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrap
125125
HandleConfirm: func(str string) error {
126126
return wrappedF(str)
127127
},
128+
AllowEmptyInput: true,
129+
PreserveWhitespace: true,
128130
})
129131

130132
return nil

0 commit comments

Comments
 (0)