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
Provenance
- Commit
d1e9996- Tree
05f0ac6- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
6c607aa
1 file changed+31 added−25 removed
modified:tutorials/basics/README.md+31 added-25 removed
28 unmodified lines29 tea "github.com/charmbracelet/bubbletea"30 )31 ```32 33-Bubble Tea programs are comprised of a model that describes the application34-state and three simple functions that are centered around the model:33+Bubble Tea programs are comprised of a **model** that describes the application34+state and three simple functions that are centered around that model:35 36 * **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.39 40 ## The Model41 42-So let's start by defining our application's model. The model is simply the43-application's state. It can be any type, but a `struct` usually makes the most44-sense.42+So let's start by defining our model which will store our application's state.43+It can be any type, but a `struct` usually makes the most sense.44 45 ```go46 type model struct {47 choices []string // items on the to-do list7 unmodified lines55 Next we'll define a function that will initialize our application. An56 initialize function returns a model representing our application's initial57 state, as well as a `Cmd` that could perform some initial I/O. For now, we58 don't need to do any I/O, so for the command we'll just return nil, which60-translate to "no command."59+translates to "no command."60 61 ```go62 func initialize() (tea.Model, tea.Cmd) {63 m := model{14 unmodified lines78 79 ## Update80 81 Next 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 an84-updated model based on whatever happened. It can also return a `Cmd` and make85-more 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 updated83+model in response to whatever happened. It can also return a `Cmd` and make84+more things happen, but for not don't worry about that part.85 86 In our case, when a user presses the down arrow `update`'s job is to notice87 that the down arrow was pressed and move the cursor accordingly (or not).88 90-The "something happened" comes in as a `Msg`, which can be any type. Messages91-indicate some I/O happened, such as a keypress, timer, or a response from92-a server.89+The "something happened" comes in the form of a `Msg`, which can be any type.90+Messages indicate some I/O happened, such as a keypress, timer tick, or91+a response from a server.92 93 We usually figure out which type of `Msg` we received with a type switch, but94 you could also use a type assertion.95 97-For now, we'll just deal with `tea.KeyMsg`, which are automatically sent to98-the update function when keys are pressed.96+For now, we'll just deal with `tea.KeyMsg` messages, which are automatically97+sent to the update function when keys are pressed.98 99 ```go100 func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {101 m, _ := mdl.(model)39 unmodified lines141 return m, nil142 }143 ```144 146-You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` with the147-model. That's a special command which instructs the Bubble Tea runtime to exit,148-effectively quitting the program.145+You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command146+with the model. That's a special command which instructs the Bubble Tea runtime147+to quit, effectively exiting the program.148 149 ## The View150 151 At last, it's time to render our UI. Of all the functions, the view is the58 unmodified lines210 This tutorial covers the basics of building an interactive terminal UI, but211 in the real world you'll also need to perform I/O. To learn about that have a212 look at the [Cmd Tutorial][cmd]. It's pretty simple.213 215-There are also several [examples][examples] available. Many of the examples216-make use of [Bubbles][bubbles], the little Bubble Tea component library which217-includes handy things like a text input component, spinners and a viewport.214+There are also several [examples][examples] available and, of course,215+there are also [Go Docs][docs] for Bubble Tea.216 219-Of course, there are also [Go Docs][docs] for Bubble Tea.220-217 [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=doc220 221+## Libraries we use with Bubble Tea222+223+* [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!227+228+[termenv]: https://github.com/muesli/termenv229+[reflow]: https://github.com/muesli/reflow230+[bubbles]: https://github.com/charmbracelet/bubbles231+[runewidth]: https://github.com/mattn/go-runewidth232+233 ## Feedback234 228-We'd love to hear your thoughts on this tutorial. Please feel free to reach out229-anytime.235+We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!236 237 * [Twitter](https://twitter.com/charmcli)238 * [The Fediverse](https://mastodon.technology/@charm)