Debian Codegraph @

ui/options.go

raw symbols SCIP
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 // apply applies the given options.
22 func (o *options) apply(opts []Option) *options {
23 for _, fn := range opts {
24 fn(o)
25 }
26 return o
27 }
28
29 // valid returns true if the validate function passes on the value.
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 // getValue validates the value and returns it.
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 // getValueBytes validates the value and returns it as a byte slice.
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 // Option is the type of the functions that modify the prompt options.
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 // WithMask adds a mask to a prompt.
75 func WithMask(r rune) Option {
76 return func(o *options) {
77 o.mask = r
78 }
79 }
80
81 // WithDefaultValue adds a custom string as the default value.
82 func WithDefaultValue(s string) Option {
83 return func(o *options) {
84 o.defaultValue = s
85 }
86 }
87
88 // WithValue sets a custom string as the result of a prompt. If value is set,
89 // the prompt won't be displayed.
90 func WithValue(value string) Option {
91 return func(o *options) {
92 o.value = value
93 }
94 }
95
96 // WithAllowEdit if true, let's the user edit the default value set.
97 func WithAllowEdit(b bool) Option {
98 return func(o *options) {
99 o.allowEdit = b
100 }
101 }
102
103 // WithPrintTemplate sets the template to use on the print methods.
104 func WithPrintTemplate(template string) Option {
105 return func(o *options) {
106 o.printTemplate = template
107 }
108 }
109
110 // WithPromptTemplates adds a custom template to a prompt.
111 func WithPromptTemplates(t *promptui.PromptTemplates) Option {
112 return func(o *options) {
113 o.promptTemplates = t
114 }
115 }
116
117 // WithSelectTemplates adds a custom template to a select.
118 func WithSelectTemplates(t *promptui.SelectTemplates) Option {
119 return func(o *options) {
120 o.selectTemplates = t
121 }
122 }
123
124 // WithValidateFunc adds a custom validation function to a prompt.
125 func WithValidateFunc(fn func(string) error) Option {
126 return func(o *options) {
127 o.validateFunc = fn
128 }
129 }
130
131 // WithValidateNotEmpty adds a custom validation function to a prompt that
132 // checks that the propted string is not empty.
133 func WithValidateNotEmpty() Option {
134 return WithValidateFunc(NotEmpty())
135 }
136
137 // WithValidateYesNo adds a custom validation function to a prompt for a Yes/No
138 // prompt.
139 func WithValidateYesNo() Option {
140 return WithValidateFunc(YesNo())
141 }
142
143 // WithRichPrompt add the template option with rich templates.
144 func WithRichPrompt() Option {
145 return WithPromptTemplates(PromptTemplates())
146 }
147
148 // WithSimplePrompt add the template option with simple templates.
149 func WithSimplePrompt() Option {
150 return WithPromptTemplates(SimplePromptTemplates())
151 }
152
153 // WithValidateRegexp checks a prompt answer with a regular expression. If the
154 // regular expression is not a valid one, the option will panic.
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 }