-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathinteract_demo.go
204 lines (170 loc) · 5.17 KB
/
interact_demo.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cmd
import (
"fmt"
"os/exec"
"github.com/gookit/color"
"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/interact"
"github.com/gookit/gcli/v3/interact/cparam"
"github.com/gookit/gcli/v3/show/emoji"
"github.com/gookit/goutil"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/strutil"
)
// InteractDemo command
var InteractDemo = &gcli.Command{
Name: "interact",
Func: interactDemo,
Desc: "the command will show some interactive methods",
Subs: []*gcli.Command{InteractCollectCmd},
Aliases: []string{"itt"},
Config: func(c *gcli.Command) {
c.AddArg("name", "want running interact method name", true)
},
Examples: `{$fullCmd} confirm
{$fullCmd} select
`,
Help: `
Supported interactive methods:
read read user input text
answerIsYes check user answer is Yes
confirm confirm message
select select one from given options
password read user hidden input
multiSelect select multi from given options
`,
}
var funcMap = map[string]func(c *gcli.Command){
"read": demoReadInput,
"select": demoSelect,
"confirm": demoConfirm,
"password": demoPassword,
"ms": demoMultiSelect,
"multiSelect": demoMultiSelect,
"answerIsYes": demoAnswerIsYes,
}
func demoReadInput(c *gcli.Command) {
ans, _ := interact.ReadLine("Your name?")
if ans != "" {
color.Println("Your input: ", ans)
} else {
color.Cyan.Println("No input!")
}
}
func interactDemo(c *gcli.Command, _ []string) error {
name := c.Arg("name").String()
if handler, ok := funcMap[name]; ok {
handler(c)
} else {
return c.NewErrf("want run unknown demo method: %s", name)
}
return nil
}
func demoSelect(_ *gcli.Command) {
color.Green.Println("Thies's An Select Demo")
fmt.Println("----------------------------------------------------------")
ans := interact.SelectOne(
"Your city name(use string slice/array)?",
[]string{"chengdu", "beijing", "shanghai"},
"",
)
color.Info.Println("your select is:", ans)
fmt.Println("----------------------------------------------------------")
ans1 := interact.Choice(
"Your age(use int slice/array)?",
[]int{23, 34, 45},
"",
)
color.Info.Println("your select is:", ans1)
fmt.Println("----------------------------------------------------------")
ans2 := interact.SingleSelect(
"Your city name(use map)?",
map[string]string{"a": "chengdu", "b": "beijing", "c": "shanghai"},
"a",
)
color.Info.Println("your select is:", ans2)
s := interact.NewSelect("Your city", []string{"chengdu", "beijing", "shanghai"})
s.DefOpt = "2"
r := s.Run()
color.Info.Println("your select key:", r.K.String())
color.Info.Println("your select val:", r.String())
}
func demoMultiSelect(_ *gcli.Command) {
color.Green.Println("Thies's An MultiSelect Demo")
ans := interact.MultiSelect(
"Your city name(use array)?",
[]string{"chengdu", "beijing", "shanghai"},
nil,
)
color.Comment.Println("your select is: ", ans)
fmt.Println("----------------------------------------------------------")
ans2 := interact.Checkbox(
"Your city name(use map)?",
map[string]string{"a": "chengdu", "b": "beijing", "c": "shanghai"},
[]string{"a"},
)
color.Comment.Println("your select is:", ans2)
}
func demoConfirm(_ *gcli.Command) {
color.Green.Println("Thies's An Confirm Demo")
if interact.Confirm("Ensure continue") {
fmt.Println(emoji.Render(":smile: Confirmed"))
} else {
color.Warn.Println("Unconfirmed")
}
}
func demoPassword(_ *gcli.Command) {
color.Green.Println("Thies's An ReadPassword Demo")
// hiddenInputTest()
// return
// pwd := interact.GetHiddenInput("Enter Password:", true)
// color.Comment.Println("you input password is: ", pwd)
pwd := interact.ReadPassword()
color.Comment.Println("Your input password is:", pwd)
}
func hiddenInputTest() {
// COMMAND: sh -c 'read -p "Enter Password:" -s user_input && echo $user_input'
// str := fmt.Sprintf(`'read -p "%s" -s user_input && echo $user_input'`, "Enter Password:")
// cmd := exec.CommandContext()
cmd := exec.Command("sh", "-c", `read -p "Enter Password:" -s user_input && echo $user_input`)
err := cmd.Start()
fmt.Println("start", err)
err = cmd.Wait()
fmt.Println("wait", err, cmd.Process.Pid, cmd.ProcessState.Pid())
cmd = exec.Command("sh", "./read-pwd.sh")
bs, err := cmd.Output()
fmt.Println(string(bs), err)
}
func demoAnswerIsYes(_ *gcli.Command) {
}
func demoQuestion(_ *gcli.Command) {
ans := interact.Ask("Your name? ", "", nil, 3)
color.Comment.Println("Your answer is:", ans)
}
// InteractCollectCmd instance.
var InteractCollectCmd = &gcli.Command{
Name: "collect",
Desc: "collect multi input params at once",
Func: func(c *gcli.Command, args []string) error {
vc := interact.NewCollector()
err := vc.AddParams(
cparam.NewStringParam("title", "title name").Config(func(p *cparam.StringParam) {
p.ValidFn = func(val string) error {
return goutil.OrError(strutil.IsBlank(val), errorx.Raw("title is required"))
}
}),
cparam.NewChoiceParam("projects", "select projects").
WithChoices([]string{"user", "order", "goods"}),
)
if err != nil {
return err
}
if err = vc.Run(); err != nil {
return err
}
c.Println("Result:")
dump.P(vc.Results())
return nil
},
}