1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-20 17:22:23 +03:00

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).
This commit is contained in:
Stefan Haller
2025-11-15 13:05:39 +01:00
parent a7bc1012b9
commit b3435bd59c
9 changed files with 28 additions and 3 deletions

View File

@@ -1211,6 +1211,7 @@ func (self *FilesController) handleStashSave(stashFunc func(message string) erro
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}})
return nil
},
AllowEmptyInput: true,
})
return nil

View File

@@ -44,7 +44,12 @@ func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.Can
}
}
func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(cancel goContext.CancelFunc, function func(string) error, getResponse func() string) func() error {
func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
cancel goContext.CancelFunc,
function func(string) error,
getResponse func() string,
allowEmptyInput bool,
) 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
@@ -54,8 +59,15 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(cancel goConte
return nil
}
response := getResponse()
if response == "" && !allowEmptyInput {
self.c.ErrorToast(self.c.Tr.PromptInputCannotBeEmptyToast)
return nil
}
return self.closeAndCallConfirmationFunction(cancel, func() error {
return function(getResponse())
return function(response)
})
}
}
@@ -235,12 +247,14 @@ func (self *ConfirmationHelper) setConfirmationKeyBindings(cancel goContext.Canc
func (self *ConfirmationHelper) setPromptKeyBindings(cancel goContext.CancelFunc, opts types.CreatePopupPanelOpts) {
onConfirm := self.wrappedPromptConfirmationFunction(cancel, opts.HandleConfirmPrompt,
func() string { return self.c.Views().Prompt.TextArea.GetContent() })
func() string { return self.c.Views().Prompt.TextArea.GetContent() },
opts.AllowEmptyInput)
onSuggestionConfirm := self.wrappedPromptConfirmationFunction(
cancel,
opts.HandleConfirmPrompt,
self.getSelectedSuggestionValue,
opts.AllowEmptyInput,
)
onClose := self.wrappedConfirmationFunction(cancel, opts.HandleClose)

View File

@@ -41,6 +41,7 @@ func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.Cr
return nil
},
AllowEmptyInput: true,
})
return nil

View File

@@ -130,6 +130,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
return f()
},
AllowEmptyInput: true,
})
return nil
@@ -147,6 +148,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
return f()
},
AllowEmptyInput: false,
})
return nil

View File

@@ -209,6 +209,7 @@ func (self *StashController) handleRenameStashEntry(stashEntry *models.StashEntr
self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH}})
return nil
},
AllowEmptyInput: true,
})
return nil

View File

@@ -139,6 +139,7 @@ func (self *PopupHandler) Prompt(opts types.PromptOpts) {
HandleDeleteSuggestion: opts.HandleDeleteSuggestion,
FindSuggestionsFunc: opts.FindSuggestionsFunc,
AllowEditSuggestion: opts.AllowEditSuggestion,
AllowEmptyInput: opts.AllowEmptyInput,
Mask: opts.Mask,
})
}

View File

@@ -125,6 +125,7 @@ func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrap
HandleConfirm: func(str string) error {
return wrappedF(str)
},
AllowEmptyInput: true,
})
return nil

View File

@@ -172,6 +172,7 @@ type CreatePopupPanelOpts struct {
FindSuggestionsFunc func(string) []*Suggestion
Mask bool
AllowEditSuggestion bool
AllowEmptyInput bool
}
type ConfirmOpts struct {
@@ -190,6 +191,7 @@ type PromptOpts struct {
FindSuggestionsFunc func(string) []*Suggestion
HandleConfirm func(string) error
AllowEditSuggestion bool
AllowEmptyInput bool
// CAPTURE THIS
HandleClose func() error
HandleDeleteSuggestion func(int) error

View File

@@ -618,6 +618,7 @@ type TranslationSet struct {
MustStashTitle string
ConfirmationTitle string
PromptTitle string
PromptInputCannotBeEmptyToast string
PrevPage string
NextPage string
GotoTop string
@@ -1713,6 +1714,7 @@ func EnglishTranslationSet() *TranslationSet {
MustStashTitle: "Must stash",
ConfirmationTitle: "Confirmation panel",
PromptTitle: "Input prompt",
PromptInputCannotBeEmptyToast: "Empty input is not allowed",
PrevPage: "Previous page",
NextPage: "Next page",
GotoTop: "Scroll to top",