Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #81: do not return err if 'n' is chosen in confirm prompt #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ type Prompt struct {
// text/template syntax. Custom state, colors and background color are available for use inside
// the templates and are documented inside the Variable section of the docs.
//
// Examples
// # Examples
//
// text/templates use a special notation to display programmable content. Using the double bracket notation,
// the value can be printed with specific helper functions. For example
//
// This displays the value given to the template as pure, unstylized text.
// '{{ . }}'
//
// '{{ . }}'
//
// This displays the value colored in cyan
// '{{ . | cyan }}'
//
// '{{ . | cyan }}'
//
// This displays the value colored in red with a cyan background-color
// '{{ . | red | cyan }}'
//
// '{{ . | red | cyan }}'
//
// See the doc of text/template for more info: https://golang.org/pkg/text/template/
type PromptTemplates struct {
Expand Down Expand Up @@ -229,11 +232,13 @@ func (p *Prompt) Run() (string, error) {
prompt := render(p.Templates.success, p.Label)
prompt = append(prompt, []byte(echo)...)

lowerDefault := strings.ToLower(p.Default)
if p.IsConfirm {
lowerDefault := strings.ToLower(p.Default)
if strings.ToLower(cur.Get()) != "y" && (lowerDefault != "y" || (lowerDefault == "y" && cur.Get() != "")) {
if (cur.Get() == "" && lowerDefault == "") ||
(strings.ToLower(cur.Get()) != "y" && strings.ToLower(cur.Get()) != "n" && cur.Get() != "") {

prompt = render(p.Templates.invalid, p.Label)
err = ErrAbort
err = ErrInvalidInput
}
}

Expand All @@ -248,6 +253,9 @@ func (p *Prompt) Run() (string, error) {
rl.Write([]byte(showCursor))
rl.Close()

if lowerDefault != "" && cur.Get() == "" {
return lowerDefault, err
}
return cur.Get(), err
}

Expand Down
4 changes: 2 additions & 2 deletions promptui.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var ErrEOF = errors.New("^D")
// encountered.
var ErrInterrupt = errors.New("^C")

// ErrAbort is the error returned when confirm prompts are supplied "n"
var ErrAbort = errors.New("")
// ErrInvalidInput is the error returned when confirm prompts are supplied input different from y/n
var ErrInvalidInput = errors.New("invalid input")

// ValidateFunc is a placeholder type for any validation functions that validates a given input. It should return
// a ValidationError if the input is not valid.
Expand Down