From 7c126cd2fc6d3723004948d3a33178c0e58ee539 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 15 Nov 2025 14:24:54 +0100 Subject: [PATCH] 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. --- pkg/gui/controllers/helpers/confirmation_helper.go | 7 ++++++- pkg/gui/controllers/shell_command_action.go | 1 + pkg/gui/popup/popup_handler.go | 1 + pkg/gui/services/custom_commands/handler_creator.go | 3 ++- pkg/gui/types/common.go | 2 ++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/gui/controllers/helpers/confirmation_helper.go b/pkg/gui/controllers/helpers/confirmation_helper.go index 2650de203..b1c334914 100644 --- a/pkg/gui/controllers/helpers/confirmation_helper.go +++ b/pkg/gui/controllers/helpers/confirmation_helper.go @@ -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) diff --git a/pkg/gui/controllers/shell_command_action.go b/pkg/gui/controllers/shell_command_action.go index 185f74893..114c7fcb1 100644 --- a/pkg/gui/controllers/shell_command_action.go +++ b/pkg/gui/controllers/shell_command_action.go @@ -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( diff --git a/pkg/gui/popup/popup_handler.go b/pkg/gui/popup/popup_handler.go index 2cfbca946..d8824016f 100644 --- a/pkg/gui/popup/popup_handler.go +++ b/pkg/gui/popup/popup_handler.go @@ -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, }) } diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index 1bfaaaa0f..04899b004 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -125,7 +125,8 @@ func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrap HandleConfirm: func(str string) error { return wrappedF(str) }, - AllowEmptyInput: true, + AllowEmptyInput: true, + PreserveWhitespace: true, }) return nil diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index f754e0da5..2afe4f7d3 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -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