Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
(feat): allow help input as a valid answer
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jun 30, 2021
1 parent 62afa3b commit 76af29d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 11 additions & 1 deletion examples/inputfilesuggestion.go
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"path/filepath"

Expand All @@ -21,7 +22,16 @@ var q = []*survey.Question{
Suggest: suggestFiles,
Help: "Any file; do not need to exist yet",
},
Validate: survey.Required,
Validate: survey.ComposeValidators(
survey.Required,
func(file interface{}) error {
if file == "?" {
return errors.New("? is not a valid file name")
}

return nil
},
),
},
}

Expand Down
2 changes: 1 addition & 1 deletion input.go
Expand Up @@ -185,7 +185,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
cursor.Up(1)

// if we ran into the help string
if i.answer == config.HelpInput && i.Help != "" {
if i.answer == config.HelpInput && i.Help != "" && !i.showingHelp {
// show the help and prompt again
i.showingHelp = true
return i.Prompt(config)
Expand Down
15 changes: 15 additions & 0 deletions input_test.go
Expand Up @@ -184,6 +184,21 @@ func TestInputPrompt(t *testing.T) {
},
"Satoshi Nakamoto",
},
{
"Test Input prompt interaction and using help input as answer",
&Input{
Message: "Are you confused?",
Help: "You can use ? as answer when in doubt",
},
func(c *expect.Console) {
c.ExpectString("Are you confused?")
c.SendLine("?")
c.ExpectString("You can use ? as answer when in doubt")
c.SendLine("?")
c.ExpectEOF()
},
"?",
},
{
// https://en.wikipedia.org/wiki/ANSI_escape_code
// Device Status Report - Reports the cursor position (CPR) to the
Expand Down

0 comments on commit 76af29d

Please sign in to comment.