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 (
"fmt"
"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.Group(
huh.Select().
Title("Shell?").
Options("Hard", "Soft"),
huh.Select().
Title("Base").
Options("Chicken", "Beef", "Fish", "Beans"),
),
// Prompt for toppings and special instructions.
// The customer can ask for up to 4 toppings.
huh.Group(
huh.MultiSelect().
Title("Toppings").
Options("Lettuce", "Tomatoes", "Corn", "Salsa", "Sour Cream", "Cheese").
Filterable(true).
Limit(4),
huh.Text().
Title("Special Instructions").
CharLimit(400),
),
// Gather final details for the order.
huh.Group(
huh.Input().
Key("name").
Title("What's your name?").
Validate(huh.ValidateLength(0, 20)),
huh.Confirm().
Key("discount").
Title("Would you like 15% off"),
),
)
r, err := form.Run()
if err != nil {
log.Fatal(err)
}
fmt.Println("A %s shell filled with %s and %s, topped with %s.",
r["Shell?"], r["Base"], r["Toppings"])
fmt.Println("That will be $%.2f. Thanks for your order, %s!",
calculatePrice(r), r["name"])
}
Input
s are single line text fields.
huh.Input().
Title("What's for lunch?").
Validate(huh.ValidateLength(0, 20)).
Prompt("?")
Text
s are multi-line text fields.
huh.Text().
Title("Tell me a story.").
Validate(huh.ValidateLength(100, 400)).
Prompt(">").
Editor(true) // open in $EDITOR
Select
s are multiple choice questions.
huh.Select().
Title("Pick a country.").
Option("United States").
Option("Germany").
Option("Brazil").
Option("Canada").
Cursor("→")
Alternatively,
huh.Select().
Title("Pick a country.").
Options("United States", "Germany", "Brazil", "Canada").
Cursor("→")
MultiSelect
s are multiple choice questions but allow multiple selections.
huh.MultiSelect().
Title("Toppings.").
Option("Lettuce").
Option("Tomatoes").
Option("Cheese").
Option("Corn").
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 • نحنُ نحب المصادر المفتوحة