What you’ll build
A terminal UI where users can:- Navigate through a list of items with arrow keys
- Select and deselect items with the spacebar or Enter
- Quit the program with
qorCtrl+C
Create your project
Build the application
Import dependencies
Start by importing the necessary packages:main.go
Define the model
The model represents your application’s state. For this shopping list, you need to track the available choices, cursor position, and which items are selected:main.go
Initialize the model
Create a function that returns the initial state of your application:main.go
Implement Init
TheInit method returns an initial command. For this simple app, we don’t need any initial I/O, so return nil:
main.go
Init can return commands for initial I/O operations like fetching data from an API or reading a file. You’ll learn more about commands in the concepts section.Implement Update
TheUpdate method handles all events. It receives a message, updates the model, and optionally returns a command:
main.go
- Uses a type switch to determine the message type
- Handles
KeyPressMsgfor keyboard input - Moves the cursor up/down with arrow keys or vim bindings
- Toggles selection with Enter or spacebar
- Returns
tea.Quitcommand to exit the program
Implement View
TheView method renders your UI based on the current model state:
main.go
- Builds a string representation of your UI
- Shows a cursor (
>) next to the current item - Shows a checkmark (
x) for selected items - Returns a
tea.Viewcontaining the UI content
You don’t need to worry about redrawing logic. Bubble Tea automatically handles rendering when the model changes.
Create the main function
Finally, create the main function to run your program:main.go
Run your program
Execute your application:- A list of grocery items
- A cursor (
>) pointing at the first item - The ability to navigate with arrow keys
- The ability to select items with spacebar or Enter
Understanding the flow
Initialization
main() calls tea.NewProgram() with your initial model, then Run() starts the program.Update loop
When you press a key:
- Bubble Tea sends a
KeyPressMsgtoUpdate() Update()modifies the model based on the key pressedView()is called again to re-render with the updated model
Next steps
Core concepts
Dive deeper into models, messages, and commands
Commands guide
Learn how to perform I/O operations
Examples
Explore more complex Bubble Tea applications
Bubbles components
Use pre-built UI components like text inputs and spinners