bubbletea/README.md

97 lines
2.1 KiB
Markdown
Raw Normal View History

2020-01-18 10:41:01 -05:00
# 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
[The Elm Architecture][elm]. 茶!
⚠️ This project is a pre-release! The API is subject to change a little.
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"
"github.com/charmbracelet/tea"
)
type model int
type tickMsg time.Time
2020-01-15 16:58:41 -05:00
func main() {
p := tea.NewProgram(init, update, view, subscriptions)
if err := p.Start(); err != nil {
2020-01-15 16:58:41 -05:00
log.Fatal(err)
}
}
// Listen for messages and update the model accordingly
2020-01-15 16:58:41 -05:00
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m, _ := mdl.(model)
switch msg.(type) {
case tickMsg:
m--
if m == 0 {
2020-01-15 16:58:41 -05:00
return m, tea.Quit
}
}
return m, nil
}
// Render to the terminal
2020-01-15 16:58:41 -05:00
func view(mdl tea.Model) string {
m, _ := mdl.(model)
return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
}
// Subscribe to events
func subscriptions(_ tea.Model) tea.Subs {
return tea.Subs{
"tick": time.Every(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
},
}
}
2020-01-15 16:58:41 -05:00
```
Hungry for more? See the [other examples][examples].
[examples]: https://github.com/charmbracelet/tea/tree/master/examples
2020-04-27 11:43:11 -04:00
## Other Resources
2020-01-15 16:58:41 -05:00
* [Tea Party](https://github.com/charmbracelet/teaparty): a collection of Tea
components.
2020-04-27 11:43:11 -04:00
* [Termenv](https://github.com/muesli/termenv): advanced ANSI style and color
support for your terminal applications. Very useful when rendering your
views.
* [Reflow](https://github.com/muesli/reflow): a collection of ANSI-aware text
formatting tools. Also useful for view rendering.
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-01-24 15:05:25 -05:00
## License
[MIT](https://github.com/charmbracelet/tea/raw/master/LICENSE)
***
2020-04-27 11:51:02 -04:00
Part of [Charm](https://charm.sh).
<img alt="the Charm logo" src="https://stuff.charm.sh/charm-logotype.png" width="400px">
Charm热爱开源!