11# Bubble Tea Basics
22
33Bubble Tea is based on the functional design paradigms of [ The Elm
4- Architecture] [ elm ] , which happens to work nicely with Go. It' s a delightful way
4+ Architecture] [ elm ] , which happens to work nicely with Go. It’ s a delightful way
55to build applications.
66
77This tutorial assumes you have a working knowledge of Go.
@@ -12,12 +12,12 @@ By the way, the non-annotated source code for this program is available
1212[ elm ] : https://guide.elm-lang.org/architecture/
1313[ tut-source ] : https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics
1414
15- ## Enough! Let' s get to it.
15+ ## Enough! Let’ s get to it.
1616
17- For this tutorial, we' re making a shopping list.
17+ For this tutorial, we’ re making a shopping list.
1818
19- To start we' ll define our package and import some libraries. Our only external
20- import will be the Bubble Tea library, which we' ll call ` tea ` for short.
19+ To start we’ ll define our package and import some libraries. Our only external
20+ import will be the Bubble Tea library, which we’ ll call ` tea ` for short.
2121
2222``` go
2323package main
@@ -26,7 +26,7 @@ import (
2626 " fmt"
2727 " os"
2828
29- tea " github.com/charmbracelet /bubbletea/v2"
29+ tea " charm.land /bubbletea/v2"
3030)
3131```
3232
@@ -39,7 +39,7 @@ state and three simple methods on that model:
3939
4040## The Model
4141
42- So let' s start by defining our model which will store our application' s state.
42+ So let’ s start by defining our model which will store our application’ s state.
4343It can be any type, but a ` struct ` usually makes the most sense.
4444
4545``` go
@@ -70,34 +70,22 @@ func initialModel() model {
7070}
7171```
7272
73- After that, we’ll define our application’s initial state in the ` Init ` method. ` Init `
74- can return a ` Cmd ` that could perform some initial I/O. For now, we don't need
75- to do any I/O, so for the command, we'll just return ` nil ` , which translates to
76- "no command."
73+ After that, we’ll define the ` Init ` method. ` Init ` can return a ` Cmd ` that
74+ could perform some initial I/O. For now, we don’t need to do any I/O, so for
75+ the command, we’ll just return ` nil ` , which translates to "no command."
7776
7877``` go
79- func (m model ) Init () (tea .Model , tea .Cmd ) {
80- m = {
81- // Our to-do list is a grocery list
82- choices: []string {" Buy carrots" , " Buy celery" , " Buy kohlrabi" },
83-
84- // A map which indicates which choices are selected. We're using
85- // the map like a mathematical set. The keys refer to the indexes
86- // of the `choices` slice, above.
87- selected: make (map [int ]struct {}),
88- }
89-
90- // Just return `nil`, which means "no I/O right now, please."
91- return m, nil
78+ func (m model ) Init () tea .Cmd {
79+ return nil
9280}
9381```
9482
9583## The Update Method
9684
97- Next up is the update method. The update function is called when ” things
85+ Next up is the update method. The update function is called when “ things
9886happen.” Its job is to look at what has happened and return an updated model in
9987response. It can also return a ` Cmd ` to make more things happen, but for now
100- don' t worry about that part.
88+ don’ t worry about that part.
10189
10290In our case, when a user presses the down arrow, ` Update ` ’s job is to notice
10391that the down arrow was pressed and move the cursor accordingly (or not).
@@ -109,7 +97,7 @@ tick, or a response from a server.
10997We usually figure out which type of ` Msg ` we received with a type switch, but
11098you could also use a type assertion.
11199
112- For now, we' ll just deal with ` tea.KeyPressMsg ` messages, which are
100+ For now, we’ ll just deal with ` tea.KeyPressMsg ` messages, which are
113101automatically sent to the update function when keys are pressed.
114102
115103``` go
@@ -164,14 +152,15 @@ the Bubble Tea runtime to quit, exiting the program.
164152
165153At last, it’s time to render our UI. Of all the methods, the view is the
166154simplest. We look at the model in its current state and use it to return
167- a ` string ` . That string is our UI!
155+ a ` tea.View ` . The view declares our UI content and, optionally, terminal
156+ features like alt screen mode, mouse tracking, cursor position, and more.
168157
169158Because the view describes the entire UI of your application, you don’t have to
170159worry about redrawing logic and stuff like that. Bubble Tea takes care of it
171160for you.
172161
173162``` go
174- func (m model ) View () string {
163+ func (m model ) View () tea . View {
175164 // The header
176165 s := " What should we buy at the market?\n\n "
177166
@@ -198,13 +187,13 @@ func (m model) View() string {
198187 s += " \n Press q to quit.\n "
199188
200189 // Send the UI for rendering
201- return s
190+ return tea. NewView (s)
202191}
203192```
204193
205194## All Together Now
206195
207- The last step is to simply run our program. We pass an empty model
196+ The last step is to simply run our program. We pass our initial model to
208197` tea.NewProgram ` and let it rip:
209198
210199``` go
@@ -220,15 +209,15 @@ func main() {
220209## What’s Next?
221210
222211This tutorial covers the basics of building an interactive terminal UI, but
223- in the real world you' ll also need to perform I/O. To learn about that have a
224- look at the [ Command Tutorial] [ cmd ] . It' s pretty simple.
212+ in the real world you’ ll also need to perform I/O. To learn about that have a
213+ look at the [ Command Tutorial] [ cmd ] . It’ s pretty simple.
225214
226215There are also several [ Bubble Tea examples] [ examples ] available and, of course,
227216there are [ Go Docs] [ docs ] .
228217
229218[ cmd ] : http://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands/
230219[ examples ] : http://github.com/charmbracelet/bubbletea/tree/master/examples
231- [ docs ] : https://pkg.go.dev/github.com/charmbracelet/ bubbletea?tab=doc
220+ [ docs ] : https://pkg.go.dev/charm.land/ bubbletea/v2 ?tab=doc
232221
233222## Additional Resources
234223
@@ -237,7 +226,7 @@ there are [Go Docs][docs].
237226
238227### Feedback
239228
240- We' d love to hear your thoughts on this tutorial. Feel free to drop us a note!
229+ We’ d love to hear your thoughts on this tutorial. Feel free to drop us a note!
241230
242231- [ Twitter] ( https://twitter.com/charmcli )
243232- [ The Fediverse] ( https://mastodon.social/@charmcli )
0 commit comments