Comments (9)
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.
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.
Repeated with latest version both in Windows and Linux
from promptui.
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
Lines 43 to 45 in 981a3ca
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.
Also ran into the same issue and took the same path as @fiws... err != nil -> No / Empty
from promptui.
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.
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.
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.
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)
- Using arrays or other data structures
- repeat help string when Detail string is too long HOT 2
- Prompt select label and help appear multiple times HOT 1
- Reset the prompt after selection
- Nohup running retruns ^D
- Auto Complete? HOT 1
- How to remove ":" on label Select? HOT 1
- Prompt Validation: regex matching causes multiple rerenders on input HOT 3
- Git Bash Prompts fail with EOF HOT 1
- Empty lines in output?
- Data race after running promptui.Prompt.Run()
- Stdout should be `io.Writer` instead of `io.WriterCloser`
- Default searcher implemetation HOT 5
- Dynamic search based select HOT 1
- Replace input text while typing
- Run validation after return key
- Disabling the checkmark and output. HOT 1
- https://www.manifold.co certificate is expired since 2022-08-26 HOT 2
- Select HOT 1
- Printf before prompt run will cut long string
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promptui.