From 8db9be977c4eb73aca56ee4808a85ffe5919bf19 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Thu, 5 Jan 2023 23:49:15 -0800 Subject: [PATCH] pkg/terminal: use default for Y/n empty response (#3248) The typical convention with `[Y/n]` type command line questions is that the capitalized letter represents the default if the user simply hits "enter" instead of typing an actual response. This patch fixes our implementation to use the implied default response. --- pkg/terminal/command.go | 2 +- pkg/terminal/terminal.go | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/terminal/command.go b/pkg/terminal/command.go index 86b8779f..d661daef 100644 --- a/pkg/terminal/command.go +++ b/pkg/terminal/command.go @@ -1754,7 +1754,7 @@ func setBreakpoint(t *Term, ctx callContext, tracepoint bool, argstr string) ([] if findLocErr != nil && shouldAskToSuspendBreakpoint(t) { fmt.Fprintf(os.Stderr, "Command failed: %s\n", findLocErr.Error()) findLocErr = nil - answer, err := yesno(t.line, "Set a suspended breakpoint (Delve will try to set this breakpoint when a plugin is loaded) [Y/n]?") + answer, err := yesno(t.line, "Set a suspended breakpoint (Delve will try to set this breakpoint when a plugin is loaded) [Y/n]?", "yes") if err != nil { return nil, err } diff --git a/pkg/terminal/terminal.go b/pkg/terminal/terminal.go index 581087ef..4b62625e 100644 --- a/pkg/terminal/terminal.go +++ b/pkg/terminal/terminal.go @@ -432,13 +432,16 @@ func (t *Term) promptForInput() (string, error) { return l, nil } -func yesno(line *liner.State, question string) (bool, error) { +func yesno(line *liner.State, question, defaultAnswer string) (bool, error) { for { answer, err := line.Prompt(question) if err != nil { return false, err } answer = strings.ToLower(strings.TrimSpace(answer)) + if answer == "" { + answer = defaultAnswer + } switch answer { case "n", "no": return false, nil @@ -469,7 +472,7 @@ func (t *Term) handleExit() (int, error) { if err != nil { if isErrProcessExited(err) { if t.client.IsMulticlient() { - answer, err := yesno(t.line, "Remote process has exited. Would you like to kill the headless instance? [Y/n] ") + answer, err := yesno(t.line, "Remote process has exited. Would you like to kill the headless instance? [Y/n] ", "yes") if err != nil { return 2, io.EOF } @@ -495,7 +498,7 @@ func (t *Term) handleExit() (int, error) { doDetach := true if t.client.IsMulticlient() { - answer, err := yesno(t.line, "Would you like to kill the headless instance? [Y/n] ") + answer, err := yesno(t.line, "Would you like to kill the headless instance? [Y/n] ", "yes") if err != nil { return 2, io.EOF } @@ -505,7 +508,7 @@ func (t *Term) handleExit() (int, error) { if doDetach { kill := true if t.client.AttachedToExistingProcess() { - answer, err := yesno(t.line, "Would you like to kill the process? [Y/n] ") + answer, err := yesno(t.line, "Would you like to kill the process? [Y/n] ", "yes") if err != nil { return 2, io.EOF }