From 364c1ed8c663818461b438ecad1bd1b635cccba2 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 19 Dec 2023 09:58:59 -0500 Subject: [PATCH 1/2] docs(example): simple example showing conditional options --- examples/conditional/main.go | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 examples/conditional/main.go diff --git a/examples/conditional/main.go b/examples/conditional/main.go new file mode 100644 index 00000000..b55c2739 --- /dev/null +++ b/examples/conditional/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "fmt" + "os" + + "github.com/charmbracelet/huh" +) + +type consumable int + +const ( + fruits consumable = iota + vegetables + drinks +) + +func (c consumable) String() string { + return [...]string{"fruit", "vegetable", "drink"}[c] +} + +func main() { + + var category consumable + + // First, ask for a broad food category. + err := huh.NewSelect[consumable](). + Title("What are you in the mood for?"). + Value(&category). + Options( + huh.NewOption("Some fruit", fruits), + huh.NewOption("A vegetable", vegetables), + huh.NewOption("A drink", drinks), + ). + Run() + + if err != nil { + fmt.Println("Decision trouble:", err) + os.Exit(1) + } + + type opts []huh.Option[string] + + var choice string + + // Then ask for a specific food item based on the previous answer. + err = huh.NewSelect[string](). + Title(fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)). + Value(&choice). + Options( + func(c consumable) opts { + switch c { + case fruits: + return opts{ + huh.NewOption("Tangerine", "tangerine"), + huh.NewOption("Canteloupe", "canteloupe"), + huh.NewOption("Pomelo", "pomelo"), + huh.NewOption("Grapefruit", "grapefruit"), + } + case vegetables: + return opts{ + huh.NewOption("Tangerine", "carrot"), + huh.NewOption("Jicama", "jicama"), + huh.NewOption("Kohlrabi", "kohlrabi"), + huh.NewOption("Fennel", "fennel"), + huh.NewOption("Ginger", "ginger"), + } + case drinks: + return opts{ + huh.NewOption("Coffee", "coffee"), + huh.NewOption("Tea", "tea"), + huh.NewOption("Bubble Tea", "bubble tea"), + huh.NewOption("Agua Fresca", "agua-fresca"), + } + default: + return nil + } + }(category)..., + ). + Run() + + if err != nil { + fmt.Println("Trouble in food paradise:", err) + os.Exit(1) + } + + fmt.Printf("One %s coming right up!\n", choice) +} From 7e9cbd3dd6bbb7cb31cedeb767f6c743556b8e53 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Wed, 24 Jan 2024 17:51:20 -0500 Subject: [PATCH 2/2] refactor: conditional to use WithHideFunc groups this allows the form to be navigated and the user to choose another option by going back. --- examples/conditional/main.go | 77 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/examples/conditional/main.go b/examples/conditional/main.go index b55c2739..36db9176 100644 --- a/examples/conditional/main.go +++ b/examples/conditional/main.go @@ -22,62 +22,61 @@ func (c consumable) String() string { func main() { var category consumable - - // First, ask for a broad food category. - err := huh.NewSelect[consumable](). - Title("What are you in the mood for?"). - Value(&category). - Options( - huh.NewOption("Some fruit", fruits), - huh.NewOption("A vegetable", vegetables), - huh.NewOption("A drink", drinks), - ). - Run() - - if err != nil { - fmt.Println("Decision trouble:", err) - os.Exit(1) - } - type opts []huh.Option[string] var choice string // Then ask for a specific food item based on the previous answer. - err = huh.NewSelect[string](). - Title(fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)). - Value(&choice). - Options( - func(c consumable) opts { - switch c { - case fruits: - return opts{ + err := + huh.NewForm( + huh.NewGroup( + huh.NewSelect[consumable](). + Title("What are you in the mood for?"). + Value(&category). + Options( + huh.NewOption("Some fruit", fruits), + huh.NewOption("A vegetable", vegetables), + huh.NewOption("A drink", drinks), + ), + ), + + huh.NewGroup( + huh.NewSelect[string](). + Title("Okay, what kind of fruit are you in the mood for?"). + Options( huh.NewOption("Tangerine", "tangerine"), huh.NewOption("Canteloupe", "canteloupe"), huh.NewOption("Pomelo", "pomelo"), huh.NewOption("Grapefruit", "grapefruit"), - } - case vegetables: - return opts{ - huh.NewOption("Tangerine", "carrot"), + ). + Value(&choice), + ).WithHideFunc(func() bool { return category != fruits }), + + huh.NewGroup( + huh.NewSelect[string](). + Title("Okay, what kind of vegetable are you in the mood for?"). + Options( + huh.NewOption("Carrot", "carrot"), huh.NewOption("Jicama", "jicama"), huh.NewOption("Kohlrabi", "kohlrabi"), huh.NewOption("Fennel", "fennel"), huh.NewOption("Ginger", "ginger"), - } - case drinks: - return opts{ + ). + Value(&choice), + ).WithHideFunc(func() bool { return category != vegetables }), + + huh.NewGroup( + huh.NewSelect[string](). + Title(fmt.Sprintf("Okay, what kind of %s are you in the mood for?", category)). + Options( huh.NewOption("Coffee", "coffee"), huh.NewOption("Tea", "tea"), huh.NewOption("Bubble Tea", "bubble tea"), huh.NewOption("Agua Fresca", "agua-fresca"), - } - default: - return nil - } - }(category)..., - ). - Run() + ). + Value(&choice), + ).WithHideFunc(func() bool { return category != drinks }), + ).Run() if err != nil { fmt.Println("Trouble in food paradise:", err)