Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 734e799

Browse files
KonkradAlecAivazis
andauthored
Add Description function to multi select (#446)
* feat: Add Description function to multi select * Add multi select description test * reset color * push test for multi select descriptions Co-authored-by: Alec Aivazis <[email protected]>
1 parent a98a037 commit 734e799

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed

multiselect.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type MultiSelect struct {
2929
VimMode bool
3030
FilterMessage string
3131
Filter func(filter string, value string, index int) bool
32+
Description func(value string, index int) string
3233
filter string
3334
selectedIndex int
3435
checked map[int]bool
@@ -43,6 +44,7 @@ type MultiSelectTemplateData struct {
4344
Checked map[int]bool
4445
SelectedIndex int
4546
ShowHelp bool
47+
Description func(value string, index int) string
4648
PageEntries []core.OptionAnswer
4749
Config *PromptConfig
4850

@@ -59,12 +61,19 @@ func (m MultiSelectTemplateData) IterateOption(ix int, opt core.OptionAnswer) in
5961
return copy
6062
}
6163

64+
func (m MultiSelectTemplateData) GetDescription(opt core.OptionAnswer) string {
65+
if m.Description == nil {
66+
return ""
67+
}
68+
return m.Description(opt.Value, opt.Index)
69+
}
70+
6271
var MultiSelectQuestionTemplate = `
6372
{{- define "option"}}
6473
{{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }}{{color "reset"}}{{else}} {{end}}
6574
{{- 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}}
6675
{{- color "reset"}}
67-
{{- " "}}{{- .CurrentOpt.Value}}
76+
{{- " "}}{{- .CurrentOpt.Value}}{{ if ne ($.GetDescription .CurrentOpt) "" }} - {{color "cyan"}}{{ $.GetDescription .CurrentOpt }}{{color "reset"}}{{end}}
6877
{{end}}
6978
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
7079
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
@@ -179,6 +188,7 @@ func (m *MultiSelect) OnChange(key rune, config *PromptConfig) {
179188
SelectedIndex: idx,
180189
Checked: m.checked,
181190
ShowHelp: m.showingHelp,
191+
Description: m.Description,
182192
PageEntries: opts,
183193
Config: config,
184194
}
@@ -272,6 +282,7 @@ func (m *MultiSelect) Prompt(config *PromptConfig) (interface{}, error) {
272282
tmplData := MultiSelectTemplateData{
273283
MultiSelect: *m,
274284
SelectedIndex: idx,
285+
Description: m.Description,
275286
Checked: m.checked,
276287
PageEntries: opts,
277288
Config: config,
@@ -342,6 +353,7 @@ func (m *MultiSelect) Cleanup(config *PromptConfig, val interface{}) error {
342353
Checked: m.checked,
343354
Answer: answer,
344355
ShowAnswer: true,
356+
Description: m.Description,
345357
Config: config,
346358
},
347359
)

multiselect_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func TestMultiSelectRender(t *testing.T) {
2727
Default: []string{"bar", "buz"},
2828
}
2929

30+
descriptions := []string{"oof", "rab", "zab", "zub"}
31+
3032
helpfulPrompt := prompt
3133
helpfulPrompt.Help = "This is helpful"
3234

@@ -127,6 +129,121 @@ func TestMultiSelectRender(t *testing.T) {
127129
"\n",
128130
),
129131
},
132+
{
133+
"description all",
134+
prompt,
135+
MultiSelectTemplateData{
136+
SelectedIndex: 2,
137+
PageEntries: core.OptionAnswerList(prompt.Options),
138+
Checked: map[int]bool{1: true, 3: true},
139+
Description: func(value string, index int) string {
140+
return descriptions[index]
141+
},
142+
},
143+
strings.Join(
144+
[]string{
145+
fmt.Sprintf("%s Pick your words: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]", defaultIcons().Question.Text),
146+
fmt.Sprintf(" %s foo - oof", defaultIcons().UnmarkedOption.Text),
147+
fmt.Sprintf(" %s bar - rab", defaultIcons().MarkedOption.Text),
148+
fmt.Sprintf("%s %s baz - zab", defaultIcons().SelectFocus.Text, defaultIcons().UnmarkedOption.Text),
149+
fmt.Sprintf(" %s buz - zub\n", defaultIcons().MarkedOption.Text),
150+
},
151+
"\n",
152+
),
153+
},
154+
{
155+
"description even",
156+
prompt,
157+
MultiSelectTemplateData{
158+
SelectedIndex: 2,
159+
PageEntries: core.OptionAnswerList(prompt.Options),
160+
Checked: map[int]bool{1: true, 3: true},
161+
Description: func(value string, index int) string {
162+
163+
if index%2 == 0 {
164+
return descriptions[index]
165+
}
166+
167+
return ""
168+
},
169+
},
170+
strings.Join(
171+
[]string{
172+
fmt.Sprintf("%s Pick your words: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]", defaultIcons().Question.Text),
173+
fmt.Sprintf(" %s foo - oof", defaultIcons().UnmarkedOption.Text),
174+
fmt.Sprintf(" %s bar", defaultIcons().MarkedOption.Text),
175+
fmt.Sprintf("%s %s baz - zab", defaultIcons().SelectFocus.Text, defaultIcons().UnmarkedOption.Text),
176+
fmt.Sprintf(" %s buz\n", defaultIcons().MarkedOption.Text),
177+
},
178+
"\n",
179+
),
180+
},
181+
{
182+
"description never",
183+
prompt,
184+
MultiSelectTemplateData{
185+
SelectedIndex: 2,
186+
PageEntries: core.OptionAnswerList(prompt.Options),
187+
Checked: map[int]bool{1: true, 3: true},
188+
Description: func(value string, index int) string {
189+
return ""
190+
},
191+
},
192+
strings.Join(
193+
[]string{
194+
fmt.Sprintf("%s Pick your words: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]", defaultIcons().Question.Text),
195+
fmt.Sprintf(" %s foo", defaultIcons().UnmarkedOption.Text),
196+
fmt.Sprintf(" %s bar", defaultIcons().MarkedOption.Text),
197+
fmt.Sprintf("%s %s baz", defaultIcons().SelectFocus.Text, defaultIcons().UnmarkedOption.Text),
198+
fmt.Sprintf(" %s buz\n", defaultIcons().MarkedOption.Text),
199+
},
200+
"\n",
201+
),
202+
},
203+
{
204+
"description repeat value",
205+
prompt,
206+
MultiSelectTemplateData{
207+
SelectedIndex: 2,
208+
PageEntries: core.OptionAnswerList(prompt.Options),
209+
Checked: map[int]bool{1: true, 3: true},
210+
Description: func(value string, index int) string {
211+
return value
212+
},
213+
},
214+
strings.Join(
215+
[]string{
216+
fmt.Sprintf("%s Pick your words: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]", defaultIcons().Question.Text),
217+
fmt.Sprintf(" %s foo - foo", defaultIcons().UnmarkedOption.Text),
218+
fmt.Sprintf(" %s bar - bar", defaultIcons().MarkedOption.Text),
219+
fmt.Sprintf("%s %s baz - baz", defaultIcons().SelectFocus.Text, defaultIcons().UnmarkedOption.Text),
220+
fmt.Sprintf(" %s buz - buz\n", defaultIcons().MarkedOption.Text),
221+
},
222+
"\n",
223+
),
224+
},
225+
{
226+
"description print index",
227+
prompt,
228+
MultiSelectTemplateData{
229+
SelectedIndex: 2,
230+
PageEntries: core.OptionAnswerList(prompt.Options),
231+
Checked: map[int]bool{1: true, 3: true},
232+
Description: func(value string, index int) string {
233+
return fmt.Sprint(index)
234+
},
235+
},
236+
strings.Join(
237+
[]string{
238+
fmt.Sprintf("%s Pick your words: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]", defaultIcons().Question.Text),
239+
fmt.Sprintf(" %s foo - 0", defaultIcons().UnmarkedOption.Text),
240+
fmt.Sprintf(" %s bar - 1", defaultIcons().MarkedOption.Text),
241+
fmt.Sprintf("%s %s baz - 2", defaultIcons().SelectFocus.Text, defaultIcons().UnmarkedOption.Text),
242+
fmt.Sprintf(" %s buz - 3\n", defaultIcons().MarkedOption.Text),
243+
},
244+
"\n",
245+
),
246+
},
130247
}
131248

132249
for _, test := range tests {

tests/multiselect.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"github.com/AlecAivazis/survey/v2"
5-
"github.com/AlecAivazis/survey/v2/tests/util"
7+
TestUtil "github.com/AlecAivazis/survey/v2/tests/util"
68
)
79

810
var answer = []string{}
@@ -42,6 +44,16 @@ var table = []TestUtil.TestTableEntry{
4244
Default: []string{"Sundayaa"},
4345
}, &answer, nil,
4446
},
47+
{
48+
"descriptions", &survey.MultiSelect{
49+
Message: "What days do you prefer:",
50+
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
51+
Description: func(value string, index int) string {
52+
return value + fmt.Sprint(index)
53+
54+
},
55+
}, &answer, nil,
56+
},
4557
}
4658

4759
func main() {

0 commit comments

Comments
 (0)