Skip to content

Commit

Permalink
docs(example): simple example showing conditional options
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Dec 19, 2023
1 parent 47340b5 commit 0f20f61
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions examples/conditional/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0f20f61

Please sign in to comment.