Skip to content

Commit b3435bd

Browse files
committed
Add AllowEmptyInput flag to PromptOpts
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).
1 parent a7bc101 commit b3435bd

File tree

9 files changed

+28
-3
lines changed

9 files changed

+28
-3
lines changed

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/helpers/confirmation_helper.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.Can
4444
}
4545
}
4646

47-
func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(cancel goContext.CancelFunc, function func(string) error, getResponse func() string) func() error {
47+
func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
48+
cancel goContext.CancelFunc,
49+
function func(string) error,
50+
getResponse func() string,
51+
allowEmptyInput bool,
52+
) func() error {
4853
return func() error {
4954
if self.c.GocuiGui().IsPasting {
5055
// The user is pasting multi-line text into a prompt; we don't want to handle the
@@ -54,8 +59,15 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(cancel goConte
5459
return nil
5560
}
5661

62+
response := getResponse()
63+
64+
if response == "" && !allowEmptyInput {
65+
self.c.ErrorToast(self.c.Tr.PromptInputCannotBeEmptyToast)
66+
return nil
67+
}
68+
5769
return self.closeAndCallConfirmationFunction(cancel, func() error {
58-
return function(getResponse())
70+
return function(response)
5971
})
6072
}
6173
}
@@ -235,12 +247,14 @@ func (self *ConfirmationHelper) setConfirmationKeyBindings(cancel goContext.Canc
235247

236248
func (self *ConfirmationHelper) setPromptKeyBindings(cancel goContext.CancelFunc, opts types.CreatePopupPanelOpts) {
237249
onConfirm := self.wrappedPromptConfirmationFunction(cancel, opts.HandleConfirmPrompt,
238-
func() string { return self.c.Views().Prompt.TextArea.GetContent() })
250+
func() string { return self.c.Views().Prompt.TextArea.GetContent() },
251+
opts.AllowEmptyInput)
239252

240253
onSuggestionConfirm := self.wrappedPromptConfirmationFunction(
241254
cancel,
242255
opts.HandleConfirmPrompt,
243256
self.getSelectedSuggestionValue,
257+
opts.AllowEmptyInput,
244258
)
245259

246260
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
130130

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

135136
return nil
@@ -147,6 +148,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
147148

148149
return f()
149150
},
151+
AllowEmptyInput: false,
150152
})
151153

152154
return nil

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func (self *PopupHandler) Prompt(opts types.PromptOpts) {
139139
HandleDeleteSuggestion: opts.HandleDeleteSuggestion,
140140
FindSuggestionsFunc: opts.FindSuggestionsFunc,
141141
AllowEditSuggestion: opts.AllowEditSuggestion,
142+
AllowEmptyInput: opts.AllowEmptyInput,
142143
Mask: opts.Mask,
143144
})
144145
}

pkg/gui/services/custom_commands/handler_creator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrap
125125
HandleConfirm: func(str string) error {
126126
return wrappedF(str)
127127
},
128+
AllowEmptyInput: true,
128129
})
129130

130131
return nil

pkg/gui/types/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ type CreatePopupPanelOpts struct {
172172
FindSuggestionsFunc func(string) []*Suggestion
173173
Mask bool
174174
AllowEditSuggestion bool
175+
AllowEmptyInput bool
175176
}
176177

177178
type ConfirmOpts struct {
@@ -190,6 +191,7 @@ type PromptOpts struct {
190191
FindSuggestionsFunc func(string) []*Suggestion
191192
HandleConfirm func(string) error
192193
AllowEditSuggestion bool
194+
AllowEmptyInput bool
193195
// CAPTURE THIS
194196
HandleClose func() error
195197
HandleDeleteSuggestion func(int) error

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ type TranslationSet struct {
618618
MustStashTitle string
619619
ConfirmationTitle string
620620
PromptTitle string
621+
PromptInputCannotBeEmptyToast string
621622
PrevPage string
622623
NextPage string
623624
GotoTop string
@@ -1713,6 +1714,7 @@ func EnglishTranslationSet() *TranslationSet {
17131714
MustStashTitle: "Must stash",
17141715
ConfirmationTitle: "Confirmation panel",
17151716
PromptTitle: "Input prompt",
1717+
PromptInputCannotBeEmptyToast: "Empty input is not allowed",
17161718
PrevPage: "Previous page",
17171719
NextPage: "Next page",
17181720
GotoTop: "Scroll to top",

0 commit comments

Comments
 (0)