charmbracelet/huh - Library for building forms and prompts in the terminal #955
Labels
CLI-UX
Command Line Interface user experience and best practices
Git-Repo
Source code repository like gitlab or gh
github
gh tools like cli, Actions, Issues, Pages
source-code
Code snippets
Huh?
A simple, powerful library for building interactive forms and prompts in the terminal.
huh?
is easy to use in a standalone fashion, can beintegrated into a Bubble Tea application, and contains
a first-class accessible mode for screen readers.
The above example is running from a single Go program (source).
Tutorial
Let's build a form for ordering burgers. To start, we'll import the library and
define a few variables where'll we store answers.
huh?
separates forms into groups (you can think of groups as pages). Groupsare made of fields (e.g.
Select
,Input
,Text
). We will set up threegroups for the customer to fill out.
Finally, run the form:
And that's it! For more info see the full source for this
example as well as the docs.
If you need more dynamic forms that change based on input from previous fields,
check out the dynamic forms example.
Field Reference
Input
: single line text inputText
: multi-line text inputSelect
: select an option from a listMultiSelect
: select multiple options from a listConfirm
: confirm an action (yes or no)Tip
Just want to prompt the user with a single field? Each field has a
Run
method that can be used as a shorthand for gathering quick and easy input.
Input
Prompt the user for a single line of text.
Text
Prompt the user for multiple lines of text.
Select
Prompt the user to select a single option from a list.
Multiple Select
Prompt the user to select multiple (zero or more) options from a list.
Confirm
Prompt the user to confirm (Yes or No).
Accessibility
huh?
has a special rendering option designed specifically for screen readers.You can enable it with
form.WithAccessible(true)
.Tip
We recommend setting this through an environment variable or configuration
option to allow the user to control accessibility.
Accessible forms will drop TUIs in favor of standard prompts, providing better
dictation and feedback of the information on screen for the visually impaired.
Themes
huh?
contains a powerful theme abstraction. Supply your own custom theme orchoose from one of the five predefined themes:
Charm
Dracula
Catppuccin
Base 16
Default
Themes can take advantage of the full range of
Lip Gloss style options. For a high level theme reference see
the docs.
Dynamic Forms
huh?
forms can be as dynamic as your heart desires. Simply replace propertieswith their equivalent
Func
to recompute the properties value every time adifferent part of your form changes.
Here's how you would build a simple country + state / province picker.
First, define some variables that we'll use to store the user selection.
Define your country select as you normally would:
Define your state select with
TitleFunc
andOptionsFunc
instead ofTitle
and
Options
. This will allow you to change the title and options based on theselection of the previous field, i.e.
country
.To do this, we provide a
func() string
and abinding any
toTitleFunc
. Thefunction defines what to show for the title and the binding specifies what value
needs to change for the function to recompute. So if
country
changes (e.g. theuser changes the selection) we will recompute the function.
For
OptionsFunc
, we provide afunc() []Option[string]
and abinding any
.We'll fetch the country's states, provinces, or territories from an API.
huh
will automatically handle caching for you.
Important
We have to pass
&country
as the binding to recompute the function only whencountry
changes, otherwise we will hit the API too often.Lastly, run the
form
with these inputs.Bonus: Spinner
huh?
ships with a standalone spinner package. It's useful for indicatingbackground activity after a form is submitted.
Create a new spinner, set a title, set the action (or provide a
Context
), and run the spinner:For more on Spinners see the spinner examples and
the spinner docs.
What about Bubble Tea?
In addition to its standalone mode,
huh?
has first-class support for[Bubble Tea][tea] and can be easily integrated into Bubble Tea applications.
It's incredibly useful in portions of your Bubble Tea application that need
form-like input.
A
huh.Form
is merely atea.Model
, so you can use it just asyou would any other Bubble.
The text was updated successfully, but these errors were encountered: