forked from cqroot/prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runners.go
95 lines (78 loc) · 2.85 KB
/
runners.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package prompt
import (
"github.com/ad8-adriant/cqroot-prompt/choose"
"github.com/ad8-adriant/cqroot-prompt/input"
"github.com/ad8-adriant/cqroot-prompt/multichoose"
"github.com/ad8-adriant/cqroot-prompt/write"
)
// Choose lets the user choose one of the given choices.
func (p Prompt) Choose(choices []string, opts ...choose.Option) (string, error) {
pm := choose.NewWithStrings(choices, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return "", err
}
return m.(choose.Model).Data(), nil
}
// ChooseIndex lets the user choose one of the given choices.
func (p Prompt) ChooseIndex(choices []string, opts ...choose.Option) (int, error) {
pm := choose.NewWithStrings(choices, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return 0, err
}
return m.(choose.Model).Index(), nil
}
// Choose lets the user choose one of the given choices.
func (p Prompt) AdvancedChoose(choices []choose.Choice, opts ...choose.Option) (string, error) {
pm := choose.New(choices, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return "", err
}
return m.(choose.Model).Data(), nil
}
// Choose lets the user choose one of the given choices.
func (p Prompt) AdvancedChooseIndex(choices []choose.Choice, opts ...choose.Option) (int, error) {
pm := choose.New(choices, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return 0, err
}
return m.(choose.Model).Index(), nil
}
// MultiChoose lets the user choose multiples from the given choices.
func (p Prompt) MultiChoose(choices []string, opts ...multichoose.Option) ([]string, error) {
pm := multichoose.New(choices, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return nil, err
}
return m.(multichoose.Model).Data(), nil
}
// MultiChooseIndex lets the user choose multiples from the given choices.
func (p Prompt) MultiChooseIndex(choices []string, opts ...multichoose.Option) ([]int, error) {
pm := multichoose.New(choices, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return nil, err
}
return m.(multichoose.Model).Index(), nil
}
// Input asks the user to enter a string.
func (p Prompt) Input(defaultValue string, opts ...input.Option) (string, error) {
pm := input.New(defaultValue, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return "", err
}
return m.(input.Model).Data(), nil
}
func (p Prompt) Write(defaultValue string, opts ...write.Option) (string, error) {
pm := write.New(defaultValue, opts...)
m, err := p.Run(*pm, append(p.teaProgramOpts, pm.TeaProgramOpts()...)...)
if err != nil {
return "", err
}
return m.(write.Model).Data(), nil
}