-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
40 lines (31 loc) · 904 Bytes
/
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
package prompt
import (
"fmt"
survey "github.com/AlecAivazis/survey/v2"
)
// SelectQuestion is a question that asks the user to select an option from a list.
type SelectQuestion struct {
enableFilter bool
}
func NewSelectQuestion() *SelectQuestion {
return &SelectQuestion{}
}
// EnableFilter enables or disables the filter feature of the select question.
func (q *SelectQuestion) EnableFilter(enable bool) {
q.enableFilter = enable
}
// Ask asks a question with a list of options.
func (q *SelectQuestion) Ask(question string, options []string) (string, error) {
if len(options) == 0 {
return "", fmt.Errorf("options must not be empty")
}
answer := ""
err := survey.AskOne(&survey.Select{
Message: question,
Options: options,
}, &answer, survey.WithKeepFilter(q.enableFilter))
if err != nil {
return "", fmt.Errorf("failed to ask question: %w", err)
}
return answer, nil
}