2020-05-25 19:26:40 -04:00
|
|
|
# Bubble Tea
|
2020-01-15 16:58:41 -05:00
|
|
|
|
2020-01-27 21:27:20 -05:00
|
|
|
The fun, functional way to build terminal apps. A Go framework based on
|
2020-05-12 16:39:08 -04:00
|
|
|
[The Elm Architecture][elm].
|
|
|
|
|
|
|
|
⚠️ This project is a pre-release so the API is subject to change
|
|
|
|
a little. That said, we're using it in production.
|
2020-01-27 21:27:20 -05:00
|
|
|
|
2020-01-15 16:58:41 -05:00
|
|
|
|
|
|
|
## Simple example
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
// A simple program that counts down from 5 and then exits.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2020-05-27 14:21:54 -04:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-01-15 16:58:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type model int
|
|
|
|
|
2020-05-27 14:21:54 -04:00
|
|
|
type tickMsg struct{}
|
2020-01-15 16:58:41 -05:00
|
|
|
|
|
|
|
func main() {
|
2020-05-27 14:21:54 -04:00
|
|
|
p := tea.NewProgram(initialize, update, view, subscriptions)
|
2020-01-25 21:40:14 -05:00
|
|
|
if err := p.Start(); err != nil {
|
2020-01-15 16:58:41 -05:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:21:54 -04:00
|
|
|
// Return the initial model and initial command
|
|
|
|
func initialize() (tea.Model, tea.Cmd) {
|
|
|
|
return 5, tick
|
|
|
|
}
|
|
|
|
|
2020-01-25 21:40:14 -05:00
|
|
|
// Listen for messages and update the model accordingly
|
2020-05-25 19:26:40 -04:00
|
|
|
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
2020-01-15 16:58:41 -05:00
|
|
|
m, _ := mdl.(model)
|
|
|
|
|
|
|
|
switch msg.(type) {
|
|
|
|
case tickMsg:
|
2020-05-27 14:21:54 -04:00
|
|
|
m--
|
2020-05-11 12:54:44 -04:00
|
|
|
if m == 0 {
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Quit
|
2020-01-15 16:58:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-01-25 21:40:14 -05:00
|
|
|
// Render to the terminal
|
2020-05-25 19:26:40 -04:00
|
|
|
func view(mdl tea.Model) string {
|
2020-01-15 16:58:41 -05:00
|
|
|
m, _ := mdl.(model)
|
|
|
|
return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:21:54 -04:00
|
|
|
// A simple command which Bubble Tea runs asynchronously.
|
|
|
|
func tick() tea.Msg {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
return tickMsg{}
|
2020-01-25 21:40:14 -05:00
|
|
|
}
|
2020-01-15 16:58:41 -05:00
|
|
|
```
|
|
|
|
|
2020-06-11 21:51:52 -04:00
|
|
|
Hungry for more? Totally confused? See the [other examples][examples].
|
2020-01-15 16:58:41 -05:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
[examples]: https://github.com/charmbracelet/tea/tree/master/examples
|
2020-05-12 16:39:08 -04:00
|
|
|
|
2020-01-25 21:40:14 -05:00
|
|
|
|
2020-04-27 11:43:11 -04:00
|
|
|
## Other Resources
|
2020-01-15 16:58:41 -05:00
|
|
|
|
2020-06-11 21:51:52 -04:00
|
|
|
* [termenv](https://github.com/muesli/termenv): advanced ANSI style and color
|
2020-04-27 11:43:11 -04:00
|
|
|
support for your terminal applications. Very useful when rendering your
|
|
|
|
views.
|
2020-06-11 21:51:52 -04:00
|
|
|
* [reflow](https://github.com/muesli/reflow): a collection of ANSI-aware text
|
2020-04-27 11:46:44 -04:00
|
|
|
formatting tools. Also useful for view rendering.
|
2020-06-11 21:51:52 -04:00
|
|
|
* [go-runewidth](https://github.com/mattn/go-runewidth): functions to get the
|
|
|
|
physical width of runes in terms of cells. Indispensable when working with
|
|
|
|
fullwidth and zero-width characters.
|
2020-04-27 11:46:44 -04:00
|
|
|
|
2020-01-25 21:40:14 -05:00
|
|
|
|
2020-04-27 11:43:11 -04:00
|
|
|
## Acknowledgments
|
2020-01-15 16:58:41 -05:00
|
|
|
|
2020-01-15 23:31:01 -05:00
|
|
|
Heavily inspired by both [The Elm Architecture][elm] by Evan Czaplicki et al.
|
2020-01-15 16:58:41 -05:00
|
|
|
and [go-tea][gotea] by TJ Holowaychuk.
|
|
|
|
|
2020-01-15 23:40:50 -05:00
|
|
|
[elm]: https://guide.elm-lang.org/architecture/
|
2020-01-15 16:58:41 -05:00
|
|
|
[gotea]: https://github.com/tj/go-tea
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
|
2020-01-24 15:05:25 -05:00
|
|
|
## License
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
[MIT](https://github.com/charmbracelet/tea/raw/master/LICENSE)
|
2020-01-25 21:40:14 -05:00
|
|
|
|
|
|
|
***
|
|
|
|
|
2020-04-27 11:51:02 -04:00
|
|
|
Part of [Charm](https://charm.sh).
|
2020-01-25 21:40:14 -05:00
|
|
|
|
|
|
|
<img alt="the Charm logo" src="https://stuff.charm.sh/charm-logotype.png" width="400px">
|
|
|
|
|
|
|
|
Charm热爱开源!
|