-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
340 lines (276 loc) · 7.06 KB
/
select.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
328
329
330
331
332
333
334
335
336
337
338
339
340
package prompt
import (
"fmt"
"github.com/rivo/uniseg"
"strings"
)
const defaultNumLinesShown = 7
type SelectionOption struct {
ID string
Name string
Description string
}
type line struct {
optionIndex int
text string
isFirst bool
}
type Select struct {
base
// The question to display to the user
Question string
// Array of options for the user to select
Options []SelectionOption
// The number of lines that will be shown at a time
// Default is 7
NumLinesShown int
// Called when a key is pressed but before it is processed. Return `false` to cancel the event.
OnKeyFunc func(Prompt, Key) bool
offset int
cursor int
filter string
lines []line
}
func (s *Select) Show() error {
err := s.show()
if err != nil {
return err
}
s.computeLines()
s.offset = s.NumLinesToShow() / 2
s.output.hideCursor()
s.render(false)
for s.State() == Showing {
nextKey, err := s.nextKey()
if err != nil {
s.output.showCursor()
s.finish()
return err
}
s.handleInput(nextKey)
}
return nil
}
func (s *Select) handleInput(input Key) {
if s.OnKeyFunc != nil && !s.OnKeyFunc(s, input) {
return
}
if input == ControlUp {
if len(s.filteredOptions()) > 1 {
for {
s.cursor--
if s.cursor < 0 {
s.cursor = len(s.lines) - 1
}
curLine := s.lines[s.cursor]
// If we haven't reached the first line then keep going.
if !curLine.isFirst {
continue
}
// If we're on a filtered out line then keep going
if !s.matchesFilter(s.Options[curLine.optionIndex]) {
continue
}
// Otherwise, we've completed our search.
break
}
}
} else if input == ControlDown {
if len(s.filteredOptions()) > 1 {
for {
s.cursor++
if s.cursor == len(s.lines) {
s.cursor = 0
}
curLine := s.lines[s.cursor]
// If we're on a filtered out line then keep going
if !s.matchesFilter(s.Options[curLine.optionIndex]) {
continue
}
// Otherwise, we've completed our search.
break
}
}
} else if input == ControlEnter {
if len(s.filteredOptions()) != 0 {
s.output.showCursor()
s.render(true)
s.finish()
return
}
} else if input.IsText() {
s.filter += string(input.Rune())
if len(s.filteredOptions()) > 0 && !s.matchesFilter(s.curOption()) {
closestValidLine := -1
for i, line := range s.lines {
// We're only interested in the first lines.
if !line.isFirst {
continue
}
// This line doesn't match the filter
if !s.matchesFilter(s.Options[line.optionIndex]) {
continue
}
if closestValidLine == -1 {
closestValidLine = i
} else if abs(s.cursor-i) < abs(s.cursor-closestValidLine) {
closestValidLine = i
}
}
s.cursor = closestValidLine
}
} else if input == ControlBackspace {
if s.filter != "" {
s.filter = s.filter[:len(s.filter)-1]
}
}
if s.State() != Waiting {
s.render(false)
}
}
func (s *Select) curOption() SelectionOption {
return s.Options[s.lines[s.cursor].optionIndex]
}
func (s *Select) matchesFilter(option SelectionOption) bool {
if s.filter == "" {
return true
}
if strings.Contains(strings.ToLower(option.Name), strings.ToLower(s.filter)) {
return true
}
return false
}
func (s *Select) filteredOptions() []SelectionOption {
var result []SelectionOption
for _, option := range s.Options {
if s.matchesFilter(option) {
result = append(result, option)
}
}
return result
}
func (s *Select) NumLinesToShow() int {
if s.NumLinesShown <= 0 {
return min(defaultNumLinesShown, len(s.lines))
}
return min(s.NumLinesShown, len(s.lines))
}
func (s *Select) render(isFinished bool) {
s.output.clear()
s.output.writeColor("? ", colorGreen)
s.output.write(s.Question)
s.output.write(": ")
if isFinished {
s.output.writeColor(fmt.Sprintf("%s: %s", s.Response().Name, s.Response().Description), colorCyan)
return
} else {
s.output.writeColor("(Use arrow keys) (Type to filter)", colorGreen)
}
s.output.nextLine()
cursorLine := s.lines[s.cursor]
startOffset := (-s.NumLinesToShow() / 2) + s.offset
endOffset := (s.NumLinesToShow() / 2) + s.offset
fillRemainingWithBlank := false
if len(s.filteredOptions()) == 0 {
s.output.writeColorLn(s.filter, colorRed)
fillRemainingWithBlank = true
startOffset++
}
for offset := startOffset; offset <= endOffset; offset++ {
lineIndex := s.actualLineNumber(s.cursor + offset)
// We've looped back to the start
if offset != startOffset && lineIndex == s.actualLineNumber(s.cursor+startOffset) {
fillRemainingWithBlank = true
}
if fillRemainingWithBlank {
s.output.nextLine()
continue
}
line := s.lines[lineIndex]
option := s.Options[line.optionIndex]
if !s.matchesFilter(option) {
endOffset++
continue
}
if lineIndex == s.cursor {
s.output.writeColor("> ", colorCyan)
} else {
s.output.write(" ")
}
if line.isFirst {
option := s.Options[line.optionIndex]
redRemaining := 0
for i, c := range line.text {
if i >= len(option.Name) {
if line.optionIndex == cursorLine.optionIndex {
s.output.writeColor(string(c), colorCyan)
} else {
s.output.write(string(c))
}
} else {
if s.filter != "" && strings.HasPrefix(strings.ToLower(option.Name[i:]), strings.ToLower(s.filter)) {
s.output.writeColor(string(c), colorRed)
redRemaining = len(s.filter) - 1
} else if redRemaining > 0 {
s.output.writeColor(string(c), colorRed)
redRemaining--
} else if line.optionIndex == cursorLine.optionIndex {
s.output.writeColor(string(c), colorCyan)
} else {
s.output.write(string(c))
}
}
}
s.output.nextLine()
} else {
if line.optionIndex == cursorLine.optionIndex {
s.output.writeColorLn(line.text, colorCyan)
} else {
s.output.writeLn(line.text)
}
}
}
if len(s.lines) > s.NumLinesToShow() {
s.output.writeColor("(Move up and down to reveal more choices)", colorGreen)
}
s.output.flush()
}
func (s *Select) computeLines() {
s.lines = make([]line, 0, len(s.Options))
longestName := s.longestName()
for optionIndex, option := range s.Options {
wrappedDescription := wrapString(option.Description, s.output.outputWidth-longestName-4)
for i, wrapped := range wrappedDescription {
var currentLineText string
if i == 0 {
padding := strings.Repeat(" ", longestName-uniseg.GraphemeClusterCount(option.Name))
currentLineText = fmt.Sprintf("%s: %s%s", option.Name, padding, wrapped)
} else {
currentLineText = fmt.Sprintf("%s %s", strings.Repeat(" ", longestName), wrapped)
}
s.lines = append(s.lines, line{
optionIndex: optionIndex,
text: currentLineText,
isFirst: i == 0,
})
}
}
}
func (s *Select) actualLineNumber(line int) int {
if line < 0 {
return line + len(s.lines)
} else if line >= len(s.lines) {
return line - len(s.lines)
}
return line
}
func (s *Select) Response() SelectionOption {
return s.curOption()
}
func (s *Select) longestName() int {
longestName := 0
for _, option := range s.Options {
longestName = max(uniseg.GraphemeClusterCount(option.Name), longestName)
}
return longestName
}