Skip to content
Myr

Tutorial edits and improvements

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 6c607aa · Browse files · 1 file+31 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−25 removed

  • other:+31 added−25 removed

1 file+31 added−25 removed

modified:tutorials/basics/README.md+31 added-25 removed
28 unmodified lines
29 tea "github.com/charmbracelet/bubbletea"30 )31```3233Bubble Tea programs are comprised of a model that describes the application34state and three simple functions that are centered around the model:33Bubble Tea programs are comprised of a **model** that describes the application34state and three simple functions that are centered around that model:3536* **Initialize**, a function that returns the model's initial state.37* **Update**, a function that handles incoming events and updates the model accordingly.38* **View**, a function that renders the UI based on the data in the model.3940## The Model4142So let's start by defining our application's model. The model is simply the43application's state. It can be any type, but a `struct` usually makes the most44sense.42So let's start by defining our model which will store our application's state.43It can be any type, but a `struct` usually makes the most sense.4445```go46 type model struct {47 choices []string // items on the to-do list
7 unmodified lines
55Next we'll define a function that will initialize our application. An56initialize function returns a model representing our application's initial57state, as well as a `Cmd` that could perform some initial I/O. For now, we58don't need to do any I/O, so for the command we'll just return nil, which60translate to "no command."59translates to "no command."6061```go62 func initialize() (tea.Model, tea.Cmd) {63 m := model{
14 unmodified lines
7879## Update8081Next we'll define the update function. The update function is called when83"things happen." It's job is to look at what has happened and return an84updated model based on whatever happened. It can also return a `Cmd` and make85more things happen, but we'll get into that later.82"things happen." It's job is to look at what has happened and return an updated83model in response to whatever happened. It can also return a `Cmd` and make84more things happen, but for not don't worry about that part.8586In our case, when a user presses the down arrow `update`'s job is to notice87that the down arrow was pressed and move the cursor accordingly (or not).8890The "something happened" comes in as a `Msg`, which can be any type. Messages91indicate some I/O happened, such as a keypress, timer, or a response from92a server.89The "something happened" comes in the form of a `Msg`, which can be any type.90Messages indicate some I/O happened, such as a keypress, timer tick, or91a response from a server.9293We usually figure out which type of `Msg` we received with a type switch, but94you could also use a type assertion.9597For now, we'll just deal with `tea.KeyMsg`, which are automatically sent to98the update function when keys are pressed.96For now, we'll just deal with `tea.KeyMsg` messages, which are automatically97sent to the update function when keys are pressed.9899```go100 func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {101 m, _ := mdl.(model)
39 unmodified lines
141 return m, nil142 }143```144146You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` with the147model. That's a special command which instructs the Bubble Tea runtime to exit,148effectively quitting the program.145You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command146with the model. That's a special command which instructs the Bubble Tea runtime147to quit, effectively exiting the program.148149## The View150151At last, it's time to render our UI. Of all the functions, the view is the
58 unmodified lines
210This tutorial covers the basics of building an interactive terminal UI, but211in the real world you'll also need to perform I/O. To learn about that have a212look at the [Cmd Tutorial][cmd]. It's pretty simple.213215There are also several [examples][examples] available. Many of the examples216make use of [Bubbles][bubbles], the little Bubble Tea component library which217includes handy things like a text input component, spinners and a viewport.214There are also several [examples][examples] available and, of course,215there are also [Go Docs][docs] for Bubble Tea.216219Of course, there are also [Go Docs][docs] for Bubble Tea.220217[cmd]: http://github.com/charmbracelet/bubbletea/tree/master/tutorials/cmds/218[examples]: http://github.com/charmbracelet/bubbletea/tree/master/examples223[bubbles]: https://github.com/charmbracelet/bubbles219[docs]: https://pkg.go.dev/github.com/charmbracelet/glow?tab=doc220221## Libraries we use with Bubble Tea222223* [Bubbles][bubbles] various Bubble Tea components we've built224* [Termenv][termenv]: Advanced ANSI coloring for terminal applications225* [Reflow][reflow]: ANSI-aware methods for reflowing blocks of text226* [go-runewidth][runewidth]: Get the physical width of strings in terms of terminal cells. Many runes, such as East Asian charcters and emojis, are two cells wide, so measuring a layout with `len()` often won't cut it!227228[termenv]: https://github.com/muesli/termenv229[reflow]: https://github.com/muesli/reflow230[bubbles]: https://github.com/charmbracelet/bubbles231[runewidth]: https://github.com/mattn/go-runewidth232233## Feedback234228We'd love to hear your thoughts on this tutorial. Please feel free to reach out229anytime.235We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!236237* [Twitter](https://twitter.com/charmcli)238* [The Fediverse](https://mastodon.technology/@charm)