Skip to content
Myr

docs: link to tutorial + general README edits (#344)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* docs: clean up readme * chore: add references * chore: set to 80 char width * docs: remove 404 docs link for now + tidy up READMEs * docs: small language change in README

authored by bashbunni · committed by GitHub · parent ae4381a · Browse files · 4 files+49 added−238 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Christian Rocha <christian@rocha.is>

Diff stats

4 files+49 added−238 removed

  • other:+49 added−238 removed

4 files+49 added−238 removed

modified:tutorials/basics/README.md+8 added-7 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 command73Next, 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,75we'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 it's current state and use it to return155simplest. We look at the model in its 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)234235***236237Part of [Charm](https://charm.sh).238238<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>239<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>240241Charm热爱开源 • Charm loves open source
modified:tutorials/commands/README.md+2 added-1 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)240241***242243Part of [Charm](https://charm.sh).244244<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>245<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a>246247Charm热爱开源 • Charm loves open source
modified:.gitignore+1 added
16 unmodified lines
17tutorials/basics/basics18tutorials/commands/commands19.idea20coverage.txt21README.md.*
modified:README.md+38 added-230 removed
19 unmodified lines
20performance optimizations we’ve added along the way. Among those is a standard21framerate-based renderer, a renderer for high-performance scrollable22regions which works alongside the main renderer, and mouse support.2324To get started, see the tutorial below, the [examples][examples], the25[docs][docs] and some common [resources](#libraries-we-use-with-bubble-tea).24## Getting Started2527## By the way26We recommend starting with the [basics tutorial][basics] followed by the27[commands tutorial][commands], both of which should give you a good28understanding of how things work.2929Be sure to check out [Bubbles][bubbles], a library of common UI components for Bubble Tea.30There are a bunch of [examples][examples], too!3132[basics]: https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics33[commands]: https://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands34[documentation]: https://github.com/charmbracelet/bubbletea/tree/master/docs35[examples]: https://github.com/charmbracelet/bubbletea/tree/master/examples3637## Components3839For a bunch of basic user interface components check out [Bubbles][bubbles],40the official Bubble Tea component library.4142<p>43 <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;44 <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>45</p>4636* * *47## Debugging4838## Tutorial49### Debugging with Delve5040Bubble Tea is based on the functional design paradigms of [The Elm41Architecture][elm], which happen to work nicely with Go. It's a delightful way to42build applications.4344By the way, the non-annotated source code for this program is available45[on GitHub](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics).4647This tutorial assumes you have a working knowledge of Go.4849[elm]: https://guide.elm-lang.org/architecture/5051## Enough! Let's get to it.5253For this tutorial, we're making a shopping list.5455To start we'll define our package and import some libraries. Our only external56import will be the Bubble Tea library, which we'll call `tea` for short.5758```go59package main6061import (62 "fmt"63 "os"6465 tea "github.com/charmbracelet/bubbletea"66)67```6869Bubble Tea programs are comprised of a **model** that describes the application70state and three simple methods on that model:7172* **Init**, a function that returns an initial command for the application to run.73* **Update**, a function that handles incoming events and updates the model accordingly.74* **View**, a function that renders the UI based on the data in the model.7576## The Model7778So let's start by defining our model which will store our application's state.79It can be any type, but a `struct` usually makes the most sense.8081```go82type model struct {83 choices []string // items on the to-do list84 cursor int // which to-do list item our cursor is pointing at85 selected map[int]struct{} // which to-do items are selected86}87```8889## Initialization9091Next, we’ll define our application’s initial state. In this case, we’re defining92a function to return our initial model, however, we could just as easily define93the initial model as a variable elsewhere, too.9495```go96func initialModel() model {97 return model{98 // Our shopping list is a grocery list99 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},100101 // A map which indicates which choices are selected. We're using102 // the map like a mathematical set. The keys refer to the indexes103 // of the `choices` slice, above.104 selected: make(map[int]struct{}),105 }106}107```108109Next, we define the `Init` method. `Init` can return a `Cmd` that could perform110some initial I/O. For now, we don't need to do any I/O, so for the command,111we'll just return `nil`, which translates to "no command."112113```go114func (m model) Init() tea.Cmd {115 // Just return `nil`, which means "no I/O right now, please."116 return nil117}118```119120## The Update Method121122Next up is the update method. The update function is called when ”things123happen.” Its job is to look at what has happened and return an updated model in124response. It can also return a `Cmd` to make more things happen, but for now125don't worry about that part.126127In our case, when a user presses the down arrow, `Update`’s job is to notice128that the down arrow was pressed and move the cursor accordingly (or not).129130The “something happened” comes in the form of a `Msg`, which can be any type.131Messages are the result of some I/O that took place, such as a keypress, timer132tick, or a response from a server.133134We usually figure out which type of `Msg` we received with a type switch, but135you could also use a type assertion.136137For now, we'll just deal with `tea.KeyMsg` messages, which are automatically138sent to the update function when keys are pressed.139140```go141func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {142 switch msg := msg.(type) {143144 // Is it a key press?145 case tea.KeyMsg:146147 // Cool, what was the actual key pressed?148 switch msg.String() {149150 // These keys should exit the program.151 case "ctrl+c", "q":152 return m, tea.Quit153154 // The "up" and "k" keys move the cursor up155 case "up", "k":156 if m.cursor > 0 {157 m.cursor--158 }159160 // The "down" and "j" keys move the cursor down161 case "down", "j":162 if m.cursor < len(m.choices)-1 {163 m.cursor++164 }165166 // The "enter" key and the spacebar (a literal space) toggle167 // the selected state for the item that the cursor is pointing at.168 case "enter", " ":169 _, ok := m.selected[m.cursor]170 if ok {171 delete(m.selected, m.cursor)172 } else {173 m.selected[m.cursor] = struct{}{}174 }175 }176 }177178 // Return the updated model to the Bubble Tea runtime for processing.179 // Note that we're not returning a command.180 return m, nil181}182```183184You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return185a `tea.Quit` command with the model. That’s a special command which instructs186the Bubble Tea runtime to quit, exiting the program.187188## The View Method189190At last, it’s time to render our UI. Of all the methods, the view is the191simplest. We look at the model in its current state and use it to return192a `string`. That string is our UI!193194Because the view describes the entire UI of your application, you don’t have to195worry about redrawing logic and stuff like that. Bubble Tea takes care of it196for you.197198```go199func (m model) View() string {200 // The header201 s := "What should we buy at the market?\n\n"202203 // Iterate over our choices204 for i, choice := range m.choices {205206 // Is the cursor pointing at this choice?207 cursor := " " // no cursor208 if m.cursor == i {209 cursor = ">" // cursor!210 }211212 // Is this choice selected?213 checked := " " // not selected214 if _, ok := m.selected[i]; ok {215 checked = "x" // selected!216 }217218 // Render the row219 s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)220 }221222 // The footer223 s += "\nPress q to quit.\n"224225 // Send the UI for rendering226 return s227}228```229230## All Together Now231232The last step is to simply run our program. We pass our initial model to233`tea.NewProgram` and let it rip:234235```go236func main() {237 p := tea.NewProgram(initialModel())238 if err := p.Start(); err != nil {239 fmt.Printf("Alas, there's been an error: %v", err)240 os.Exit(1)241 }242}243```244245## What’s Next?246247This tutorial covers the basics of building an interactive terminal UI, but248in the real world you'll also need to perform I/O. To learn about that have a249look at the [Command Tutorial][cmd]. It's pretty simple.250251There are also several [Bubble Tea examples][examples] available and, of course,252there are [Go Docs][docs].253254[cmd]: http://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands/255[examples]: http://github.com/charmbracelet/bubbletea/tree/master/examples256[docs]: https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc257258## Debugging with Delve25951Since Bubble Tea apps assume control of stdin and stdout, you’ll need to run52delve in headless mode and then connect to it:5354```bash
7 unmodified lines
6263Note that the default port used will vary on your system and per run, so64actually watch out what address the first `dlv` run tells you to connect to.6566### Logging Stuff6768You can log to a debug file to print debug Bubble Tea applications. To do so,69include something like…7071```go72if len(os.Getenv("DEBUG")) > 0 {73 f, err := tea.LogToFile("debug.log", "debug")74 if err != nil {75 fmt.Println("fatal:", err)76 os.Exit(1)77 }78 defer f.Close()79}80```8182…before you start your Bubble Tea program. To see what’s printed in real time,83run `tail -f debug.go` while you run your program in another window.8485## Libraries we use with Bubble Tea8687* [Bubbles][bubbles]: Common Bubble Tea components such as text inputs, viewports, spinners and so on88* [Lip Gloss][lipgloss]: Style, format and layout tools for terminal applications
8 unmodified lines
97[bubblezone]: https://github.com/lrstanley/bubblezone98[termenv]: https://github.com/muesli/termenv99[reflow]: https://github.com/muesli/reflow100291## Additional utility libraries to use with Bubble Tea292293101## Bubble Tea in the Wild102103For some Bubble Tea programs in production, see:104
5 unmodified lines
110* [fork-cleaner](https://github.com/caarlos0/fork-cleaner): cleans up old and inactive forks in your GitHub account111* [gambit](https://github.com/maaslalani/gambit): play chess in the terminal112* [gembro](https://git.sr.ht/~rafael/gembro): a mouse-driven Gemini browser113* [gh-b](https://github.com/joaom00/gh-b): GitHub CLI extension to easily manage your branches307* [gh-dash](https://www.github.com/dlvhdr/gh-dash): GitHub cli extension to display a dashboard of PRs and issues114* [gh-dash](https://www.github.com/dlvhdr/gh-dash): GitHub CLI extension to display a dashboard of PRs and issues115* [gitflow-toolkit](https://github.com/mritd/gitflow-toolkit): a GitFlow submission tool116* [Glow](https://github.com/charmbracelet/glow): a markdown reader, browser and online markdown stash117* [gocovsh](https://github.com/orlangure/gocovsh): explore Go coverage reports from the CLI118* [httpit](https://github.com/gonetx/httpit): a rapid http(s) benchmark tool
23 unmodified lines
142We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!143144* [Twitter](https://twitter.com/charmcli)145* [The Fediverse](https://mastodon.technology/@charm)146* [Slack](https://charm.sh/slack)147148## Acknowledgments149150Bubble Tea is based on the paradigms of [The Elm Architecture][elm] by Evan
19 unmodified lines