-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.go
More file actions
377 lines (361 loc) · 10 KB
/
prompt.go
File metadata and controls
377 lines (361 loc) · 10 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package cli
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/peterh/liner"
"github.com/shopspring/decimal"
"github.com/motki/core/evedb"
"github.com/motki/core/log"
"github.com/motki/core/proto/client"
"github.com/motki/cli/text"
)
// A Prompter handles prompting the user for well-defined input.
type Prompter struct {
logger log.Logger
cli *Server
client client.Client
}
func NewPrompter(cli *Server, cl client.Client, logger log.Logger) *Prompter {
return &Prompter{
logger: logger,
cli: cli,
client: cl,
}
}
// Prompt displays the prompt and returns the string input.
func (p *Prompter) Prompt(prompt string) (string, error) {
return p.PromptWithSuggestion(prompt, "", 0)
}
// PromptWithSuggestion displays prompt and an editable text with cursor at
// given position.
func (p *Prompter) PromptWithSuggestion(prompt string, text string, pos int) (string, error) {
return p.cli.PromptWithSuggestion(prompt, text, pos)
}
// PromptInt prompts the user for a valid integer input.
//
// If defVal is not nil, the prompt will be pre-populated with the default
// value.
//
// Additionally, any number of filter functions can be passed in to perform
// custom validation and filtering on the user's input. Filter functions
// receive the value received from the prompt and return the new value and
// and indicator whether the value is valid.
func (p *Prompter) PromptInt(prompt string, defVal *int, filters ...func(int) (int, bool)) (int, bool) {
var val int
var valStr string
var err error
prompt = fmt.Sprintf("%s: ", prompt)
for {
begin:
if defVal != nil {
valStr, err = p.PromptWithSuggestion(prompt, strconv.Itoa(*defVal), -1)
} else {
valStr, err = p.Prompt(prompt)
}
if err != nil {
if err == liner.ErrPromptAborted {
return 0, false
}
if err == io.EOF {
err = errors.New("unexpected EOF")
fmt.Println()
}
p.logger.Debugf("unable to read input: %s", err.Error())
goto begin
}
val, err = strconv.Atoi(valStr)
if err != nil {
fmt.Println("Invalid value, specify an integer.")
goto begin
}
var ok bool
for _, v := range filters {
val, ok = v(val)
if !ok {
goto begin
}
}
break
}
return val, true
}
// PromptString prompts the user for any string input.
//
// If defVal is not nil, the prompt will be pre-populated with the default
// value.
//
// Additionally, any number of filter funcs can be passed in to perform
// custom validation and filtering on the user's input. Filter functions
// receive the value received from the prompt and return the new value and
// and indicator whether the value is valid.
func (p *Prompter) PromptString(prompt string, defVal *string, filters ...func(string) (string, bool)) (string, bool) {
var val string
var err error
prompt = fmt.Sprintf("%s: ", prompt)
for {
begin:
if defVal != nil {
val, err = p.PromptWithSuggestion(prompt, *defVal, -1)
} else {
val, err = p.Prompt(prompt)
}
if err != nil {
if err == liner.ErrPromptAborted {
return "", false
}
if err == io.EOF {
err = errors.New("unexpected EOF")
fmt.Println()
}
p.logger.Debugf("unable to read input: %s", err.Error())
goto begin
}
var ok bool
for _, v := range filters {
val, ok = v(val)
if !ok {
goto begin
}
}
break
}
return val, true
}
// PromptStringWithArgs prompts the user for any string input.
//
// This function differs from PromptString in that it will split the received
// input by spaces, returning the first value as the main value, and a slice
// of additional arguments.
//
// If defVal is not nil, the prompt will be pre-populated with the default
// value.
//
// Additionally, any number of filter funcs can be passed in to perform
// custom validation and filtering on the user's input. Filter functions
// receive the value received from the prompt and return the new value and
// and indicator whether the value is valid.
func (p *Prompter) PromptStringWithArgs(prompt string, defVal *string, filters ...func(string) (string, bool)) (string, []string, bool) {
var val string
var args []string
var err error
prompt = fmt.Sprintf("%s: ", prompt)
for {
begin:
if defVal != nil {
val, err = p.PromptWithSuggestion(prompt, *defVal, -1)
} else {
val, err = p.Prompt(prompt)
}
if err != nil {
if err == liner.ErrPromptAborted {
return "", nil, false
}
if err == io.EOF {
err = errors.New("unexpected EOF")
fmt.Println()
}
p.logger.Debugf("unable to read input: %s", err.Error())
goto begin
}
parts := strings.Split(val, " ")
val = parts[0]
args = parts[1:]
var ok bool
for _, v := range filters {
val, ok = v(val)
if !ok {
goto begin
}
}
break
}
return val, args, true
}
// PromptDecimal prompts the user for a valid decimal input.
//
// If defVal is not nil, the prompt will be pre-populated with the default
// value.
//
// Additionally, any number of filter funcs can be passed in to perform
// custom validation and filtering on the user's input. Filter functions
// receive the value received from the prompt and return the new value and
// and indicator whether the value is valid.
func (p *Prompter) PromptDecimal(prompt string, defVal *decimal.Decimal, filters ...func(decimal.Decimal) (decimal.Decimal, bool)) (decimal.Decimal, bool) {
var val decimal.Decimal
var valStr string
var defStr string
if defVal != nil {
defStr = defVal.StringFixed(2)
}
var err error
prompt = fmt.Sprintf("%s: ", prompt)
for {
begin:
if defVal != nil {
valStr, err = p.PromptWithSuggestion(prompt, defStr, -1)
} else {
valStr, err = p.Prompt(prompt)
}
if err != nil {
if err == liner.ErrPromptAborted {
return decimal.Zero, false
}
if err == io.EOF {
err = errors.New("unexpected EOF")
fmt.Println()
}
p.logger.Debugf("unable to read input: %s", err.Error())
goto begin
}
val, err = decimal.NewFromString(valStr)
if err != nil {
fmt.Println("Invalid input, specify a valid decimal value.")
goto begin
}
var ok bool
for _, v := range filters {
val, ok = v(val)
if !ok {
goto begin
}
}
break
}
return val, true
}
// PromptRegion prompts the user for a valid region input.
//
// If the user enters an integer, it is treated as the region's Region ID.
// Otherwise, the value is used to lookup regions.
//
// This function also accepts an initial input that should be used to
// as the first round of prompt input.
func (p *Prompter) PromptRegion(prompt string, initialInput string) (*evedb.Region, bool) {
var val *evedb.Region
var id int
var err error
var regions []*evedb.Region
valStr := initialInput
prompt = fmt.Sprintf("%s: ", prompt)
regions, err = p.client.GetRegions()
if err != nil {
p.logger.Warnf("error loading regions: %s", err.Error())
fmt.Println("Error loading regions, try again.")
return nil, false
}
regionIndex := map[string]*evedb.Region{}
for _, region := range regions {
regionIndex[strings.ToUpper(region.Name)] = region
}
for {
// This loop is ordered in such a way that it does the input validation
// first. This allows us to specify an initial input and test that first,
// before actually prompting the user for input.
if valStr == "" {
goto prompt
}
id, err = strconv.Atoi(valStr)
if err != nil {
matches := []*evedb.Region{}
checkVal := strings.ToUpper(valStr)
for capsName, region := range regionIndex {
if strings.Contains(capsName, checkVal) {
matches = append(matches, region)
}
}
fmt.Printf("Top %d results for \"%s\":\n", len(matches), valStr)
for _, r := range matches {
fmt.Printf("%s %s\n", text.PadTextLeft(fmt.Sprintf("%d", r.RegionID), 12), r.Name)
}
goto prompt
}
val, err = p.client.GetRegion(id)
if err != nil {
fmt.Printf("No region exists with ID %d.\n", id)
goto prompt
}
// We have a valid value, break out of the loop.
break
prompt:
valStr, err = p.Prompt(prompt)
if err != nil {
if err == liner.ErrPromptAborted {
return nil, false
}
if err == io.EOF {
err = errors.New("unexpected EOF")
fmt.Println()
}
p.logger.Debugf("unable to read input: %s", err.Error())
goto prompt
}
// Loop back around and check valStr for valid input.
continue
}
return val, true
}
// PromptItemTypeDetail prompts the user for a valid item type input.
//
// If the user enters an integer, it is treated as the item's Type ID.
// Otherwise, the value is used to lookup item types.
//
// This function also accepts an initial input that should be used to
// as the first round of prompt input.
func (p *Prompter) PromptItemTypeDetail(prompt string, initialInput string) (*evedb.ItemTypeDetail, bool) {
var val *evedb.ItemTypeDetail
var id int
var err error
valStr := initialInput
prompt = fmt.Sprintf("%s: ", prompt)
for {
// This loop is ordered in such a way that it does the input validation
// first. This allows us to specify an initial input and test that first,
// before actually prompting the user for input.
if valStr == "" {
goto prompt
}
id, err = strconv.Atoi(valStr)
if err != nil {
its, err := p.client.QueryItemTypes(valStr)
if err != nil || len(its) == 0 {
if err != nil {
p.logger.Debugf("error querying item types: %s", err.Error())
}
fmt.Printf("Nothing found for \"%s\".\n", valStr)
goto prompt
}
fmt.Printf("Top %d results for \"%s\":\n", len(its), valStr)
for _, it := range its {
fmt.Printf("%s %s\n", text.PadTextLeft(fmt.Sprintf("%d", it.ID), 8), it.Name)
}
goto prompt
}
val, err = p.client.GetItemTypeDetail(id)
if err != nil {
fmt.Printf("No item exists with ID %d.\n", id)
p.logger.Warnf("error fetching item type detail: %s", err.Error())
goto prompt
}
// We have a valid value, break out of the loop.
break
prompt:
valStr, err = p.Prompt(prompt)
if err != nil {
if err == liner.ErrPromptAborted {
return nil, false
}
if err == io.EOF {
err = errors.New("unexpected EOF")
fmt.Println()
}
p.logger.Debugf("unable to read input: %s", err.Error())
goto prompt
}
// Loop back around and check valStr for valid input.
continue
}
return val, true
}