manifoldco / promptui

Interactive prompt for command-line applications
https://www.manifold.co
BSD 3-Clause "New" or "Revised" License
6.03k stars 333 forks source link

Prompt Validation: regex matching causes multiple rerenders on input #207

Open jenil777007 opened 2 years ago

jenil777007 commented 2 years ago

Issue reproduction:

emailPrompt := promptui.Prompt{
  Label: "Email",
  Validate: func(input string) error {
      match, err := regexp.MatchString("([a-z]+)@email.com", input)
      if err != nil {
          log.Fatal(err)
          os.Exit(1)
      }

          if !match {
        return errors.New("Please provide a valid email. i.e. abc@email.com")
    }
  },
}

Issue:

Screenshot 2022-06-24 at 2 41 24 PM

sabino-ramirez commented 2 years ago

hi @jenil777007 did you find a solution to this issue? Something similar happens when the input exceeds the length of the terminal and starts a new line.

banfg56 commented 1 year ago

config Templates in promptui.Prompt to control output content format,more detail please refer template

smnspz commented 11 months ago

Try this:

emailPrompt := promptui.Prompt{
  Label: "Email",
  Validate: func(input string) error {
          regex := regex.MustCompile(`([a-z]+)@email.com`)
          if !regex.MatchString(input) {
                  return errors.New("Wrong mail format")
          }
          return nil
  },
}