Git Product home page Git Product logo

Comments (9)

fiws avatar fiws commented on July 24, 2024 14

I also ran into this multiple times. Just accepted that err is the result.

imo there just should be a separate ConfirmPrompt that returns bool, error

from promptui.

randallmlough avatar randallmlough commented on July 24, 2024 2

Yeah this is frustrating behavior. No shouldn't be an error. If you want to split the logic tree based on the confirm result you can't do that, since it's an error. Also an acceptable answer should be "y, yes, YES, Y, 1", etc.

from promptui.

perolo avatar perolo commented on July 24, 2024 2

Repeated with latest version both in Windows and Linux

from promptui.

mkungla avatar mkungla commented on July 24, 2024 2

If in your want to use boolean expression of the prompt then following would do the the job.

package main

import (
  "errors"
  "fmt"
  "strings"

  "github.com/manifoldco/promptui"
)

func main() {
  prompt := promptui.Prompt{
    Label:     "does this work?",
    IsConfirm: true,
    Default:   "y",
  }
  validate := func(s string) error {
    if len(s) == 1 && strings.Contains("YyNn", s) || prompt.Default != "" && len(s) == 0 {
      return nil
    }
    return errors.New("invalid input")
  }
  prompt.Validate = validate
  
  answer, err := prompt.Run()
  confirmed := !errors.Is(err, promptui.ErrAbort)
  if err != nil && confirmed {
    fmt.Println("ERROR: ", err)
    return
  }
 
  fmt.Printf("error <%v>, answer <%s> confirmed <%t>\n", err, answer, confirmed)
}
type output
y error <<nil>>, answer <y> confirmed <true>
Y error <<nil>>, answer <Y> confirmed <true>
n error <>, answer <n> confirmed <false>
N error <>, answer <n> confirmed <false>
`` error <<nil>>, answer <> confirmed <true>
foo shows <invalid input> and keeps prompt open

Since implementation is emphasizing Y/N

promptui/prompt.go

Lines 43 to 45 in 981a3ca

// IsConfirm makes the prompt ask for a yes or no ([Y/N]) question rather than request an input. When set,
// most properties related to input will be ignored.
IsConfirm bool

My Two Cents to this topic is that perhaps there should be new error added to the package and return ErrInvalidInput when input string is invalid option instead returning promptui.ErrAbort any input which is not y or Default.

from promptui.

danlsgiga avatar danlsgiga commented on July 24, 2024

Also ran into the same issue and took the same path as @fiws... err != nil -> No / Empty

from promptui.

smitt04 avatar smitt04 commented on July 24, 2024

It looks like this is expected behavior. Reading the godocs states:

var ErrAbort = errors.New("")

ErrAbort is the error returned when confirm prompts are supplied "n"

from promptui.

randallmlough avatar randallmlough commented on July 24, 2024

for anyone coming across this issue, I've gotten around this issue by mocking a normal prompt to look and act like a confirm prompt. I run the results through regex to get a true/false statement.

func confirm(label string) (bool, error) {
	val := func(input string) error {
		//if input == "" {
		//	return errors.New("input required")
		//}
		if len(input) > 3 {
			return errors.New("Please give a yes or no answer")
		}
		return nil
	}
	faintText := promptui.Styler(promptui.FGFaint)
	boldText := promptui.Styler(promptui.FGBold)
	greenText := promptui.Styler(promptui.FGGreen)
	redText := promptui.Styler(promptui.FGRed)
	defaultLabel := fmt.Sprintf("%s %s ", boldText(label), faintText("[y/N]"))

	template := promptui.PromptTemplates{
		Prompt:  defaultLabel,
		Valid:   defaultLabel,
		Invalid: fmt.Sprintf("%s %s ", redText(label), faintText("[y/N]")),
		Success: greenText(label) + " ",
	}

	prompt := promptui.Prompt{
		Validate:  val,
		Templates: &template,
	}
	result, err := prompt.Run()
	if err != nil {
		return false, err
	}
	return isConfirm(result), nil
}

// acceptable answers: y,yes,Y,YES,1
var confirmExp = regexp.MustCompile("(?i)y(?:es)?|1")

func isConfirm(confirm string) bool {
	return confirmExp.MatchString(confirm)
}

from promptui.

pgcayen avatar pgcayen commented on July 24, 2024

oh, man...this threw me for a loop. The confirm within _examples doesn't make this clear either. I agree with @randallmlough that "No" shouldn't be an error since it's a valid user response.

from promptui.

feketegy avatar feketegy commented on July 24, 2024

Just test for ErrAbort like

if err != nil {
  if err == promptui.ErrAbort {
    // deal with abort
  }

  // deal with error
}

OR use the errors package to test if the returned error is/will be wrapped:

if errors.Is(err, promptui.ErrAbort) {
  ...
}

from promptui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.