Skip to content

Commit

Permalink
docs(example): simple example showing conditional options (#75)
Browse files Browse the repository at this point in the history
* docs(example): simple example showing conditional options

* refactor: conditional to use WithHideFunc groups

this allows the form to be navigated and the user to choose another
option by going back.

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
  • Loading branch information
meowgorithm and maaslalani authored Jan 24, 2024
1 parent c017ca2 commit 0350ef6
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions examples/conditional/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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
type opts []huh.Option[string]

var choice string

// Then ask for a specific food item based on the previous answer.
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"),
).
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"),
).
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"),
).
Value(&choice),
).WithHideFunc(func() bool { return category != drinks }),
).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 0350ef6

Please sign in to comment.