mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-11-22 04:42:37 +03:00
Strip leading/trailing whitespace from prompt input
This doesn't really solve a pressing problem, because I guess it's unlikely that users add spaces at the beginning or end of what they type into a prompt; but it could happen, and in this case we almost always want to strip it. Just adding this here for completeness while I was working on this code. The only exception is the input prompt of custom commands, because who knows what users want to use that input for in their custom command.
This commit is contained in:
@@ -49,6 +49,7 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
|
||||
function func(string) error,
|
||||
getResponse func() string,
|
||||
allowEmptyInput bool,
|
||||
preserveWhitespace bool,
|
||||
) func() error {
|
||||
return func() error {
|
||||
if self.c.GocuiGui().IsPasting {
|
||||
@@ -60,6 +61,9 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
|
||||
}
|
||||
|
||||
response := getResponse()
|
||||
if !preserveWhitespace {
|
||||
response = strings.TrimSpace(response)
|
||||
}
|
||||
|
||||
if response == "" && !allowEmptyInput {
|
||||
self.c.ErrorToast(self.c.Tr.PromptInputCannotBeEmptyToast)
|
||||
@@ -248,13 +252,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() },
|
||||
opts.AllowEmptyInput)
|
||||
opts.AllowEmptyInput, opts.PreserveWhitespace)
|
||||
|
||||
onSuggestionConfirm := self.wrappedPromptConfirmationFunction(
|
||||
cancel,
|
||||
opts.HandleConfirmPrompt,
|
||||
self.getSelectedSuggestionValue,
|
||||
opts.AllowEmptyInput,
|
||||
opts.PreserveWhitespace,
|
||||
)
|
||||
|
||||
onClose := self.wrappedConfirmationFunction(cancel, opts.HandleClose)
|
||||
|
||||
@@ -19,6 +19,7 @@ func (self *ShellCommandAction) Call() error {
|
||||
Title: self.c.Tr.ShellCommand,
|
||||
FindSuggestionsFunc: self.GetShellCommandsHistorySuggestionsFunc(),
|
||||
AllowEditSuggestion: true,
|
||||
PreserveWhitespace: true,
|
||||
HandleConfirm: func(command string) error {
|
||||
if self.shouldSaveCommand(command) {
|
||||
self.c.GetAppState().ShellCommandsHistory = utils.Limit(
|
||||
|
||||
@@ -140,6 +140,7 @@ func (self *PopupHandler) Prompt(opts types.PromptOpts) {
|
||||
FindSuggestionsFunc: opts.FindSuggestionsFunc,
|
||||
AllowEditSuggestion: opts.AllowEditSuggestion,
|
||||
AllowEmptyInput: opts.AllowEmptyInput,
|
||||
PreserveWhitespace: opts.PreserveWhitespace,
|
||||
Mask: opts.Mask,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -126,6 +126,7 @@ func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrap
|
||||
return wrappedF(str)
|
||||
},
|
||||
AllowEmptyInput: true,
|
||||
PreserveWhitespace: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
|
||||
@@ -173,6 +173,7 @@ type CreatePopupPanelOpts struct {
|
||||
Mask bool
|
||||
AllowEditSuggestion bool
|
||||
AllowEmptyInput bool
|
||||
PreserveWhitespace bool
|
||||
}
|
||||
|
||||
type ConfirmOpts struct {
|
||||
@@ -192,6 +193,7 @@ type PromptOpts struct {
|
||||
HandleConfirm func(string) error
|
||||
AllowEditSuggestion bool
|
||||
AllowEmptyInput bool
|
||||
PreserveWhitespace bool
|
||||
// CAPTURE THIS
|
||||
HandleClose func() error
|
||||
HandleDeleteSuggestion func(int) error
|
||||
|
||||
Reference in New Issue
Block a user