1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-20 17:22:23 +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:
Stefan Haller
2025-11-15 14:24:54 +01:00
parent d7e733cd56
commit 7c126cd2fc
5 changed files with 12 additions and 2 deletions

View File

@@ -49,6 +49,7 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
function func(string) error, function func(string) error,
getResponse func() string, getResponse func() string,
allowEmptyInput bool, allowEmptyInput bool,
preserveWhitespace bool,
) func() error { ) func() error {
return func() error { return func() error {
if self.c.GocuiGui().IsPasting { if self.c.GocuiGui().IsPasting {
@@ -60,6 +61,9 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
} }
response := getResponse() response := getResponse()
if !preserveWhitespace {
response = strings.TrimSpace(response)
}
if response == "" && !allowEmptyInput { if response == "" && !allowEmptyInput {
self.c.ErrorToast(self.c.Tr.PromptInputCannotBeEmptyToast) 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) { func (self *ConfirmationHelper) setPromptKeyBindings(cancel goContext.CancelFunc, opts types.CreatePopupPanelOpts) {
onConfirm := self.wrappedPromptConfirmationFunction(cancel, opts.HandleConfirmPrompt, 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) opts.AllowEmptyInput, opts.PreserveWhitespace)
onSuggestionConfirm := self.wrappedPromptConfirmationFunction( onSuggestionConfirm := self.wrappedPromptConfirmationFunction(
cancel, cancel,
opts.HandleConfirmPrompt, opts.HandleConfirmPrompt,
self.getSelectedSuggestionValue, self.getSelectedSuggestionValue,
opts.AllowEmptyInput, opts.AllowEmptyInput,
opts.PreserveWhitespace,
) )
onClose := self.wrappedConfirmationFunction(cancel, opts.HandleClose) onClose := self.wrappedConfirmationFunction(cancel, opts.HandleClose)

View File

@@ -19,6 +19,7 @@ func (self *ShellCommandAction) Call() error {
Title: self.c.Tr.ShellCommand, Title: self.c.Tr.ShellCommand,
FindSuggestionsFunc: self.GetShellCommandsHistorySuggestionsFunc(), FindSuggestionsFunc: self.GetShellCommandsHistorySuggestionsFunc(),
AllowEditSuggestion: true, AllowEditSuggestion: true,
PreserveWhitespace: true,
HandleConfirm: func(command string) error { HandleConfirm: func(command string) error {
if self.shouldSaveCommand(command) { if self.shouldSaveCommand(command) {
self.c.GetAppState().ShellCommandsHistory = utils.Limit( self.c.GetAppState().ShellCommandsHistory = utils.Limit(

View File

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

View File

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

View File

@@ -173,6 +173,7 @@ type CreatePopupPanelOpts struct {
Mask bool Mask bool
AllowEditSuggestion bool AllowEditSuggestion bool
AllowEmptyInput bool AllowEmptyInput bool
PreserveWhitespace bool
} }
type ConfirmOpts struct { type ConfirmOpts struct {
@@ -192,6 +193,7 @@ type PromptOpts struct {
HandleConfirm func(string) error HandleConfirm func(string) error
AllowEditSuggestion bool AllowEditSuggestion bool
AllowEmptyInput bool AllowEmptyInput bool
PreserveWhitespace bool
// CAPTURE THIS // CAPTURE THIS
HandleClose func() error HandleClose func() error
HandleDeleteSuggestion func(int) error HandleDeleteSuggestion func(int) error