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

feat(Prompt): add lazy validation. #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Prompt struct {
// Validate is an optional function that fill be used against the entered value in the prompt to validate it.
Validate ValidateFunc

// Lazy validation on <Enter> only.
LazyValidation bool

// Mask is an optional rune that sets which character to display instead of the entered characters. This
// allows hiding private information like passwords.
Mask rune
Expand Down Expand Up @@ -157,10 +160,17 @@ func (p *Prompt) Run() (string, error) {

listen := func(input []rune, pos int, key rune) ([]rune, int, bool) {
_, _, keepOn := cur.Listen(input, pos, key)
err := validFn(cur.Get())
var prompt []byte
var (
err error
prompt []byte
)

if err != nil {
if p.LazyValidation == false {
err = validFn(cur.Get())
}


if err != nil || p.LazyValidation {
prompt = render(p.Templates.invalid, p.Label)
} else {
prompt = render(p.Templates.valid, p.Label)
Expand Down