mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
6bd34bdd14e24cb2cf87ff749afd273214a2f212
Tea core now sends the terminal dimensions on start and when the window is resized (except on Windows where SIGWINCH, the resize signal, is not supported).
Bubble Tea
The fun, functional way to build terminal apps. A Go framework based on The Elm Architecture.
⚠️ This project is a pre-release so the API is subject to change a little. That said, we're using it in production.
Simple example
package main
// A simple program that counts down from 5 and then exits.
import (
"fmt"
"log"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type model int
type tickMsg struct{}
func main() {
p := tea.NewProgram(initialize, update, view, subscriptions)
if err := p.Start(); err != nil {
log.Fatal(err)
}
}
// Return the initial model and initial command
func initialize() (tea.Model, tea.Cmd) {
return 5, tick
}
// Listen for messages and update the model accordingly
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m, _ := mdl.(model)
switch msg.(type) {
case tickMsg:
m--
if m == 0 {
return m, tea.Quit
}
}
return m, nil
}
// Render to the terminal
func view(mdl tea.Model) string {
m, _ := mdl.(model)
return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
}
// A simple command which Bubble Tea runs asynchronously.
func tick() tea.Msg {
time.Sleep(time.Second)
return tickMsg{}
}
Hungry for more? Totally confused? See the other examples.
Other Resources
- termenv: advanced ANSI style and color support for your terminal applications. Very useful when rendering your views.
- reflow: a collection of ANSI-aware text formatting tools. Also useful for view rendering.
- go-runewidth: functions to get the physical width of runes in terms of cells. Indispensable when working with fullwidth and zero-width characters.
Acknowledgments
Heavily inspired by both The Elm Architecture by Evan Czaplicki et al. and go-tea by TJ Holowaychuk.
License
Part of Charm.
Charm热爱开源!
Languages
Go
100%