README.md
This file
Path
tutorials/commands/README.md
246 lines · 7.1 KiB · Markdown
Last changed by
docs: fix links to tutorials & examples (#1645)mhdnabefore adoption882727b
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247# Commands in Bubble Tea
This is the second tutorial for Bubble Tea covering commands, which deal withI/O. The tutorial assumes you have a working knowledge of Go and a decentunderstanding of [the first tutorial][basics].
You can find the non-annotated version of this program [on GitHub][source].
[basics]: https://github.com/charmbracelet/bubbletea/tree/main/tutorials/basics[source]: https://github.com/charmbracelet/bubbletea/blob/master/tutorials/commands/main.go
## Let’s Go!
For this tutorial we’re building a very simple program that makes an HTTPrequest to a server and reports the status code of the response.
We’ll import a few necessary packages and put the URL we’re going to check ina `const`.
```gopackage main
import ( "fmt" "net/http" "os" "time"
tea "charm.land/bubbletea/v2")
const url = "https://charm.sh/"```
## The Model
Next we’ll define our model. The only things we need to store are the statuscode of the HTTP response and a possible error.
```gotype model struct { status int err error}```
## Commands and Messages
`Cmd`s are functions that perform some I/O and then return a `Msg`. Checking thetime, ticking a timer, reading from the disk, and network stuff are all I/O andshould be run through commands. That might sound harsh, but it will keep yourBubble Tea program straightforward and simple.
Anyway, let’s write a `Cmd` that makes a request to a server and returns theresult as a `Msg`.
```gofunc checkServer() tea.Msg {
// Create an HTTP client and make a GET request. c := &http.Client{Timeout: 10 * time.Second} res, err := c.Get(url)
if err != nil { // There was an error making our request. Wrap the error we received // in a message and return it. return errMsg{err} } // We received a response from the server. Return the HTTP status code // as a message. return statusMsg(res.StatusCode)}
type statusMsg int
type errMsg struct{ err error }
// For messages that contain errors it's often handy to also implement the// error interface on the message.func (e errMsg) Error() string { return e.err.Error() }```
And notice that we’ve defined two new `Msg` types. They can be any type, evenan empty struct. We’ll come back to them later in our update function.First, let’s write our initialization function.
## The Initialization Method
The initialization method is very simple: we return the `Cmd` we made earlier.Note that we don’t call the function; the Bubble Tea runtime will do that whenthe time is right.
```gofunc (m model) Init() tea.Cmd { return checkServer}```
## The Update Method
Internally, `Cmd`s run asynchronously in a goroutine. The `Msg` they return iscollected and sent to our update function for handling. Remember those messagetypes we made earlier when we were making the `checkServer` command? We handlethem here. This makes dealing with many asynchronous operations very easy.
```gofunc (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) {
case statusMsg: // The server returned a status message. Save it to our model. Also // tell the Bubble Tea runtime we want to exit because we have nothing // else to do. We'll still be able to render a final view with our // status message. m.status = int(msg) return m, tea.Quit
case errMsg: // There was an error. Note it in the model. And tell the runtime // we're done and want to quit. m.err = msg return m, tea.Quit
case tea.KeyPressMsg: // Ctrl+c exits. Even with short running programs it's good to have // a quit key, just in case your logic is off. Users will be very // annoyed if they can't exit. if msg.Mod == tea.ModCtrl && msg.Code == 'c' { return m, tea.Quit } }
// If we happen to get any other messages, don't do anything. return m, nil}```
## The View Function
Our view is very straightforward. We look at the current model and build a`tea.View` accordingly:
```gofunc (m model) View() tea.View { // If there's an error, print it out and don't do anything else. if m.err != nil { return tea.NewView(fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err)) }
// Tell the user we're doing something. s := fmt.Sprintf("Checking %s ... ", url)
// When the server responds with a status, add it to the current line. if m.status > 0 { s += fmt.Sprintf("%d %s!", m.status, http.StatusText(m.status)) }
// Send off whatever we came up with above for rendering. return tea.NewView("\n" + s + "\n\n")}```
## Run the program
The only thing left to do is run the program, so let’s do that! Our initialmodel doesn’t need any data at all in this case, we just initialize it witha `model` struct with default values.
```gofunc main() { if _, err := tea.NewProgram(model{}).Run(); err != nil { fmt.Printf("Uh oh, there was an error: %v\n", err) os.Exit(1) }}```
And that’s that. There’s one more thing that is helpful to know about`Cmd`s, though.
## One More Thing About Commands
`Cmd`s are defined in Bubble Tea as `type Cmd func() Msg`. So they’re justfunctions that don’t take any arguments and return a `Msg`, which can beany type. If you need to pass arguments to a command, you just make a functionthat returns a command. For example:
```gofunc cmdWithArg(id int) tea.Cmd { return func() tea.Msg { return someMsg{id: id} }}```
A more real-world example looks like:
```gofunc checkSomeUrl(url string) tea.Cmd { return func() tea.Msg { c := &http.Client{Timeout: 10 * time.Second} res, err := c.Get(url) if err != nil { return errMsg{err} } return statusMsg(res.StatusCode) }}```
Anyway, just make sure you do as much stuff as you can in the innermostfunction, because that’s the one that runs asynchronously.
## Now What?
After doing this tutorial and [the previous one][basics] you should be ready tobuild a Bubble Tea program of your own. We also recommend that you look at theBubble Tea [example programs][examples] as well as [Bubbles][bubbles],a component library for Bubble Tea.
And, of course, check out the [Go Docs][docs].
[bubbles]: https://github.com/charmbracelet/bubbles[docs]: https://pkg.go.dev/charm.land/bubbletea/v2?tab=doc[examples]: https://github.com/charmbracelet/bubbletea/tree/main/examples
## Additional Resources
- [Libraries we use with Bubble Tea](https://github.com/charmbracelet/bubbletea/#libraries-we-use-with-bubble-tea)- [Bubble Tea in the Wild](https://github.com/charmbracelet/bubbletea/#bubble-tea-in-the-wild)
### Feedback
We’d love to hear your thoughts on this tutorial. Feel free to drop us a note!
- [Twitter](https://twitter.com/charmcli)- [The Fediverse](https://mastodon.social/@charmcli)- [Discord](https://charm.sh/chat)
---
Part of [Charm](https://charm.sh).
<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>
Charm热爱开源 • Charm loves open source