@@ -35,6 +35,21 @@ func init() {
35
35
{{- template "option" $.IterateOption $ix $option}}
36
36
{{- end}}
37
37
{{- end }}`
38
+
39
+ survey .MultiSelectQuestionTemplate = `
40
+ {{- define "option"}}
41
+ {{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }}{{color "reset"}}{{else}} {{end}}
42
+ {{- if index .Checked .CurrentOpt.Index }}{{color .Config.Icons.MarkedOption.Format }} {{ .Config.Icons.MarkedOption.Text }} {{else}}{{color .Config.Icons.UnmarkedOption.Format }} {{ .Config.Icons.UnmarkedOption.Text }} {{end}}
43
+ {{- color "reset"}}
44
+ {{- " "}}{{- .CurrentOpt.Value}}
45
+ {{end}}
46
+ {{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
47
+ {{- if not .ShowAnswer }}
48
+ {{- "\n"}}
49
+ {{- range $ix, $option := .PageEntries}}
50
+ {{- template "option" $.IterateOption $ix $option}}
51
+ {{- end}}
52
+ {{- end}}`
38
53
}
39
54
40
55
type SelectOptions struct {
@@ -118,6 +133,30 @@ func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
118
133
return value , err
119
134
}
120
135
136
+ func MultiSelect (cmd * cobra.Command , items []string ) ([]string , error ) {
137
+ // Similar hack is applied to Select()
138
+ if flag .Lookup ("test.v" ) != nil {
139
+ return items , nil
140
+ }
141
+
142
+ prompt := & survey.MultiSelect {
143
+ Message : "Options:" ,
144
+ Options : items ,
145
+ Default : items ,
146
+ }
147
+
148
+ var values []string
149
+ err := survey .AskOne (prompt , & values , survey .WithStdio (fileReadWriter {
150
+ Reader : cmd .InOrStdin (),
151
+ }, fileReadWriter {
152
+ Writer : cmd .OutOrStdout (),
153
+ }, cmd .OutOrStdout ()))
154
+ if errors .Is (err , terminal .InterruptErr ) {
155
+ return nil , Canceled
156
+ }
157
+ return values , err
158
+ }
159
+
121
160
type fileReadWriter struct {
122
161
io.Reader
123
162
io.Writer
0 commit comments