A simple and powerful library for building interactive forms in the terminal. Powered by Bubble Tea.
Huh?
provides a straightforward interface to prompt users for input.
There are several Question
types available to use:
package main
import (
"log"
"github.com/charmbracelet/huh"
)
func main() {
form := huh.NewForm(
// What's a taco without a shell?
// We'll need to know what filling to put inside too.
huh.NewGroup(
huh.NewSelect("Hard", "Soft").
Title("Shell?"),
huh.NewSelect("Chicken", "Beef", "Fish", "Beans").
Title("Base"),
),
// Prompt for toppings and special instructions.
// The customer can ask for up to 4 toppings.
huh.NewGroup(
huh.NewMultiSelect("Lettuce", "Tomatoes", "Corn", "Salsa", "Sour Cream", "Cheese").
Title("Toppings").
Limit(4),
huh.NewText().
Title("Special Instructions").
CharLimit(400),
),
// Gather final details for the order.
huh.NewGroup(
huh.NewInput().
Title("What's your name?").
Validate(validateName),
huh.NewConfirm().
Title("Would you like 15% off"),
),
)
err := form.Run()
if err != nil {
log.Fatal(err)
}
}
Input
s are single line text fields.
huh.NewInput().
Title("What's for lunch?").
Validate(validateLength).
Prompt("?")
Text
s are multi-line text fields.
huh.NewText().
Title("Tell me a story.").
Validate(validateLength).
Prompt(">").
Editor(true) // open in $EDITOR
Select
s are multiple choice questions.
huh.NewSelect[Country]().
Title("Pick a country.").
Options(
huh.NewOption("United States", "US"),
huh.NewOption("Germany", "DE"),
huh.NewOption("Brazil", "BR"),
huh.NewOption("Canada", "CA"),
).
Cursor("→")
Alternatively,
huh.NewSelect("United States", "Germany", "Brazil", "Canada").
Title("Pick a country.").
Cursor("→")
MultiSelect
s are multiple choice questions but allow multiple selections.
huh.NewMultiSelect().
Title("Toppings.").
Limit(4)
We'd love to hear your thoughts on this project. Feel free to drop us a note!
huh
is inspired by the wonderful Survey library by Alec Aivazis.
Part of Charm.
Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة