Skip to content

Commit 7c126cd

Browse files
committed
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.
1 parent d7e733c commit 7c126cd

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

pkg/gui/controllers/helpers/confirmation_helper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
4949
function func(string) error,
5050
getResponse func() string,
5151
allowEmptyInput bool,
52+
preserveWhitespace bool,
5253
) func() error {
5354
return func() error {
5455
if self.c.GocuiGui().IsPasting {
@@ -60,6 +61,9 @@ func (self *ConfirmationHelper) wrappedPromptConfirmationFunction(
6061
}
6162

6263
response := getResponse()
64+
if !preserveWhitespace {
65+
response = strings.TrimSpace(response)
66+
}
6367

6468
if response == "" && !allowEmptyInput {
6569
self.c.ErrorToast(self.c.Tr.PromptInputCannotBeEmptyToast)
@@ -248,13 +252,14 @@ func (self *ConfirmationHelper) setConfirmationKeyBindings(cancel goContext.Canc
248252
func (self *ConfirmationHelper) setPromptKeyBindings(cancel goContext.CancelFunc, opts types.CreatePopupPanelOpts) {
249253
onConfirm := self.wrappedPromptConfirmationFunction(cancel, opts.HandleConfirmPrompt,
250254
func() string { return self.c.Views().Prompt.TextArea.GetContent() },
251-
opts.AllowEmptyInput)
255+
opts.AllowEmptyInput, opts.PreserveWhitespace)
252256

253257
onSuggestionConfirm := self.wrappedPromptConfirmationFunction(
254258
cancel,
255259
opts.HandleConfirmPrompt,
256260
self.getSelectedSuggestionValue,
257261
opts.AllowEmptyInput,
262+
opts.PreserveWhitespace,
258263
)
259264

260265
onClose := self.wrappedConfirmationFunction(cancel, opts.HandleClose)

pkg/gui/controllers/shell_command_action.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (self *ShellCommandAction) Call() error {
1919
Title: self.c.Tr.ShellCommand,
2020
FindSuggestionsFunc: self.GetShellCommandsHistorySuggestionsFunc(),
2121
AllowEditSuggestion: true,
22+
PreserveWhitespace: true,
2223
HandleConfirm: func(command string) error {
2324
if self.shouldSaveCommand(command) {
2425
self.c.GetAppState().ShellCommandsHistory = utils.Limit(

pkg/gui/popup/popup_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (self *PopupHandler) Prompt(opts types.PromptOpts) {
140140
FindSuggestionsFunc: opts.FindSuggestionsFunc,
141141
AllowEditSuggestion: opts.AllowEditSuggestion,
142142
AllowEmptyInput: opts.AllowEmptyInput,
143+
PreserveWhitespace: opts.PreserveWhitespace,
143144
Mask: opts.Mask,
144145
})
145146
}

pkg/gui/services/custom_commands/handler_creator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrap
125125
HandleConfirm: func(str string) error {
126126
return wrappedF(str)
127127
},
128-
AllowEmptyInput: true,
128+
AllowEmptyInput: true,
129+
PreserveWhitespace: true,
129130
})
130131

131132
return nil

pkg/gui/types/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ type CreatePopupPanelOpts struct {
173173
Mask bool
174174
AllowEditSuggestion bool
175175
AllowEmptyInput bool
176+
PreserveWhitespace bool
176177
}
177178

178179
type ConfirmOpts struct {
@@ -192,6 +193,7 @@ type PromptOpts struct {
192193
HandleConfirm func(string) error
193194
AllowEditSuggestion bool
194195
AllowEmptyInput bool
196+
PreserveWhitespace bool
195197
// CAPTURE THIS
196198
HandleClose func() error
197199
HandleDeleteSuggestion func(int) error

0 commit comments

Comments
 (0)