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

Added an option to add a comment to every select's option #418

Merged
merged 14 commits into from May 17, 2022
21 changes: 16 additions & 5 deletions README.md
Expand Up @@ -185,12 +185,23 @@ The user can also press `esc` to toggle the ability cycle through the options wi
By default, the select prompt is limited to showing 7 options at a time
and will paginate lists of options longer than that. This can be changed a number of ways:

```golang
// as a field on a single select
prompt := &survey.MultiSelect{..., PageSize: 10}
#### Select option's description

// or as an option to Ask or AskOne
survey.AskOne(prompt, &days, survey.WithPageSize(10))
luanraithz marked this conversation as resolved.
Show resolved Hide resolved
![Example](img/description-demo.gif)
luanraithz marked this conversation as resolved.
Show resolved Hide resolved

```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
Expand Down
53 changes: 53 additions & 0 deletions examples/meals.go
@@ -0,0 +1,53 @@
package main
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the file meals.go be named something more descriptive of the feature it's demoing, such as select_description.go?


import (
"fmt"

"github.com/AlecAivazis/survey/v2"
)

type Meal struct {
Title string
Comment string
}

func main() {
var meals = []Meal{
{Title: "Bread", Comment: "Good, not so healthy"},
{Title: "Eggs", Comment: "Healthy"},
{Title: "Apple", Comment: ""},
{Title: "Burger", Comment: "Really?"},
}

answers := struct {
Meal string
}{}

titles := make([]string, len(meals))
for i, m := range meals {
titles[i] = m.Title
}
var qs = []*survey.Question{
{
Name: "meal",
Prompt: &survey.Select{
Message: "Choose a meal:",
Options: titles,
Description: func(value string, index int) string {
return meals[index].Comment
},
},
Validate: survey.Required,
luanraithz marked this conversation as resolved.
Show resolved Hide resolved
},
}

// ask the question
err := survey.Ask(qs, &answers)
luanraithz marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
fmt.Println(err.Error())
return
}
// print the answers
fmt.Printf("you picked %s, nice choice.\n", answers.Meal)
}
Binary file added img/description-demo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 17 additions & 5 deletions select.go
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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}} {{- color "reset"}} {{ end }}
{{- color "reset"}}
{{end}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
)
}