-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.go
60 lines (53 loc) · 1.15 KB
/
form.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package prompt
import (
"fmt"
"strings"
)
type Form struct {
labels []string
inputs []func() error
}
func NewForm() *Form {
return &Form{}
}
func (f *Form) Print(label string, ival interface{}) {
i := len(f.labels)
f.labels = append(f.labels, label)
f.inputs = append(f.inputs, func() error {
fmt.Printf("%v: %v\n", f.labels[i], ival)
return nil
})
}
func (f *Form) Prompt(idst interface{}, label string, validators ...Validator) {
i := len(f.labels)
f.labels = append(f.labels, label)
f.inputs = append(f.inputs, func() error {
return Prompt(idst, f.labels[i], validators...)
})
}
func (f *Form) Select(idst interface{}, label string, ioptions interface{}) {
i := len(f.labels)
f.labels = append(f.labels, label)
f.inputs = append(f.inputs, func() error {
return Select(idst, f.labels[i], ioptions)
})
}
func (f *Form) Send() error {
n := 0
for _, label := range f.labels {
if n < len(label) {
n = len(label)
}
}
for i, label := range f.labels {
if len(label) < n {
f.labels[i] = strings.Repeat(" ", n-len(label)) + label
}
}
for _, input := range f.inputs {
if err := input(); err != nil {
return err
}
}
return nil
}