Skip to content
/ huh Public
forked from charmbracelet/huh

Build terminal forms and prompts 🤷🏻‍♀️

License

Notifications You must be signed in to change notification settings

kralicky/huh

Repository files navigation

Huh?

A simple and powerful library for building interactive forms in the terminal. Powered by Bubble Tea.

Running a taco form

The above example is running from a single Go program (source).

Tutorial

huh? provides a straightforward API to build forms and prompt users for input.

For this tutorial, we're building a taco order form.

We start by defining our package importing huh.

package main

import (
  "log"

  "github.com/charmbracelet/huh"
)

Let's start defining our form, huh forms contain groups which group different fields together such that they are displayed on the same page.

We'll build three groups to get all our information for the taco order.

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[string]().
      Options(huh.NewOptions("Hard", "Soft")...).
      Title("Shell?").
      Value(&shell),

    huh.NewSelect[string]().
      Options(huh.NewOptions("Chicken", "Beef", "Fish", "Beans")...).
      Title("Base").
      Value(&base),
  ),

  // Prompt for toppings and special instructions.
  // The customer can ask for up to 4 toppings.
  huh.NewGroup(
    huh.NewMultiSelect[string]().
      Options(
        huh.NewOption("Tomatoes", "tomatoes").Selected(true),
        huh.NewOption("Lettuce", "lettuce").Selected(true),
        huh.NewOption("Salsa", "salsa"),
        huh.NewOption("Cheese", "cheese"),
        huh.NewOption("Sour Cream", "sour cream"),
        huh.NewOption("Corn", "corn"),
      ).
      Title("Toppings").
      Limit(4).
      Value(&toppings),
  ),

  // Gather final details for the order.
  huh.NewGroup(
    huh.NewInput().
      Title("What's your name?").
      Value(&name).
      Validate(validateName),

    huh.NewText().
      Title("Special Instructions").
      Value(&instructions).
      CharLimit(400),

    huh.NewConfirm().
      Title("Would you like 15% off").
      Value(&discount),
  ),
)

Finally, we can run the form:

err := form.Run()
if err != nil {
  log.Fatal(err)
}

Field Reference

  • Input: gather text input from the user
  • Text: gather multiline text input from the user
  • Select: prompt user to select an option from a list.
  • MultiSelect: prompt user to select multiple options from a list.
  • Confirm: ask the user a yes or no question.

Input

Inputs are single line text fields.

huh.NewInput().
  Title("What's for lunch?").
  Validate(validateLength).
  Prompt("?").
  Value(&lunch)

Text

Texts are multi-line text fields.

huh.NewText().
  Title("Tell me a story.").
  Validate(checkForPlagiarism).
  Prompt(">").
  Editor(true). // open in $EDITOR
  Value(&text)

Select

Selects are multiple choice questions.

huh.NewSelect[string]().
  Title("Pick a country.").
  Options(
    huh.NewOption("United States", "US"),
    huh.NewOption("Germany", "DE"),
    huh.NewOption("Brazil", "BR"),
    huh.NewOption("Canada", "CA"),
  ).
  Cursor("→").
  Value(&country)

Alternatively, use the huh.NewOptions shorthand when keys and values are the same:

huh.NewSelect[string]().
  Title("Pick a country.").
  Options(huh.NewOptions("United States", "Germany", "Brazil", "Canada")...).
  Cursor("→").
  Value(&country)

Multiple Select

MultiSelects are multiple choice questions but allow multiple selections.

huh.NewMultiSelect[string]().
  Options(
    huh.NewOption("Cheese", "cheese").Selected(true),
    huh.NewOption("Lettuce", "lettuce").Selected(true),
    huh.NewOption("Tomatoes", "tomatoes"),
    huh.NewOption("Corn", "corn"),
    huh.NewOption("Salsa", "salsa"),
    huh.NewOption("Sour Cream", "sour cream"),
  ).
  Title("Toppings").
  Limit(4).
  Value(&toppings),

Confirm

Confirm is a yes or no confirmation.

huh.NewConfirm().
  Title("Toppings").
  Affirmative("Yes!").
  Negative("No.").
  Value(&confirm)

Themes

Forms can

Feedback

We'd love to hear your thoughts on this project. Feel free to drop us a note!

Acknowledgments

huh is inspired by the wonderful Survey library by Alec Aivazis.

License

MIT


Part of Charm.

The Charm logo

Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة

About

Build terminal forms and prompts 🤷🏻‍♀️

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.9%
  • Makefile 0.1%