Improve formatting and wording in basics tutorial
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent 9c5d426 · Browse files · 2 files·+174 added−173 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+174 added−173 removed
- other:+174 added−173 removed
Provenance
- Commit
dc3e244- Tree
4c246df- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
9c5d426
2 files changed+174 added−173 removed
modified:tutorials/basics/README.md+87 added-86 removed
1 Bubble Tea Basics2 =================3 4 Bubble Tea is based on the functional design paradigms of [The Elm5-Architecture][elm] which happens work nicely with Go.5+Architecture][elm] which happens work nicely with Go. It's a delightful way to6+build applications.7 8 By the way, the non-annotated source code for this program is available9 [on GitHub](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics).10 8 unmodified lines19 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 22 ```go22- package main23+package main24 24- import (25- "fmt"26- "os"25+import (26+ "fmt"27+ "os"28 28- tea "github.com/charmbracelet/bubbletea"29- )29+ tea "github.com/charmbracelet/bubbletea"30+)31 ```32 33 Bubble Tea programs are comprised of a **model** that describes the application34 state and three simple methods on that model:34-35+We36 * **Init**, a function that returns an initial command for the application to run.37 * **Update**, a function that handles incoming events and updates the model accordingly.38 * **View**, a function that renders the UI based on the data in the model.39 2 unmodified lines42 So let's start by defining our model which will store our application's state.43 It can be any type, but a `struct` usually makes the most sense.44 45 ```go45- type model struct {46- choices []string // items on the to-do list47- cursor int // which to-do list item our cursor is pointing at48- selected map[int]struct{} // which to-do items are selected49- }46+type model struct {47+ choices []string // items on the to-do list48+ cursor int // which to-do list item our cursor is pointing at49+ selected map[int]struct{} // which to-do items are selected50+}51 ```52 53 ## Initialization54 3 unmodified lines58 do any I/O, so for the command we'll just return `nil`, which translates to "no59 command."60 61 ```go61- var initialModel = model{62- // Our to-do list is just a grocery list63- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},62+var initialModel = model{63+ // Our to-do list is just a grocery list64+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},65 65- // A map which indicates which choices are selected. We're using66- // the map like a mathematical set. The keys refer to the indexes67- // of the `choices` slice, above.68- selected: make(map[int]struct{}),69- }66+ // A map which indicates which choices are selected. We're using67+ // the map like a mathematical set. The keys refer to the indexes68+ // of the `choices` slice, above.69+ selected: make(map[int]struct{}),70+}71 71- func (m model) Init() (tea.Cmd) {72- // Just return `nil`, which means "no I/O right now, please."73- return m, nil74- }72+func (m model) Init() (tea.Cmd) {73+ // Just return `nil`, which means "no I/O right now, please."74+ return m, nil75+}76 ```77 78 ## The Update Method79 15 unmodified lines95 For now, we'll just deal with `tea.KeyMsg` messages, which are automatically96 sent to the update function when keys are pressed.97 98 ```go98- func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {99- switch msg := msg.(type) {99+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {100+ switch msg := msg.(type) {101 101- // Is it a key press?102- case tea.KeyMsg:102+ // Is it a key press?103+ case tea.KeyMsg:104 104- // Cool, what was the actual key pressed?105- switch msg.String() {105+ // Cool, what was the actual key pressed?106+ switch msg.String() {107 107- // These keys should exit the program.108- case "ctrl+c", "q":109- return m, tea.Quit108+ // These keys should exit the program.109+ case "ctrl+c", "q":110+ return m, tea.Quit111 111- // The "up" and "k" keys move the cursor up112- case "up", "k":113- if m.cursor > 0 {114- m.cursor--115- }112+ // The "up" and "k" keys move the cursor up113+ case "up", "k":114+ if m.cursor > 0 {115+ m.cursor--116+ }117 117- // The "down" and "j" keys move the cursor down118- case "down", "j":119- if m.cursor < len(m.choices)-1 {120- m.cursor++121- }118+ // The "down" and "j" keys move the cursor down119+ case "down", "j":120+ if m.cursor < len(m.choices)-1 {121+ m.cursor++122+ }123 123- // The "enter" key and the spacebar (a literal space) toggle124- // the selected state for the item that the cursor is pointing at.125- case "enter", " ":126- _, ok := m.selected[m.cursor]127- if ok {128- delete(m.selected, m.cursor)129- } else {130- m.selected[m.cursor] = struct{}{}131- }124+ // The "enter" key and the spacebar (a literal space) toggle125+ // the selected state for the item that the cursor is pointing at.126+ case "enter", " ":127+ _, ok := m.selected[m.cursor]128+ if ok {129+ delete(m.selected, m.cursor)130+ } else {131+ m.selected[m.cursor] = struct{}{}132 }133 }134-135- // Return the updated model to the Bubble Tea runtime for processing.136- // Note that we're not returning a command.137- return m, nil134 }135+136+ // Return the updated model to the Bubble Tea runtime for processing.137+ // Note that we're not returning a command.138+ return m, nil139+}140 ```141 142 You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command143 with the model. That's a special command which instructs the Bubble Tea runtime9 unmodified lines153 to worry about redraw logic and stuff like that. Bubble Tea takes care of it154 for you.155 156 ```go156- func (m model) View() string {157- // The header158- s := "What should we buy at the market?\n\n"157+func (m model) View() string {158+ // The header159+ s := "What should we buy at the market?\n\n"160 160- // Iterate over our choices161- for i, choice := range m.choices {161+ // Iterate over our choices162+ for i, choice := range m.choices {163 163- // Is the cursor pointing at this choice?164- cursor := " " // no cursor165- if m.cursor == i {166- cursor = ">" // cursor!167- }164+ // Is the cursor pointing at this choice?165+ cursor := " " // no cursor166+ if m.cursor == i {167+ cursor = ">" // cursor!168+ }169 169- // Is this choice selected?170- checked := " " // not selected171- if _, ok := m.selected[i]; ok {172- checked = "x" // selected!173- }174-175- // Render the row176- s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)170+ // Is this choice selected?171+ checked := " " // not selected172+ if _, ok := m.selected[i]; ok {173+ checked = "x" // selected!174 }175 179- // The footer180- s += "\nPress q to quit.\n"181-182- // Send the UI for rendering183- return s176+ // Render the row177+ s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)178 }179+180+ // The footer181+ s += "\nPress q to quit.\n"182+183+ // Send the UI for rendering184+ return s185+}186 ```187 188 ## All Together Now189 190 The last step is to simply run our program. We pass our initial model to191 `tea.NewProgram` and let it rip:192 193 ```go193- func main() {194- p := tea.NewProgram(initialModel)195- if err := p.Start(); err != nil {196- fmt.Printf("Alas, there's been an error: %v", err)197- os.Exit(1)198- }194+func main() {195+ p := tea.NewProgram(initialModel)196+ if err := p.Start(); err != nil {197+ fmt.Printf("Alas, there's been an error: %v", err)198+ os.Exit(1)199 }200+}201 ```202 203 ## What's Next?204 36 unmodified linesmodified:README.md+87 added-87 removed
37 unmodified lines38 39 ## Tutorial40 41 Bubble Tea is based on the functional design paradigms of [The Elm42-Architecture][elm] which happens work nicely with Go.42+Architecture][elm] which happens work nicely with Go. It's a delightful way to43+build applications.44 45 By the way, the non-annotated source code for this program is available46 [on GitHub](https://github.com/charmbracelet/bubbletea/tree/master/tutorials/basics).47 8 unmodified lines56 To start we'll define our package and import some libraries. Our only external57 import will be the Bubble Tea library, which we'll call `tea` for short.58 59 ```go59- package main60+package main61 61- import (62- "fmt"63- "os"62+import (63+ "fmt"64+ "os"65 65- tea "github.com/charmbracelet/bubbletea"66- )66+ tea "github.com/charmbracelet/bubbletea"67+)68 ```69 70 Bubble Tea programs are comprised of a **model** that describes the application71 state and three simple methods on that model:71-72+We73 * **Init**, a function that returns an initial command for the application to run.74 * **Update**, a function that handles incoming events and updates the model accordingly.75 * **View**, a function that renders the UI based on the data in the model.76 2 unmodified lines79 So let's start by defining our model which will store our application's state.80 It can be any type, but a `struct` usually makes the most sense.81 82 ```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- }83+type model struct {84+ choices []string // items on the to-do list85+ cursor int // which to-do list item our cursor is pointing at86+ selected map[int]struct{} // which to-do items are selected87+}88 ```89 90 ## Initialization91 3 unmodified lines95 do any I/O, so for the command we'll just return `nil`, which translates to "no96 command."97 98 ```go98- var initialModel = model{99- // Our to-do list is just a grocery list100- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},99+var initialModel = model{100+ // Our to-do list is just a grocery list101+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},102 102- // A map which indicates which choices are selected. We're using103- // the map like a mathematical set. The keys refer to the indexes104- // of the `choices` slice, above.105- selected: make(map[int]struct{}),106- }103+ // A map which indicates which choices are selected. We're using104+ // the map like a mathematical set. The keys refer to the indexes105+ // of the `choices` slice, above.106+ selected: make(map[int]struct{}),107+}108 108- func (m model) Init() (tea.Cmd) {109- // Just return `nil`, which means "no I/O right now, please."110- return m, nil111- }109+func (m model) Init() (tea.Cmd) {110+ // Just return `nil`, which means "no I/O right now, please."111+ return m, nil112+}113 ```114 115 ## The Update Method116 15 unmodified lines132 For now, we'll just deal with `tea.KeyMsg` messages, which are automatically133 sent to the update function when keys are pressed.134 135 ```go135- func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {136- switch msg := msg.(type) {136+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {137+ switch msg := msg.(type) {138 138- // Is it a key press?139- case tea.KeyMsg:139+ // Is it a key press?140+ case tea.KeyMsg:141 141- // Cool, what was the actual key pressed?142- switch msg.String() {142+ // Cool, what was the actual key pressed?143+ switch msg.String() {144 144- // These keys should exit the program.145- case "ctrl+c", "q":146- return m, tea.Quit145+ // These keys should exit the program.146+ case "ctrl+c", "q":147+ return m, tea.Quit148 148- // The "up" and "k" keys move the cursor up149- case "up", "k":150- if m.cursor > 0 {151- m.cursor--152- }149+ // The "up" and "k" keys move the cursor up150+ case "up", "k":151+ if m.cursor > 0 {152+ m.cursor--153+ }154 154- // The "down" and "j" keys move the cursor down155- case "down", "j":156- if m.cursor < len(m.choices)-1 {157- m.cursor++158- }155+ // The "down" and "j" keys move the cursor down156+ case "down", "j":157+ if m.cursor < len(m.choices)-1 {158+ m.cursor++159+ }160 160- // The "enter" key and the spacebar (a literal space) toggle161- // the selected state for the item that the cursor is pointing at.162- case "enter", " ":163- _, ok := m.selected[m.cursor]164- if ok {165- delete(m.selected, m.cursor)166- } else {167- m.selected[m.cursor] = struct{}{}168- }161+ // The "enter" key and the spacebar (a literal space) toggle162+ // the selected state for the item that the cursor is pointing at.163+ case "enter", " ":164+ _, ok := m.selected[m.cursor]165+ if ok {166+ delete(m.selected, m.cursor)167+ } else {168+ m.selected[m.cursor] = struct{}{}169 }170 }171-172- // Return the updated model to the Bubble Tea runtime for processing.173- // Note that we're not returning a command.174- return m, nil171 }172+173+ // Return the updated model to the Bubble Tea runtime for processing.174+ // Note that we're not returning a command.175+ return m, nil176+}177 ```178 179 You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command180 with the model. That's a special command which instructs the Bubble Tea runtime9 unmodified lines190 to worry about redraw logic and stuff like that. Bubble Tea takes care of it191 for you.192 193 ```go193- func (m model) View() string {194- // The header195- s := "What should we buy at the market?\n\n"194+func (m model) View() string {195+ // The header196+ s := "What should we buy at the market?\n\n"197 197- // Iterate over our choices198- for i, choice := range m.choices {198+ // Iterate over our choices199+ for i, choice := range m.choices {200 200- // Is the cursor pointing at this choice?201- cursor := " " // no cursor202- if m.cursor == i {203- cursor = ">" // cursor!204- }201+ // Is the cursor pointing at this choice?202+ cursor := " " // no cursor203+ if m.cursor == i {204+ cursor = ">" // cursor!205+ }206 206- // Is this choice selected?207- checked := " " // not selected208- if _, ok := m.selected[i]; ok {209- checked = "x" // selected!210- }211-212- // Render the row213- s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)207+ // Is this choice selected?208+ checked := " " // not selected209+ if _, ok := m.selected[i]; ok {210+ checked = "x" // selected!211 }212 216- // The footer217- s += "\nPress q to quit.\n"218-219- // Send the UI for rendering220- return s213+ // Render the row214+ s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)215 }216+217+ // The footer218+ s += "\nPress q to quit.\n"219+220+ // Send the UI for rendering221+ return s222+}223 ```224 225 ## All Together Now226 227 The last step is to simply run our program. We pass our initial model to228 `tea.NewProgram` and let it rip:229 230 ```go230- func main() {231- p := tea.NewProgram(initialModel)232- if err := p.Start(); err != nil {233- fmt.Printf("Alas, there's been an error: %v", err)234- os.Exit(1)235- }231+func main() {232+ p := tea.NewProgram(initialModel)233+ if err := p.Start(); err != nil {234+ fmt.Printf("Alas, there's been an error: %v", err)235+ os.Exit(1)236 }237+}238 ```238-239 ## What's Next?240 241 This tutorial covers the basics of building an interactive terminal UI, but242 in the real world you'll also need to perform I/O. To learn about that have a89 unmodified lines