Skip to content

Commit 7b250a3

Browse files
committed
docs(tutorials): update tutorials
1 parent 820a6fa commit 7b250a3

File tree

6 files changed

+82
-90
lines changed

6 files changed

+82
-90
lines changed

tutorials/basics/README.md

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Bubble Tea Basics
22

33
Bubble 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. Its a delightful way
55
to build applications.
66

77
This 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! Lets get to it.
1616

17-
For this tutorial, we're making a shopping list.
17+
For this tutorial, were 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 well define our package and import some libraries. Our only external
20+
import will be the Bubble Tea library, which well call `tea` for short.
2121

2222
```go
2323
package 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 lets start by defining our model which will store our applications state.
4343
It 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
9886
happen.” Its job is to look at what has happened and return an updated model in
9987
response. It can also return a `Cmd` to make more things happen, but for now
100-
don't worry about that part.
88+
dont worry about that part.
10189

10290
In our case, when a user presses the down arrow, `Update`’s job is to notice
10391
that the down arrow was pressed and move the cursor accordingly (or not).
@@ -109,7 +97,7 @@ tick, or a response from a server.
10997
We usually figure out which type of `Msg` we received with a type switch, but
11098
you could also use a type assertion.
11199

112-
For now, we'll just deal with `tea.KeyPressMsg` messages, which are
100+
For now, well just deal with `tea.KeyPressMsg` messages, which are
113101
automatically 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

165153
At last, it’s time to render our UI. Of all the methods, the view is the
166154
simplest. 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

169158
Because the view describes the entire UI of your application, you don’t have to
170159
worry about redrawing logic and stuff like that. Bubble Tea takes care of it
171160
for 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 += "\nPress 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

222211
This 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 youll also need to perform I/O. To learn about that have a
213+
look at the [Command Tutorial][cmd]. Its pretty simple.
225214

226215
There are also several [Bubble Tea examples][examples] available and, of course,
227216
there 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+
Wed 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)

tutorials/basics/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"os"
66

7-
tea "github.com/charmbracelet/bubbletea/v2"
7+
tea "charm.land/bubbletea/v2"
88
)
99

1010
type model struct {
@@ -26,7 +26,7 @@ func initialModel() model {
2626
}
2727

2828
func (m model) Init() tea.Cmd {
29-
return tea.SetWindowTitle("Grocery List")
29+
return nil
3030
}
3131

3232
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -56,7 +56,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5656
return m, nil
5757
}
5858

59-
func (m model) View() string {
59+
func (m model) View() tea.View {
6060
s := "What should we buy at the market?\n\n"
6161

6262
for i, choice := range m.choices {
@@ -75,7 +75,10 @@ func (m model) View() string {
7575

7676
s += "\nPress q to quit.\n"
7777

78-
return s
78+
v := tea.NewView(s)
79+
v.WindowTitle = "Grocery List"
80+
81+
return v
7982
}
8083

8184
func main() {

tutorials/commands/README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ You can find the non-annotated version of this program [on GitHub][source].
99
[basics]: https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics
1010
[source]: https://github.com/charmbracelet/bubbletea/blob/master/tutorials/commands/main.go
1111

12-
## Let's Go!
12+
## Lets Go!
1313

14-
For this tutorial we're building a very simple program that makes an HTTP
14+
For this tutorial were building a very simple program that makes an HTTP
1515
request to a server and reports the status code of the response.
1616

17-
We'll import a few necessary packages and put the URL we're going to check in
17+
Well import a few necessary packages and put the URL were going to check in
1818
a `const`.
1919

2020
```go
@@ -26,15 +26,15 @@ import (
2626
"os"
2727
"time"
2828

29-
tea "github.com/charmbracelet/bubbletea/v2"
29+
tea "charm.land/bubbletea/v2"
3030
)
3131

3232
const url = "https://charm.sh/"
3333
```
3434

3535
## The Model
3636

37-
Next we'll define our model. The only things we need to store are the status
37+
Next well define our model. The only things we need to store are the status
3838
code of the HTTP response and a possible error.
3939

4040
```go
@@ -51,7 +51,7 @@ time, ticking a timer, reading from the disk, and network stuff are all I/O and
5151
should be run through commands. That might sound harsh, but it will keep your
5252
Bubble Tea program straightforward and simple.
5353

54-
Anyway, let's write a `Cmd` that makes a request to a server and returns the
54+
Anyway, lets write a `Cmd` that makes a request to a server and returns the
5555
result as a `Msg`.
5656

5757
```go
@@ -80,19 +80,19 @@ type errMsg struct{ err error }
8080
func (e errMsg) Error() string { return e.err.Error() }
8181
```
8282

83-
And notice that we've defined two new `Msg` types. They can be any type, even
84-
an empty struct. We'll come back to them later in our update function.
85-
First, let's write our initialization function.
83+
And notice that weve defined two new `Msg` types. They can be any type, even
84+
an empty struct. Well come back to them later in our update function.
85+
First, lets write our initialization function.
8686

8787
## The Initialization Method
8888

8989
The initialization method is very simple: we return the `Cmd` we made earlier.
90-
Note that we don't call the function; the Bubble Tea runtime will do that when
90+
Note that we dont call the function; the Bubble Tea runtime will do that when
9191
the time is right.
9292

9393
```go
94-
func (m model) Init() (tea.Model, tea.Cmd) {
95-
return m, checkServer
94+
func (m model) Init() tea.Cmd {
95+
return checkServer
9696
}
9797
```
9898

@@ -138,13 +138,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
138138
## The View Function
139139

140140
Our view is very straightforward. We look at the current model and build a
141-
string accordingly:
141+
`tea.View` accordingly:
142142

143143
```go
144-
func (m model) View() string {
144+
func (m model) View() tea.View {
145145
// If there's an error, print it out and don't do anything else.
146146
if m.err != nil {
147-
return fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err)
147+
return tea.NewView(fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err))
148148
}
149149

150150
// Tell the user we're doing something.
@@ -156,14 +156,14 @@ func (m model) View() string {
156156
}
157157

158158
// Send off whatever we came up with above for rendering.
159-
return "\n" + s + "\n\n"
159+
return tea.NewView("\n" + s + "\n\n")
160160
}
161161
```
162162

163163
## Run the program
164164

165-
The only thing left to do is run the program, so let's do that! Our initial
166-
model doesn't need any data at all in this case, we just initialize it with
165+
The only thing left to do is run the program, so lets do that! Our initial
166+
model doesnt need any data at all in this case, we just initialize it with
167167
a `model` struct with default values.
168168

169169
```go
@@ -175,13 +175,13 @@ func main() {
175175
}
176176
```
177177

178-
And that's that. There's one more thing that is helpful to know about
178+
And thats that. Theres one more thing that is helpful to know about
179179
`Cmd`s, though.
180180

181181
## One More Thing About Commands
182182

183-
`Cmd`s are defined in Bubble Tea as `type Cmd func() Msg`. So they're just
184-
functions that don't take any arguments and return a `Msg`, which can be
183+
`Cmd`s are defined in Bubble Tea as `type Cmd func() Msg`. So theyre just
184+
functions that dont take any arguments and return a `Msg`, which can be
185185
any type. If you need to pass arguments to a command, you just make a function
186186
that returns a command. For example:
187187

@@ -209,7 +209,7 @@ func checkSomeUrl(url string) tea.Cmd {
209209
```
210210

211211
Anyway, just make sure you do as much stuff as you can in the innermost
212-
function, because that's the one that runs asynchronously.
212+
function, because thats the one that runs asynchronously.
213213

214214
## Now What?
215215

@@ -221,7 +221,7 @@ a component library for Bubble Tea.
221221
And, of course, check out the [Go Docs][docs].
222222

223223
[bubbles]: https://github.com/charmbracelet/bubbles
224-
[docs]: https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc
224+
[docs]: https://pkg.go.dev/charm.land/bubbletea/v2?tab=doc
225225
[examples]: https://github.com/charmbracelet/bubbletea/tree/master/examples
226226

227227
## Additional Resources
@@ -231,7 +231,7 @@ And, of course, check out the [Go Docs][docs].
231231

232232
### Feedback
233233

234-
We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!
234+
Wed love to hear your thoughts on this tutorial. Feel free to drop us a note!
235235

236236
- [Twitter](https://twitter.com/charmcli)
237237
- [The Fediverse](https://mastodon.social/@charmcli)

tutorials/commands/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77
"time"
88

9-
tea "github.com/charmbracelet/bubbletea/v2"
9+
tea "charm.land/bubbletea/v2"
1010
)
1111

1212
const url = "https://charm.sh/"
@@ -58,16 +58,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5858
return m, nil
5959
}
6060

61-
func (m model) View() string {
61+
func (m model) View() tea.View {
6262
if m.err != nil {
63-
return fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err)
63+
return tea.NewView(fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err))
6464
}
6565

6666
s := fmt.Sprintf("Checking %s ... ", url)
6767
if m.status > 0 {
6868
s += fmt.Sprintf("%d %s!", m.status, http.StatusText(m.status))
6969
}
70-
return "\n" + s + "\n\n"
70+
return tea.NewView("\n" + s + "\n\n")
7171
}
7272

7373
func main() {

0 commit comments

Comments
 (0)