-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
327 lines (266 loc) · 7.26 KB
/
input.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package surveyexpect
import (
"fmt"
"strings"
)
var (
_ Prompt = (*InputPrompt)(nil)
_ Answer = (*InputAnswer)(nil)
)
// InputPrompt is an expectation of survey.Input.
type InputPrompt struct {
*basePrompt
message string
answer Step
}
// ShowHelp sets help for the expectation.
//
// Survey.ExpectInput("Enter your name:").
// ShowHelp("It's your full name")
func (p *InputPrompt) ShowHelp(help string, options ...string) {
p.lock()
defer p.unlock()
p.answer = helpAnswer(help, options...)
p.timesLocked(1)
}
// Interrupt marks the answer is interrupted.
//
// Survey.ExpectInput("Enter your name:").
// Interrupt()
func (p *InputPrompt) Interrupt() {
p.lock()
defer p.unlock()
p.answer = interruptAnswer()
p.timesLocked(1)
}
// Answer sets the answer to the input prompt.
//
// Survey.ExpectInput("Enter your name:").
// Answer("johnny")
func (p *InputPrompt) Answer(answer string) *InputAnswer {
p.lock()
defer p.unlock()
a := newInputAnswer(p, answer)
p.answer = a
return a
}
// Type starts a sequence of steps to interact with suggestion mode.
//
// Survey.ExpectInput("Enter your name:").
// Type("johnny")
func (p *InputPrompt) Type(s string) *InputSuggestionSteps {
p.lock()
defer p.unlock()
a := newInputSuggestionSteps(p, typeAnswer(s))
p.answer = a
return a
}
// Tab starts a sequence of steps to interact with suggestion mode. Default is 1 when omitted.
//
// Survey.ExpectInput("Enter your name:").
// Tab()
func (p *InputPrompt) Tab(times ...int) *InputSuggestionSteps {
p.lock()
defer p.unlock()
a := newInputSuggestionSteps(p, repeatStep(pressTab(), times...)...)
p.answer = a
return a
}
// Do runs the step.
func (p *InputPrompt) Do(c Console) error {
if _, err := c.ExpectString(p.message); err != nil {
return err
}
err := p.answer.Do(c)
if err != nil && !IsInterrupted(err) {
return err
}
p.lock()
defer p.unlock()
p.repeatability--
p.totalCalls++
return p.isDoneLocked(err)
}
// String represents the expectation as a string.
func (p *InputPrompt) String() string {
var sb stringsBuilder
sb.WriteLabelLinef("Expect", "Input Prompt").
WriteLabelLinef("Message", "%q", p.message)
if steps, ok := p.answer.(*InputSuggestionSteps); ok {
sb.WriteString(steps.String())
} else {
sb.WriteLabelLinef("Answer", p.answer.String())
}
if p.repeatability > 0 && (p.totalCalls != 0 || p.repeatability != 1) {
sb.WriteLinef("(called: %d time(s), remaining: %d time(s))", p.totalCalls, p.repeatability)
}
return sb.String()
}
// Once indicates that the message should only be asked once.
//
// Survey.ExpectInput("Enter your name:").
// Answer("johnny").
// Once()
func (p *InputPrompt) Once() *InputPrompt {
return p.Times(1)
}
// Twice indicates that the message should only be asked twice.
//
// Survey.ExpectInput("Enter your name:").
// Answer("johnny").
// Twice()
func (p *InputPrompt) Twice() *InputPrompt {
return p.Times(2)
}
// Times indicates that the message should only be asked the indicated number of times.
//
// Survey.ExpectInput("Enter your name:").
// Answer("johnny").
// Times(5)
func (p *InputPrompt) Times(i int) *InputPrompt {
p.times(i)
return p
}
// InputAnswer is an answer for password question.
type InputAnswer struct {
parent *InputPrompt
answer string
interrupted bool
}
// Do runs the step.
// nolint: errcheck,gosec,nolintlint
func (a *InputAnswer) Do(c Console) error {
if a.interrupted {
c.Send(a.answer)
c.ExpectEOF()
return nil
}
c.SendLine(a.answer)
return nil
}
// Interrupted expects the answer will be interrupted.
func (a *InputAnswer) Interrupted() {
a.parent.lock()
defer a.parent.unlock()
a.interrupted = true
}
// String represents the answer as a string.
func (a *InputAnswer) String() string {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, "%q", a.answer)
if a.interrupted {
_, _ = sb.WriteString(" and get interrupted")
}
return sb.String()
}
func newInput(parent *Survey, message string) *InputPrompt {
return &InputPrompt{
basePrompt: &basePrompt{parent: parent},
message: message,
answer: noAnswer(),
}
}
func newInputAnswer(parent *InputPrompt, answer string) *InputAnswer {
return &InputAnswer{
parent: parent,
answer: answer,
}
}
// InputSuggestionSteps is a sequence of steps when user is in suggestion mode.
type InputSuggestionSteps struct {
parent *InputPrompt
steps *InlineSteps
}
func (a *InputSuggestionSteps) append(steps ...Step) *InputSuggestionSteps {
a.parent.lock()
defer a.parent.unlock()
a.steps.Append(steps...)
return a
}
// Tab sends the TAB key the indicated times. Default is 1 when omitted.
//
// Survey.ExpectInput("Enter your name:").
// Type("hello").
// Tab(5)
func (a *InputSuggestionSteps) Tab(times ...int) *InputSuggestionSteps {
return a.append(repeatStep(pressTab(), times...)...).
append(expectString(a.parent.message), expectString(`[Use arrows to move, enter to select, type to continue]`))
}
// Esc sends the ESC key.
//
// Survey.ExpectInput("Enter your name:").
// Type("hello").
// Esc()
func (a *InputSuggestionSteps) Esc() *InputSuggestionSteps {
return a.append(pressEsc(), expectString(a.parent.message), expectString(`for suggestions]`))
}
// Enter sends the ENTER key and ends the sequence.
//
// Survey.ExpectInput("Enter your name:").
// Type("hello").
// Enter()
func (a *InputSuggestionSteps) Enter() {
a.append(pressEnter())
a.steps.Close()
}
// Interrupt sends ^C and closes the suggestions.
//
// Survey.ExpectInput("Enter your name:").
// Type("johnny").
// Interrupt()
func (a *InputSuggestionSteps) Interrupt() *InputSuggestionSteps {
a.append(pressInterrupt())
return a
}
// MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.
//
// Survey.ExpectInput("Enter your name:").
// Tab().
// MoveUp(5)
func (a *InputSuggestionSteps) MoveUp(times ...int) *InputSuggestionSteps {
return a.append(repeatStep(pressArrowUp(), times...)...)
}
// MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.
//
// Survey.ExpectInput("Enter your name:").
// Tab().
// MoveDown(5)
func (a *InputSuggestionSteps) MoveDown(times ...int) *InputSuggestionSteps {
return a.append(repeatStep(pressArrowDown(), times...)...)
}
// Delete sends the DELETE key the indicated times. Default is 1 when omitted.
//
// Survey.ExpectInput("Enter your name:").
// Type("johnny").
// Delete(5)
func (a *InputSuggestionSteps) Delete(times ...int) *InputSuggestionSteps {
return a.append(repeatStep(pressDelete(), times...)...)
}
// Type sends a string without enter.
//
// Survey.ExpectInput("Enter your name:").
// Type("johnny").
// Tab().
// Type(".c").
// Enter()
func (a *InputSuggestionSteps) Type(s string) *InputSuggestionSteps {
return a.append(typeAnswer(s))
}
// ExpectSuggestions expects a list of suggestions.
func (a *InputSuggestionSteps) ExpectSuggestions(suggestions ...string) *InputSuggestionSteps {
return a.append(expectSelect(suggestions...))
}
// Do runs the step.
func (a *InputSuggestionSteps) Do(c Console) error {
return a.steps.Do(c)
}
// String represents the answer as a string.
func (a *InputSuggestionSteps) String() string {
return a.steps.String()
}
func newInputSuggestionSteps(parent *InputPrompt, initialSteps ...Step) *InputSuggestionSteps {
return &InputSuggestionSteps{
parent: parent,
steps: inlineSteps(initialSteps...),
}
}