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
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -191,6 +191,22 @@ prompt := &survey.MultiSelect{..., PageSize: 10}

// 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

#### 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
Expand Down
47 changes: 47 additions & 0 deletions 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)
}
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 }}{{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,
},
)
}