-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
65 lines (52 loc) · 1.36 KB
/
select.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
package utils
import (
"regexp"
"github.com/AlecAivazis/survey/v2"
"github.com/kamontat/gitgo/config/models"
)
const defaultSelectPage = 5
func askSelect(data *string, message string, config *models.CommitTypeSetting, validates []survey.AskOpt) error {
var prompt survey.Prompt
var pageSize = int(config.Prompt.Select.Page)
var suggestion = config.Prompt.Select.Suggestion
var values = config.Prompt.Select.List()
if pageSize > 0 {
validates = append(validates, survey.WithPageSize(pageSize))
} else {
validates = append(validates, survey.WithPageSize(defaultSelectPage))
}
if suggestion {
prompt = &survey.Input{
Message: message,
Suggest: func(str string) (result []string) {
for _, value := range values {
if match, err := regexp.MatchString(str, value); match && err == nil {
result = append(result, value)
}
}
return
},
}
} else {
if !config.Required {
optional := &models.CommitSelectValue{
Key: "",
Text: "go to next question",
}
values = append(values, optional.Format())
}
prompt = &survey.Select{
Message: message,
Options: values,
PageSize: pageSize,
}
}
err := survey.AskOne(prompt, data, validates...)
if err != nil {
return err
}
// update result and remove formatted string on select type
var result = updateSelectResult(*data)
*data = result
return nil
}