-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
117 lines (100 loc) · 2.22 KB
/
prompt.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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package prompt prompts user for feedback.
package prompt
import (
"fmt"
)
type Option struct {
key interface{}
prompt string
validation string
other bool
Chosen bool // Set to true if chosen.
Value string // Value used if chosen and option is "other".
}
type OptionType byte
const (
TypeSelectOne OptionType = iota // Allow user to choose single option.
TypeSelectMultiple // Allow user to choose multiple options.
)
type Response byte
const (
RespAnswer Response = iota
RespCancel
)
func NewOption(key interface{}, prompt string, other bool) Option {
return Option{key: key, prompt: prompt, other: other}
}
func (opt Option) Key() interface{} {
return opt.key
}
func (opt Option) Prompt() string {
return opt.prompt
}
func (opt Option) Other() bool {
return opt.other
}
func (opt Option) Validation() string {
return opt.validation
}
func (opt Option) String() string {
if opt.other {
return opt.Value
}
return fmt.Sprintf("%v", opt.key)
}
func ValidateOption(opt Option, validation string) Option {
return Option{
key: opt.key,
prompt: opt.prompt,
other: opt.other,
validation: validation,
Chosen: opt.Chosen,
Value: opt.Value,
}
}
type Question struct {
Error string
Prompt string
Type OptionType
Options []Option
}
func (q *Question) AnswerMultiple(must bool) []*Option {
ans := []*Option{}
for i := range q.Options {
o := &q.Options[i]
if o.Chosen {
ans = append(ans, o)
}
}
if must && len(ans) == 0 {
panic("If no option is chosen, response must be cancelled")
}
return ans
}
func (q *Question) AnswerSingle(must bool) *Option {
var ans *Option
if q.Type != TypeSelectOne {
panic("Question Type should match answer type")
}
found := false
for i := range q.Options {
o := &q.Options[i]
if found && o.Chosen {
panic("Must only respond with single option")
}
if o.Chosen {
found = true
ans = o
}
}
if must && !found {
panic("If no option is chosen, response must be cancelled")
}
return ans
}
type Prompt interface {
Ask(q *Question) (Response, error)
}