Skip to content
Myr

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

4 files+239 added−27 removed

modified:tutorials/basics/README.md+7 added-8 removed
12 unmodified lines
13[elm]: https://guide.elm-lang.org/architecture/1415## Enough! Let's get to it.1617For this tutorial, we're making a shopping list.17For this tutorial we're making a shopping list.1819To start we'll define our package and import some libraries. Our only external20import will be the Bubble Tea library, which we'll call `tea` for short.21
29 unmodified lines
51```5253## Initialization5455Next, we’ll define our application’s initial state. In this case, we’re defining56a function to return our initial model, however, we could just as easily define55Next we’ll define our application’s initial state. In this case we’re defining56a function to return our initial model, however we could just as easily define57the initial model as a variable elsewhere, too.5859```go60func initialModel() model {
8 unmodified lines
69 }70}71```7273Next, we define the `Init` method. `Init` can return a `Cmd` that could perform74some initial I/O. For now, we don't need to do any I/O, so for the command,73Next we define the `Init` method. `Init` can return a `Cmd` that could perform74some initial I/O. For now, we don't need to do any I/O, so for the command75we'll just return `nil`, which translates to "no command."7677```go78func (m model) Init() tea.Cmd {
72 unmodified lines
151152## The View Method153154At last, it’s time to render our UI. Of all the methods, the view is the155simplest. We look at the model in its current state and use it to return155simplest. We look at the model in it's current state and use it to return156a `string`. That string is our UI!157158Because the view describes the entire UI of your application, you don’t have to159worry about redrawing logic and stuff like that. Bubble Tea takes care of it
69 unmodified lines
229We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!230231* [Twitter](https://twitter.com/charmcli)232* [The Fediverse](https://mastodon.technology/@charm)233* [Slack](https://charm.sh/slack)233234***235236Part of [Charm](https://charm.sh).237239<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>239240Charm热爱开源 • Charm loves open source
modified:tutorials/commands/README.md+1 added-2 removed
234 unmodified lines
235We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!236237* [Twitter](https://twitter.com/charmcli)238* [The Fediverse](https://mastodon.technology/@charm)239* [Slack](https://charm.sh/slack)239240***241242Part of [Charm](https://charm.sh).243245<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>245246Charm热爱开源 • Charm loves open source
modified:.gitignore-1 removed
18 unmodified lines
19tutorials/basics/basics20tutorials/commands/commands21.idea22coverage.txt23README.md.*
modified:README.md+231 added-16 removed
21 unmodified lines
22performance optimizations we’ve added along the way. Among those is a standard23framerate-based renderer, a renderer for high-performance scrollable24regions which works alongside the main renderer, and mouse support.2526## Getting Started26To 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).2828We recommend starting with the [basics tutorial][basics] followed by the29[commands tutorial][commands], both of which should give you a good30understanding of how things work.29[youtube]: https://charm.sh/yt3032There are a bunch of [examples][examples], too!31## By the way3234[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/examples33Be sure to check out [Bubbles][bubbles], a library of common UI components for Bubble Tea.3439## Components4041For a bunch of basic user interface components check out [Bubbles][bubbles],42the official Bubble Tea component library.4335<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>&nbsp;&nbsp;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>3940* * *4142## Tutorial4344Bubble Tea is based on the functional design paradigms of [The Elm45Architecture][elm], which happen to work nicely with Go. It's a delightful way to46build applications.4748By the way, the non-annotated source code for this program is available49[on GitHub](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics).5051This tutorial assumes you have a working knowledge of Go.5253[elm]: https://guide.elm-lang.org/architecture/5455## Enough! Let's get to it.5657For this tutorial, we're making a shopping list.5859To start we'll define our package and import some libraries. Our only external60import will be the Bubble Tea library, which we'll call `tea` for short.6162```go63package main6465import (66 "fmt"67 "os"6869 tea "github.com/charmbracelet/bubbletea"70)71```7273Bubble Tea programs are comprised of a **model** that describes the application74state and three simple methods on that model:7576* **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.7980## The Model8182So let's start by defining our model which will store our application's state.83It can be any type, but a `struct` usually makes the most sense.8485```go86type 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```9293## Initialization9495Next, we’ll define our application’s initial state. In this case, we’re defining96a function to return our initial model, however, we could just as easily define97the initial model as a variable elsewhere, too.9899```go100func initialModel() model {101 return model{102 // Our shopping list is a grocery list103 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},104105 // 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```112113Next, we define the `Init` method. `Init` can return a `Cmd` that could perform114some initial I/O. For now, we don't need to do any I/O, so for the command,115we'll just return `nil`, which translates to "no command."116117```go118func (m model) Init() tea.Cmd {119 // Just return `nil`, which means "no I/O right now, please."120 return nil121}122```123124## The Update Method125126Next up is the update method. The update function is called when ”things127happen.” Its job is to look at what has happened and return an updated model in128response. It can also return a `Cmd` to make more things happen, but for now129don't worry about that part.130131In our case, when a user presses the down arrow, `Update`’s job is to notice132that the down arrow was pressed and move the cursor accordingly (or not).133134The “something happened” comes in the form of a `Msg`, which can be any type.135Messages are the result of some I/O that took place, such as a keypress, timer136tick, or a response from a server.137138We usually figure out which type of `Msg` we received with a type switch, but139you could also use a type assertion.140141For now, we'll just deal with `tea.KeyMsg` messages, which are automatically142sent to the update function when keys are pressed.143144```go145func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {146 switch msg := msg.(type) {147148 // Is it a key press?149 case tea.KeyMsg:150151 // Cool, what was the actual key pressed?152 switch msg.String() {153154 // These keys should exit the program.155 case "ctrl+c", "q":156 return m, tea.Quit157158 // The "up" and "k" keys move the cursor up159 case "up", "k":160 if m.cursor > 0 {161 m.cursor--162 }163164 // The "down" and "j" keys move the cursor down165 case "down", "j":166 if m.cursor < len(m.choices)-1 {167 m.cursor++168 }169170 // 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 }181182 // 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```187188You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return189a `tea.Quit` command with the model. That’s a special command which instructs190the Bubble Tea runtime to quit, exiting the program.191192## The View Method193194At last, it’s time to render our UI. Of all the methods, the view is the195simplest. We look at the model in its current state and use it to return196a `string`. That string is our UI!197198Because the view describes the entire UI of your application, you don’t have to199worry about redrawing logic and stuff like that. Bubble Tea takes care of it200for you.201202```go203func (m model) View() string {204 // The header205 s := "What should we buy at the market?\n\n"206207 // Iterate over our choices208 for i, choice := range m.choices {209210 // Is the cursor pointing at this choice?211 cursor := " " // no cursor212 if m.cursor == i {213 cursor = ">" // cursor!214 }215216 // Is this choice selected?217 checked := " " // not selected218 if _, ok := m.selected[i]; ok {219 checked = "x" // selected!220 }221222 // Render the row223 s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)224 }225226 // The footer227 s += "\nPress q to quit.\n"228229 // Send the UI for rendering230 return s231}232```233234## All Together Now235236The last step is to simply run our program. We pass our initial model to237`tea.NewProgram` and let it rip:238239```go240func 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```248249## What’s Next?250251This tutorial covers the basics of building an interactive terminal UI, but252in the real world you'll also need to perform I/O. To learn about that have a253look at the [Command Tutorial][cmd]. It's pretty simple.254255There are also several [Bubble Tea examples][examples] available and, of course,256there are [Go Docs][docs].257258[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=doc261262## Debugging263264### Debugging with Delve265
46 unmodified lines
312[bubblezone]: https://github.com/lrstanley/bubblezone313[termenv]: https://github.com/muesli/termenv314[reflow]: https://github.com/muesli/reflow315316## Additional utility libraries to use with Bubble Tea317318319## Bubble Tea in the Wild320321For some Bubble Tea programs in production, see:322
11 unmodified lines
334* [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 tool
28 unmodified lines
371We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!372373* [Twitter](https://twitter.com/charmcli)374* [The Fediverse](https://mastodon.technology/@charm)159* [Slack](https://charm.sh/slack)375376## Acknowledgments377378Bubble Tea is based on the paradigms of [The Elm Architecture][elm] by Evan
19 unmodified lines