| 1 |
package ui |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"regexp" |
| 6 |
|
| 7 |
"github.com/manifoldco/promptui" |
| 8 |
) |
| 9 |
|
| 10 |
type options struct { |
| 11 |
mask rune |
| 12 |
defaultValue string |
| 13 |
value string |
| 14 |
allowEdit bool |
| 15 |
printTemplate string |
| 16 |
promptTemplates *promptui.PromptTemplates |
| 17 |
selectTemplates *promptui.SelectTemplates |
| 18 |
validateFunc promptui.ValidateFunc |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
func (o *options) apply(opts []Option) *options { |
| 23 |
for _, fn := range opts { |
| 24 |
fn(o) |
| 25 |
} |
| 26 |
return o |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func (o *options) valid() bool { |
| 31 |
if o.validateFunc == nil { |
| 32 |
return true |
| 33 |
} |
| 34 |
return o.validateFunc(o.value) == nil |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func (o *options) getValue() (string, error) { |
| 39 |
if o.validateFunc == nil { |
| 40 |
return o.value, nil |
| 41 |
} |
| 42 |
if err := o.validateFunc(o.value); err != nil { |
| 43 |
return "", err |
| 44 |
} |
| 45 |
return o.value, nil |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (o *options) getValueBytes() ([]byte, error) { |
| 50 |
if o.validateFunc == nil { |
| 51 |
return []byte(o.value), nil |
| 52 |
} |
| 53 |
if err := o.validateFunc(o.value); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
return []byte(o.value), nil |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
type Option func(*options) |
| 61 |
|
| 62 |
func extractOptions(args []interface{}) (opts []Option, rest []interface{}) { |
| 63 |
rest = args[:0] |
| 64 |
for _, arg := range args { |
| 65 |
if o, ok := arg.(Option); ok { |
| 66 |
opts = append(opts, o) |
| 67 |
} else { |
| 68 |
rest = append(rest, arg) |
| 69 |
} |
| 70 |
} |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
func WithMask(r rune) Option { |
| 76 |
return func(o *options) { |
| 77 |
o.mask = r |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func WithDefaultValue(s string) Option { |
| 83 |
return func(o *options) { |
| 84 |
o.defaultValue = s |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func WithValue(value string) Option { |
| 91 |
return func(o *options) { |
| 92 |
o.value = value |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func WithAllowEdit(b bool) Option { |
| 98 |
return func(o *options) { |
| 99 |
o.allowEdit = b |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func WithPrintTemplate(template string) Option { |
| 105 |
return func(o *options) { |
| 106 |
o.printTemplate = template |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func WithPromptTemplates(t *promptui.PromptTemplates) Option { |
| 112 |
return func(o *options) { |
| 113 |
o.promptTemplates = t |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func WithSelectTemplates(t *promptui.SelectTemplates) Option { |
| 119 |
return func(o *options) { |
| 120 |
o.selectTemplates = t |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func WithValidateFunc(fn func(string) error) Option { |
| 126 |
return func(o *options) { |
| 127 |
o.validateFunc = fn |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
func WithValidateNotEmpty() Option { |
| 134 |
return WithValidateFunc(NotEmpty()) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
func WithValidateYesNo() Option { |
| 140 |
return WithValidateFunc(YesNo()) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func WithRichPrompt() Option { |
| 145 |
return WithPromptTemplates(PromptTemplates()) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
func WithSimplePrompt() Option { |
| 150 |
return WithPromptTemplates(SimplePromptTemplates()) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
func WithValidateRegexp(re string) Option { |
| 156 |
rx := regexp.MustCompile(re) |
| 157 |
return WithValidateFunc(func(s string) error { |
| 158 |
if rx.MatchString(s) { |
| 159 |
return nil |
| 160 |
} |
| 161 |
return fmt.Errorf("%s does not match the regular expresion %s", s, re) |
| 162 |
}) |
| 163 |
} |