File: select_description.go

package info (click to toggle)
golang-github-alecaivazis-survey 2.3.6%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 636 kB
  • sloc: makefile: 12
file content (47 lines) | stat: -rw-r--r-- 849 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)
}