mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
db4600aeb2a41f3cffcfecf1f6b41035df1c72d8
There may be too much rigamarole around this (it's simpler to get the size imperatively and synchronously) but adding it for now.
Boba
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"
"github.com/charmbracelet/boba"
)
type model int
type tickMsg time.Time
func main() {
p := boba.NewProgram(init, update, view, subscriptions)
if err := p.Start(); err != nil {
log.Fatal(err)
}
}
// Listen for messages and update the model accordingly
func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
m, _ := mdl.(model)
switch msg.(type) {
case tickMsg:
m--
if m == 0 {
return m, boba.Quit
}
}
return m, nil
}
// Render to the terminal
func view(mdl boba.Model) string {
m, _ := mdl.(model)
return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
}
// Subscribe to events
func subscriptions(_ boba.Model) boba.Subs {
return boba.Subs{
"tick": time.Every(time.Second, func(t time.Time) boba.Msg {
return tickMsg(t)
},
}
}
Hungry for more? 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.
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%