This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an option to add a comment to every select's option (#418)
Co-authored-by: Mislav Marohnić <[email protected]>
- Loading branch information
1 parent
93657ef
commit c2be27f
Showing
3 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters