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
Provenance
- Commit
219ed4e- Tree
80844bd- Author
bashbunni <15822994+bashbunni@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ae4381a
4 files changed+49 added−238 removed
modified:tutorials/basics/README.md+8 added-7 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 command73+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,75 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 it's current state and use it to return155+simplest. We look at the model in its 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)234 235 ***236 237 Part of [Charm](https://charm.sh).238 238-<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>240 241 Charm热爱开源 • Charm loves open sourcemodified:tutorials/commands/README.md+2 added-1 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)240 241 ***242 243 Part of [Charm](https://charm.sh).244 244-<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>246 247 Charm热爱开源 • Charm loves open sourcemodified:.gitignore+1 added
16 unmodified lines17 tutorials/basics/basics18 tutorials/commands/commands19 .idea20 coverage.txt21+README.md.*modified:README.md+38 added-230 removed
19 unmodified lines20 performance optimizations we’ve added along the way. Among those is a standard21 framerate-based renderer, a renderer for high-performance scrollable22 regions which works alongside the main renderer, and mouse support.23 24-To get started, see the tutorial below, the [examples][examples], the25-[docs][docs] and some common [resources](#libraries-we-use-with-bubble-tea).24+## Getting Started25 27-## By the way26+We recommend starting with the [basics tutorial][basics] followed by the27+[commands tutorial][commands], both of which should give you a good28+understanding of how things work.29 29-Be sure to check out [Bubbles][bubbles], a library of common UI components for Bubble Tea.30+There are a bunch of [examples][examples], too!31 32+[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/examples36+37+## Components38+39+For a bunch of basic user interface components check out [Bubbles][bubbles],40+the official Bubble Tea component library.41+42 <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> 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>46 36-* * *47+## Debugging48 38-## Tutorial49+### Debugging with Delve50 40-Bubble Tea is based on the functional design paradigms of [The Elm41-Architecture][elm], which happen to work nicely with Go. It's a delightful way to42-build applications.43-44-By the way, the non-annotated source code for this program is available45-[on GitHub](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics).46-47-This tutorial assumes you have a working knowledge of Go.48-49-[elm]: https://guide.elm-lang.org/architecture/50-51-## Enough! Let's get to it.52-53-For this tutorial, we're making a shopping list.54-55-To start we'll define our package and import some libraries. Our only external56-import will be the Bubble Tea library, which we'll call `tea` for short.57-58-```go59-package main60-61-import (62- "fmt"63- "os"64-65- tea "github.com/charmbracelet/bubbletea"66-)67-```68-69-Bubble Tea programs are comprised of a **model** that describes the application70-state and three simple methods on that model:71-72-* **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.75-76-## The Model77-78-So let's start by defining our model which will store our application's state.79-It can be any type, but a `struct` usually makes the most sense.80-81-```go82-type 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-```88-89-## Initialization90-91-Next, we’ll define our application’s initial state. In this case, we’re defining92-a function to return our initial model, however, we could just as easily define93-the initial model as a variable elsewhere, too.94-95-```go96-func initialModel() model {97- return model{98- // Our shopping list is a grocery list99- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},100-101- // 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-```108-109-Next, we define the `Init` method. `Init` can return a `Cmd` that could perform110-some initial I/O. For now, we don't need to do any I/O, so for the command,111-we'll just return `nil`, which translates to "no command."112-113-```go114-func (m model) Init() tea.Cmd {115- // Just return `nil`, which means "no I/O right now, please."116- return nil117-}118-```119-120-## The Update Method121-122-Next up is the update method. The update function is called when ”things123-happen.” Its job is to look at what has happened and return an updated model in124-response. It can also return a `Cmd` to make more things happen, but for now125-don't worry about that part.126-127-In our case, when a user presses the down arrow, `Update`’s job is to notice128-that the down arrow was pressed and move the cursor accordingly (or not).129-130-The “something happened” comes in the form of a `Msg`, which can be any type.131-Messages are the result of some I/O that took place, such as a keypress, timer132-tick, or a response from a server.133-134-We usually figure out which type of `Msg` we received with a type switch, but135-you could also use a type assertion.136-137-For now, we'll just deal with `tea.KeyMsg` messages, which are automatically138-sent to the update function when keys are pressed.139-140-```go141-func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {142- switch msg := msg.(type) {143-144- // Is it a key press?145- case tea.KeyMsg:146-147- // Cool, what was the actual key pressed?148- switch msg.String() {149-150- // These keys should exit the program.151- case "ctrl+c", "q":152- return m, tea.Quit153-154- // The "up" and "k" keys move the cursor up155- case "up", "k":156- if m.cursor > 0 {157- m.cursor--158- }159-160- // The "down" and "j" keys move the cursor down161- case "down", "j":162- if m.cursor < len(m.choices)-1 {163- m.cursor++164- }165-166- // 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- }177-178- // 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-```183-184-You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return185-a `tea.Quit` command with the model. That’s a special command which instructs186-the Bubble Tea runtime to quit, exiting the program.187-188-## The View Method189-190-At last, it’s time to render our UI. Of all the methods, the view is the191-simplest. We look at the model in its current state and use it to return192-a `string`. That string is our UI!193-194-Because the view describes the entire UI of your application, you don’t have to195-worry about redrawing logic and stuff like that. Bubble Tea takes care of it196-for you.197-198-```go199-func (m model) View() string {200- // The header201- s := "What should we buy at the market?\n\n"202-203- // Iterate over our choices204- for i, choice := range m.choices {205-206- // Is the cursor pointing at this choice?207- cursor := " " // no cursor208- if m.cursor == i {209- cursor = ">" // cursor!210- }211-212- // Is this choice selected?213- checked := " " // not selected214- if _, ok := m.selected[i]; ok {215- checked = "x" // selected!216- }217-218- // Render the row219- s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)220- }221-222- // The footer223- s += "\nPress q to quit.\n"224-225- // Send the UI for rendering226- return s227-}228-```229-230-## All Together Now231-232-The last step is to simply run our program. We pass our initial model to233-`tea.NewProgram` and let it rip:234-235-```go236-func 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-```244-245-## What’s Next?246-247-This tutorial covers the basics of building an interactive terminal UI, but248-in the real world you'll also need to perform I/O. To learn about that have a249-look at the [Command Tutorial][cmd]. It's pretty simple.250-251-There are also several [Bubble Tea examples][examples] available and, of course,252-there are [Go Docs][docs].253-254-[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=doc257-258-## Debugging with Delve259-51 Since Bubble Tea apps assume control of stdin and stdout, you’ll need to run52 delve in headless mode and then connect to it:53 54 ```bash7 unmodified lines62 63 Note that the default port used will vary on your system and per run, so64 actually watch out what address the first `dlv` run tells you to connect to.65 66+### Logging Stuff67+68+You can log to a debug file to print debug Bubble Tea applications. To do so,69+include something like…70+71+```go72+if 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+```81+82+…before you start your Bubble Tea program. To see what’s printed in real time,83+run `tail -f debug.go` while you run your program in another window.84+85 ## Libraries we use with Bubble Tea86 87 * [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 applications8 unmodified lines97 [bubblezone]: https://github.com/lrstanley/bubblezone98 [termenv]: https://github.com/muesli/termenv99 [reflow]: https://github.com/muesli/reflow100 291-## Additional utility libraries to use with Bubble Tea292-293-101 ## Bubble Tea in the Wild102 103 For some Bubble Tea programs in production, see:104 5 unmodified lines110 * [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 tool23 unmodified lines142 We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!143 144 * [Twitter](https://twitter.com/charmcli)145 * [The Fediverse](https://mastodon.technology/@charm)146+* [Slack](https://charm.sh/slack)147 148 ## Acknowledgments149 150 Bubble Tea is based on the paradigms of [The Elm Architecture][elm] by Evan19 unmodified lines