From c2be27f4a7faa58a920ea402d0984647a5b8deb2 Mon Sep 17 00:00:00 2001 From: Luan Raithz Machado Date: Tue, 17 May 2022 08:32:41 -0300 Subject: [PATCH] Added an option to add a comment to every select's option (#418) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- README.md | 16 ++++++++++++ examples/select_description.go | 47 ++++++++++++++++++++++++++++++++++ select.go | 22 ++++++++++++---- 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 examples/select_description.go diff --git a/README.md b/README.md index 92997f62..a9081af4 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,22 @@ prompt := &survey.MultiSelect{..., PageSize: 10} // or as an option to Ask or AskOne survey.AskOne(prompt, &days, survey.WithPageSize(10)) + +#### Select option's description + +```golang +color := "" +prompt := &survey.Select{ + Message: "Choose a color:", + Options: []string{"red", "blue", "green"}, + Description: func(value string, index int) string { + if value == "red" { + return "My favorite color" + } + return "" + }, +} +survey.AskOne(prompt, &color) ``` ### MultiSelect diff --git a/examples/select_description.go b/examples/select_description.go new file mode 100644 index 00000000..bc1568ad --- /dev/null +++ b/examples/select_description.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + + "github.com/AlecAivazis/survey/v2" +) + +type Meal struct { + Title string + Comment string +} + +func main() { + var meals = []Meal{ + {Title: "Bread", Comment: "Contains gluten"}, + {Title: "Eggs", Comment: "Free-range"}, + {Title: "Apple", Comment: ""}, + {Title: "Burger", Comment: "Veggie patties available"}, + } + + titles := make([]string, len(meals)) + for i, m := range meals { + titles[i] = m.Title + } + var qs = &survey.Select{ + Message: "Choose a meal:", + Options: titles, + Description: func(value string, index int) string { + return meals[index].Comment + }, + } + + answerIndex := 0 + + // ask the question + err := survey.AskOne(qs, &answerIndex) + + if err != nil { + fmt.Println(err.Error()) + return + } + + meal := meals[answerIndex] + // print the answers + fmt.Printf("you picked %s, nice choice.\n", meal.Title) +} diff --git a/select.go b/select.go index 5c4996f9..494f15d1 100644 --- a/select.go +++ b/select.go @@ -28,6 +28,7 @@ type Select struct { VimMode bool FilterMessage string Filter func(filter string, value string, index int) bool + Description func(value string, index int) string filter string selectedIndex int useDefault bool @@ -42,6 +43,7 @@ type SelectTemplateData struct { Answer string ShowAnswer bool ShowHelp bool + Description func(value string, index int) string Config *PromptConfig // These fields are used when rendering an individual option @@ -57,10 +59,17 @@ func (s SelectTemplateData) IterateOption(ix int, opt core.OptionAnswer) interfa return copy } +func (s SelectTemplateData) GetDescription(opt core.OptionAnswer) string { + if s.Description == nil { + return "" + } + return s.Description(opt.Value, opt.Index) +} + var SelectQuestionTemplate = ` {{- define "option"}} {{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }} {{else}}{{color "default"}} {{end}} - {{- .CurrentOpt.Value}} + {{- .CurrentOpt.Value}}{{ if ne ($.GetDescription .CurrentOpt) "" }} - {{color "cyan"}}{{ $.GetDescription .CurrentOpt }}{{end}} {{- color "reset"}} {{end}} {{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}} @@ -171,6 +180,7 @@ func (s *Select) OnChange(key rune, config *PromptConfig) bool { Select: *s, SelectedIndex: idx, ShowHelp: s.showingHelp, + Description: s.Description, PageEntries: opts, Config: config, } @@ -257,6 +267,7 @@ func (s *Select) Prompt(config *PromptConfig) (interface{}, error) { tmplData := SelectTemplateData{ Select: *s, SelectedIndex: idx, + Description: s.Description, ShowHelp: s.showingHelp, PageEntries: opts, Config: config, @@ -340,10 +351,11 @@ func (s *Select) Cleanup(config *PromptConfig, val interface{}) error { return s.Render( SelectQuestionTemplate, SelectTemplateData{ - Select: *s, - Answer: val.(core.OptionAnswer).Value, - ShowAnswer: true, - Config: config, + Select: *s, + Answer: val.(core.OptionAnswer).Value, + ShowAnswer: true, + Description: s.Description, + Config: config, }, ) }