-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiline.go
187 lines (145 loc) · 3.62 KB
/
multiline.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
package surveyexpect
import (
"fmt"
"strings"
)
var (
_ Prompt = (*MultilinePrompt)(nil)
_ Answer = (*MultilineAnswer)(nil)
)
// MultilinePrompt is an expectation of survey.Multiline.
type MultilinePrompt struct {
*basePrompt
message string
answer Step
}
// Interrupt marks the answer is interrupted.
//
// Survey.ExpectMultiline("Enter your message:").
// Interrupt()
func (p *MultilinePrompt) Interrupt() {
p.lock()
defer p.unlock()
p.answer = pressInterrupt()
p.timesLocked(1)
}
// Answer sets the answer to the input prompt.
//
// Survey.ExpectMultiline("Enter your message:").
// Answer("hello world")
func (p *MultilinePrompt) Answer(answer string) *MultilineAnswer {
p.lock()
defer p.unlock()
a := newMultilineAnswer(p, answer)
p.answer = a
return a
}
// Do runs the step.
func (p *MultilinePrompt) Do(c Console) error {
if _, err := c.ExpectString(p.message); err != nil {
return err
}
_ = waitForCursorTwice(c) //nolint: errcheck
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 *MultilinePrompt) String() string {
var sb stringsBuilder
sb.WriteLabelLinef("Expect", "Multiline Prompt").
WriteLabelLinef("Message", "%q", p.message).
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.ExpectMultiline("Enter your message:").
// Answer("hello world").
// Once()
func (p *MultilinePrompt) Once() *MultilinePrompt {
return p.Times(1)
}
// Twice indicates that the message should only be asked twice.
//
// Survey.ExpectMultiline("Enter your message:").
// Answer("hello world").
// Twice()
func (p *MultilinePrompt) Twice() *MultilinePrompt {
return p.Times(2)
}
// Times indicates that the message should only be asked the indicated number of times.
//
// Survey.ExpectMultiline("Enter your message:").
// Answer("hello world").
// Times(5)
func (p *MultilinePrompt) Times(i int) *MultilinePrompt {
p.times(i)
return p
}
// MultilineAnswer is an answer for password question.
type MultilineAnswer struct {
parent *MultilinePrompt
answer string
interrupted bool
}
// Do runs the step.
// nolint: errcheck,gosec,nolintlint
func (a *MultilineAnswer) Do(c Console) error {
if a.interrupted {
c.Send(a.answer)
c.ExpectEOF()
return nil
}
lines := strings.Split(a.answer, "\n")
lines = append(lines, "")
if a.answer != "" {
lines = append(lines, "")
}
cnt := len(lines) - 1
for i, l := range lines {
c.SendLine(l)
if i < cnt {
_ = waitForCursorTwice(c)
}
}
return nil
}
// Interrupted expects the answer will be interrupted.
func (a *MultilineAnswer) Interrupted() {
a.parent.lock()
defer a.parent.unlock()
a.interrupted = true
}
// String represents the answer as a string.
func (a *MultilineAnswer) String() string {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, "%q", a.answer)
if a.interrupted {
_, _ = sb.WriteString(" and get interrupted")
}
return sb.String()
}
func newMultiline(parent *Survey, message string) *MultilinePrompt {
p := &MultilinePrompt{
basePrompt: &basePrompt{parent: parent},
message: message,
}
p.answer = newMultilineAnswer(p, "")
return p
}
func newMultilineAnswer(parent *MultilinePrompt, answer string) *MultilineAnswer {
return &MultilineAnswer{
parent: parent,
answer: answer,
}
}