docs: move tutorial back to README
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by bashbunni · committed by Christian Rocha · parent 8ddf2a3 · Browse files · 4 files·+239 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+239 added−27 removed
- other:+239 added−27 removed
Provenance
- Commit
14422f5- Tree
7e0adf4- Author
bashbunni <bashbunni.io@gmail.com>- Committer
Christian Rocha <christian@rocha.is>- Parent
8ddf2a3
4 files changed+239 added−27 removed
modified:tutorials/basics/README.md+7 added-8 removed
12 unmodified lines13 [elm]: https://guide.elm-lang.org/architecture/14 15 ## Enough! Let's get to it.16 17-For this tutorial, we're making a shopping list.17+For this tutorial we're making a shopping list.18 19 To start we'll define our package and import some libraries. Our only external20 import will be the Bubble Tea library, which we'll call `tea` for short.21 29 unmodified lines51 ```52 53 ## Initialization54 55-Next, we’ll define our application’s initial state. In this case, we’re defining56-a function to return our initial model, however, we could just as easily define55+Next we’ll define our application’s initial state. In this case we’re defining56+a function to return our initial model, however we could just as easily define57 the initial model as a variable elsewhere, too.58 59 ```go60 func initialModel() model {8 unmodified lines69 }70 }71 ```72 73-Next, we define the `Init` method. `Init` can return a `Cmd` that could perform74-some initial I/O. For now, we don't need to do any I/O, so for the command,73+Next we define the `Init` method. `Init` can return a `Cmd` that could perform74+some initial I/O. For now, we don't need to do any I/O, so for the command75 we'll just return `nil`, which translates to "no command."76 77 ```go78 func (m model) Init() tea.Cmd {72 unmodified lines151 152 ## The View Method153 154 At last, it’s time to render our UI. Of all the methods, the view is the155-simplest. We look at the model in its current state and use it to return155+simplest. We look at the model in it's current state and use it to return156 a `string`. That string is our UI!157 158 Because the view describes the entire UI of your application, you don’t have to159 worry about redrawing logic and stuff like that. Bubble Tea takes care of it69 unmodified lines229 We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!230 231 * [Twitter](https://twitter.com/charmcli)232 * [The Fediverse](https://mastodon.technology/@charm)233-* [Slack](https://charm.sh/slack)233 234 ***235 236 Part of [Charm](https://charm.sh).237 239-<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>238+<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>239 240 Charm热爱开源 • Charm loves open sourcemodified:tutorials/commands/README.md+1 added-2 removed
234 unmodified lines235 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)239-* [Slack](https://charm.sh/slack)239 240 ***241 242 Part of [Charm](https://charm.sh).243 245-<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>244+<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>245 246 Charm热爱开源 • Charm loves open sourcemodified:.gitignore-1 removed
18 unmodified lines19 tutorials/basics/basics20 tutorials/commands/commands21 .idea22 coverage.txt23-README.md.*modified:README.md+231 added-16 removed
21 unmodified lines22 performance optimizations we’ve added along the way. Among those is a standard23 framerate-based renderer, a renderer for high-performance scrollable24 regions which works alongside the main renderer, and mouse support.25 26-## Getting Started26+To get started, see the tutorial below, the [examples][examples], the27+[docs][docs], the [video tutorials][youtube] and some common [resources](#libraries-we-use-with-bubble-tea).28 28-We recommend starting with the [basics tutorial][basics] followed by the29-[commands tutorial][commands], both of which should give you a good30-understanding of how things work.29+[youtube]: https://charm.sh/yt30 32-There are a bunch of [examples][examples], too!31+## By the way32 34-[basics]: https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics35-[commands]: https://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands36-[documentation]: https://github.com/charmbracelet/bubbletea/tree/master/docs37-[examples]: https://github.com/charmbracelet/bubbletea/tree/master/examples33+Be sure to check out [Bubbles][bubbles], a library of common UI components for Bubble Tea.34 39-## Components40-41-For a bunch of basic user interface components check out [Bubbles][bubbles],42-the official Bubble Tea component library.43-35 <p>36 <a href="https://github.com/charmbracelet/bubbles"><img src="https://stuff.charm.sh/bubbles/bubbles-badge.png" width="174" alt="Bubbles Badge"></a> 37 <a href="https://github.com/charmbracelet/bubbles"><img src="https://stuff.charm.sh/bubbles-examples/textinput.gif" width="400" alt="Text Input Example from Bubbles"></a>38 </p>39 40+* * *41+42+## Tutorial43+44+Bubble Tea is based on the functional design paradigms of [The Elm45+Architecture][elm], which happen to work nicely with Go. It's a delightful way to46+build applications.47+48+By the way, the non-annotated source code for this program is available49+[on GitHub](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics).50+51+This tutorial assumes you have a working knowledge of Go.52+53+[elm]: https://guide.elm-lang.org/architecture/54+55+## Enough! Let's get to it.56+57+For this tutorial, we're making a shopping list.58+59+To start we'll define our package and import some libraries. Our only external60+import will be the Bubble Tea library, which we'll call `tea` for short.61+62+```go63+package main64+65+import (66+ "fmt"67+ "os"68+69+ tea "github.com/charmbracelet/bubbletea"70+)71+```72+73+Bubble Tea programs are comprised of a **model** that describes the application74+state and three simple methods on that model:75+76+* **Init**, a function that returns an initial command for the application to run.77+* **Update**, a function that handles incoming events and updates the model accordingly.78+* **View**, a function that renders the UI based on the data in the model.79+80+## The Model81+82+So let's start by defining our model which will store our application's state.83+It can be any type, but a `struct` usually makes the most sense.84+85+```go86+type model struct {87+ choices []string // items on the to-do list88+ cursor int // which to-do list item our cursor is pointing at89+ selected map[int]struct{} // which to-do items are selected90+}91+```92+93+## Initialization94+95+Next, we’ll define our application’s initial state. In this case, we’re defining96+a function to return our initial model, however, we could just as easily define97+the initial model as a variable elsewhere, too.98+99+```go100+func initialModel() model {101+ return model{102+ // Our shopping list is a grocery list103+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},104+105+ // A map which indicates which choices are selected. We're using106+ // the map like a mathematical set. The keys refer to the indexes107+ // of the `choices` slice, above.108+ selected: make(map[int]struct{}),109+ }110+}111+```112+113+Next, we define the `Init` method. `Init` can return a `Cmd` that could perform114+some initial I/O. For now, we don't need to do any I/O, so for the command,115+we'll just return `nil`, which translates to "no command."116+117+```go118+func (m model) Init() tea.Cmd {119+ // Just return `nil`, which means "no I/O right now, please."120+ return nil121+}122+```123+124+## The Update Method125+126+Next up is the update method. The update function is called when ”things127+happen.” Its job is to look at what has happened and return an updated model in128+response. It can also return a `Cmd` to make more things happen, but for now129+don't worry about that part.130+131+In our case, when a user presses the down arrow, `Update`’s job is to notice132+that the down arrow was pressed and move the cursor accordingly (or not).133+134+The “something happened” comes in the form of a `Msg`, which can be any type.135+Messages are the result of some I/O that took place, such as a keypress, timer136+tick, or a response from a server.137+138+We usually figure out which type of `Msg` we received with a type switch, but139+you could also use a type assertion.140+141+For now, we'll just deal with `tea.KeyMsg` messages, which are automatically142+sent to the update function when keys are pressed.143+144+```go145+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {146+ switch msg := msg.(type) {147+148+ // Is it a key press?149+ case tea.KeyMsg:150+151+ // Cool, what was the actual key pressed?152+ switch msg.String() {153+154+ // These keys should exit the program.155+ case "ctrl+c", "q":156+ return m, tea.Quit157+158+ // The "up" and "k" keys move the cursor up159+ case "up", "k":160+ if m.cursor > 0 {161+ m.cursor--162+ }163+164+ // The "down" and "j" keys move the cursor down165+ case "down", "j":166+ if m.cursor < len(m.choices)-1 {167+ m.cursor++168+ }169+170+ // The "enter" key and the spacebar (a literal space) toggle171+ // the selected state for the item that the cursor is pointing at.172+ case "enter", " ":173+ _, ok := m.selected[m.cursor]174+ if ok {175+ delete(m.selected, m.cursor)176+ } else {177+ m.selected[m.cursor] = struct{}{}178+ }179+ }180+ }181+182+ // Return the updated model to the Bubble Tea runtime for processing.183+ // Note that we're not returning a command.184+ return m, nil185+}186+```187+188+You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return189+a `tea.Quit` command with the model. That’s a special command which instructs190+the Bubble Tea runtime to quit, exiting the program.191+192+## The View Method193+194+At last, it’s time to render our UI. Of all the methods, the view is the195+simplest. We look at the model in its current state and use it to return196+a `string`. That string is our UI!197+198+Because the view describes the entire UI of your application, you don’t have to199+worry about redrawing logic and stuff like that. Bubble Tea takes care of it200+for you.201+202+```go203+func (m model) View() string {204+ // The header205+ s := "What should we buy at the market?\n\n"206+207+ // Iterate over our choices208+ for i, choice := range m.choices {209+210+ // Is the cursor pointing at this choice?211+ cursor := " " // no cursor212+ if m.cursor == i {213+ cursor = ">" // cursor!214+ }215+216+ // Is this choice selected?217+ checked := " " // not selected218+ if _, ok := m.selected[i]; ok {219+ checked = "x" // selected!220+ }221+222+ // Render the row223+ s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)224+ }225+226+ // The footer227+ s += "\nPress q to quit.\n"228+229+ // Send the UI for rendering230+ return s231+}232+```233+234+## All Together Now235+236+The last step is to simply run our program. We pass our initial model to237+`tea.NewProgram` and let it rip:238+239+```go240+func main() {241+ p := tea.NewProgram(initialModel())242+ if err := p.Start(); err != nil {243+ fmt.Printf("Alas, there's been an error: %v", err)244+ os.Exit(1)245+ }246+}247+```248+249+## What’s Next?250+251+This tutorial covers the basics of building an interactive terminal UI, but252+in the real world you'll also need to perform I/O. To learn about that have a253+look at the [Command Tutorial][cmd]. It's pretty simple.254+255+There are also several [Bubble Tea examples][examples] available and, of course,256+there are [Go Docs][docs].257+258+[cmd]: http://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands/259+[examples]: http://github.com/charmbracelet/bubbletea/tree/master/examples260+[docs]: https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc261+262 ## Debugging263 264 ### Debugging with Delve265 46 unmodified lines312 [bubblezone]: https://github.com/lrstanley/bubblezone313 [termenv]: https://github.com/muesli/termenv314 [reflow]: https://github.com/muesli/reflow315 316+## Additional utility libraries to use with Bubble Tea317+318+319 ## Bubble Tea in the Wild320 321 For some Bubble Tea programs in production, see:322 11 unmodified lines334 * [fork-cleaner](https://github.com/caarlos0/fork-cleaner): cleans up old and inactive forks in your GitHub account335 * [gambit](https://github.com/maaslalani/gambit): play chess in the terminal336 * [gembro](https://git.sr.ht/~rafael/gembro): a mouse-driven Gemini browser337 * [gh-b](https://github.com/joaom00/gh-b): GitHub CLI extension to easily manage your branches122-* [gh-dash](https://www.github.com/dlvhdr/gh-dash): GitHub CLI extension to display a dashboard of PRs and issues338+* [gh-dash](https://www.github.com/dlvhdr/gh-dash): GitHub cli extension to display a dashboard of PRs and issues339 * [gitflow-toolkit](https://github.com/mritd/gitflow-toolkit): a GitFlow submission tool340 * [Glow](https://github.com/charmbracelet/glow): a markdown reader, browser and online markdown stash341 * [gocovsh](https://github.com/orlangure/gocovsh): explore Go coverage reports from the CLI342 * [httpit](https://github.com/gonetx/httpit): a rapid http(s) benchmark tool28 unmodified lines371 We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!372 373 * [Twitter](https://twitter.com/charmcli)374 * [The Fediverse](https://mastodon.technology/@charm)159-* [Slack](https://charm.sh/slack)375 376 ## Acknowledgments377 378 Bubble Tea is based on the paradigms of [The Elm Architecture][elm] by Evan19 unmodified lines