Bubble Tea App Template
repository·main·Indexed 42 days ago
https://github.com/charmbracelet/bubbletea-app-templateA boilerplate template for developing terminal applications using the Bubble Tea framework and Charm ecosystem. It includes pre-configured core libraries such as bubbletea, bubbles, and lipgloss, along with GitHub Actions workflows, GoReleaser, and golangci-lint for CI/CD.
What's inside Bubble Tea App Template
- This template repository provides a starting point for building terminal applications using the Charm ecosystem. It includes a sample application that integrates the core dependencies required for most Bubble Tea projects, allowing you to immediately begin building UI components and terminal logic.
Implement a Bubble Tea application structure
mainTo build a Bubble Tea application, you must implement the
tea.Modelinterface, which requires three core methods:Init,Update, andView.Init(): Returns an initialtea.Cmdto perform startup tasks (e.g., starting a spinner or fetching data).Update(tea.Msg): Handles incoming messages (key presses, timer ticks, custom errors). It returns the updated model and an optionaltea.Cmdto trigger further actions.View(): Returns a string representing the current state of the UI to be rendered in the terminal.
In the
Updatemethod, you typically use a type switch to handle different message types liketea.KeyMsgor custom error types.type model struct { spinner spinner.Model quitting bool err error } func (m model) Init() tea.Cmd { return m.spinner.Tick } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: // handle keys case errMsg: // handle errors default: // handle other messages } return m, nil } func (m model) View() string { return "rendered UI string" }Review included dependencies and tooling
mainThe template comes pre-configured with the following core libraries and DevOps tooling:
Core Libraries:
bubbletea: The framework for building terminal user interfaces.bubbles: A collection of common UI components (widgets).lipgloss: A library for styling terminal text and layouts.
DevOps & CI/CD:
- GitHub Actions workflows for
build,test,lint, andrelease. GoReleaserconfigurations for automated binary releases.golangci-lintconfigurations for code quality enforcement.
Define key bindings with bubbles/key
mainUse
key.NewBindingto define reusable key sequences and help text. This allows you to check for multiple keys (likeq,esc, orctrl+c) usingkey.Matcheswithin yourUpdateloop.var quitKeys = key.NewBinding( key.WithKeys("q", "esc", "ctrl+c"), key.WithHelp("", "press q to quit"), )Run a Bubble Tea program in main()
mainTo start the application loop, use
tea.NewProgramwith your initial model and callp.Run(). This starts the event loop and handles the terminal rendering.func main() { p := tea.NewProgram(initialModel()) if _, err := p.Run(); err != nil { fmt.Println(err) os.Exit(1) } }