Skip to content

Commit 70b936c

Browse files
committed
feat: select and multi-input prompts
1 parent e4e6364 commit 70b936c

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

prompts/multi_input.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package prompts
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
"strings"
7+
8+
"github.com/manifoldco/promptui"
9+
)
10+
11+
type MultiInput struct {
12+
Label string
13+
ValidString string
14+
}
15+
16+
func (m *MultiInput) Run() (string, error) {
17+
keepGoing := true
18+
var answers []string
19+
20+
prompt := promptui.Prompt{
21+
Label: m.Label,
22+
Validate: func(input string) error {
23+
if matched, err := regexp.Match(m.ValidString, []byte(input)); err != nil || !matched {
24+
return errors.New("not a valid string")
25+
}
26+
27+
return nil
28+
},
29+
}
30+
31+
for keepGoing {
32+
result, err := prompt.Run()
33+
34+
if err != nil {
35+
return "", err
36+
}
37+
38+
answers = append(answers, result)
39+
40+
add := promptui.Select{
41+
Label: "Add more? [Y",
42+
Items: []string{"yes", "no"},
43+
}
44+
confirm, err := add.Run()
45+
if err != nil {
46+
return "", err
47+
}
48+
49+
if confirm == "no" {
50+
keepGoing = false
51+
}
52+
}
53+
54+
return strings.Join(answers, ","), nil
55+
}

prompts/select.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package prompts
2+
3+
import "github.com/manifoldco/promptui"
4+
5+
type SelectInput struct {
6+
Label string
7+
Options []string
8+
}
9+
10+
func (s *SelectInput) Run() (string, error) {
11+
prompt := promptui.Select{
12+
Label: s.Label,
13+
Items: s.Options,
14+
}
15+
16+
_, a, err := prompt.Run()
17+
return a, err
18+
}

0 commit comments

Comments
 (0)