De-indent code blocks in the 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 118f2da · Browse files · 1 file·+86 added−86 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+86 added−86 removed
- other:+86 added−86 removed
Provenance
- Commit
a2a85d3- Tree
63f281a- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
118f2da
1 file changed+86 added−86 removed
modified:README.md+86 added-86 removed
39 unmodified lines40 We'll start by defining our package and import some libraries. Our only external41 import will be the Bubble Tea library, which we'll call `tea` for short.42 43 ```go44- package main44+package main45 46- import (47- "fmt"48- "os"46+import (47+ "fmt"48+ "os"49 50- tea "github.com/charmbracelet/bubbletea"51- )50+ tea "github.com/charmbracelet/bubbletea"51+)52 ```53 54 Bubble Tea programs are comprised of a **model** that describes the application55 state and three simple functions that are centered around that model:7 unmodified lines63 So let's start by defining our model which will store our application's state.64 It can be any type, but a `struct` usually makes the most sense.65 66 ```go67- type model struct {68- choices []string // items on the to-do list69- cursor int // which to-do list item our cursor is pointing at70- selected map[int]struct{} // which to-do items are selected71- }67+type model struct {68+ choices []string // items on the to-do list69+ cursor int // which to-do list item our cursor is pointing at70+ selected map[int]struct{} // which to-do items are selected71+}72 ```73 74 ### The Initialization Function75 3 unmodified lines79 don't need to do any I/O, so for the command we'll just return `nil`, which80 translates to "no command."81 82 ```go83- func initialize() (tea.Model, tea.Cmd) {84- m := model{83+func initialize() (tea.Model, tea.Cmd) {84+ m := model{85 86- // Our to-do list is just a grocery list87- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},86+ // Our to-do list is just a grocery list87+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},88 89- // A map which indicates which choices are selected. We're using90- // the map like a mathematical set. The keys refer to the indexes91- // of the `choices` slice, above.92- selected: make(map[int]struct{}),93- }94-95- // Return the model and `nil`, which means "no I/O right now, please."96- return m, nil89+ // A map which indicates which choices are selected. We're using90+ // the map like a mathematical set. The keys refer to the indexes91+ // of the `choices` slice, above.92+ selected: make(map[int]struct{}),93 }94+95+ // Return the model and `nil`, which means "no I/O right now, please."96+ return m, nil97+}98 ```99 100 ### The Update Function101 15 unmodified lines117 For now, we'll just deal with `tea.KeyMsg` messages, which are automatically118 sent to the update function when keys are pressed.119 120 ```go121- func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {122- m, _ := mdl.(model)121+func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {122+ m, _ := mdl.(model)123 124- switch msg := msg.(type) {124+ switch msg := msg.(type) {125 126- // Is it a key press?127- case tea.KeyMsg:126+ // Is it a key press?127+ case tea.KeyMsg:128 129- // Cool, what was the actual key pressed?130- switch msg.String() {129+ // Cool, what was the actual key pressed?130+ switch msg.String() {131 132- // These keys should exit the program.133- case "ctrl+c", "q":134- return m, tea.Quit132+ // These keys should exit the program.133+ case "ctrl+c", "q":134+ return m, tea.Quit135 136- // The "up" and "k" keys move the cursor up137- case "up", "k":138- if m.cursor > 0 {139- m.cursor--140- }136+ // The "up" and "k" keys move the cursor up137+ case "up", "k":138+ if m.cursor > 0 {139+ m.cursor--140+ }141 142- // The "down" and "j" keys move the cursor down143- case "down", "j":144- if m.cursor < len(m.choices)-1 {145- m.cursor++146- }142+ // The "down" and "j" keys move the cursor down143+ case "down", "j":144+ if m.cursor < len(m.choices)-1 {145+ m.cursor++146+ }147 148- // The "enter" key and the spacebar (a literal space) toggle149- // the selected state for the item that the cursor is pointing at.150- case "enter", " ":151- _, ok := m.selected[m.cursor]152- if ok {153- delete(m.selected, m.cursor)154- } else {155- m.selected[m.cursor] = struct{}{}156- }148+ // The "enter" key and the spacebar (a literal space) toggle149+ // the selected state for the item that the cursor is pointing at.150+ case "enter", " ":151+ _, ok := m.selected[m.cursor]152+ if ok {153+ delete(m.selected, m.cursor)154+ } else {155+ m.selected[m.cursor] = struct{}{}156 }157 }159-160- // Return the updated model to the Bubble Tea runtime for processing.161- // Note that we're not returning a command.162- return m, nil158 }159+160+ // Return the updated model to the Bubble Tea runtime for processing.161+ // Note that we're not returning a command.162+ return m, nil163+}164 ```165 166 You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command167 with the model. That's a special command which instructs the Bubble Tea runtime9 unmodified lines177 to worry about redraw logic and stuff like that. Bubble Tea takes care of it178 for you.179 180 ```go181- func view(mdl tea.Model) string {182- m, _ := mdl.(model)181+func view(mdl tea.Model) string {182+ m, _ := mdl.(model)183 184- // The header185- s := "What should we buy at the market?\n\n"184+ // The header185+ s := "What should we buy at the market?\n\n"186 187- // Iterate over our choices188- for i, choice := range m.choices {187+ // Iterate over our choices188+ for i, choice := range m.choices {189 190- // Is the cursor pointing at this choice?191- cursor := " " // no cursor192- if m.cursor == i {193- cursor = ">" // cursor!194- }190+ // Is the cursor pointing at this choice?191+ cursor := " " // no cursor192+ if m.cursor == i {193+ cursor = ">" // cursor!194+ }195 196- // Is this choice selected?197- checked := " " // not selected198- if _, ok := m.selected[i]; ok {199- checked = "x" // selected!200- }201-202- // Render the row203- s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)196+ // Is this choice selected?197+ checked := " " // not selected198+ if _, ok := m.selected[i]; ok {199+ checked = "x" // selected!200 }201 206- // The footer207- s += "\nPress q to quit.\n"208-209- // Send the UI for rendering210- return s202+ // Render the row203+ s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)204 }205+206+ // The footer207+ s += "\nPress q to quit.\n"208+209+ // Send the UI for rendering210+ return s211+}212 ```213 214 ### All Together Now215 216 The last step is to simply run our program. We pass our functions to217 `tea.NewProgram` and let it rip:218 219 ```go220- func main() {221- p := tea.NewProgram(initialize, update, view)222- if err := p.Start(); err != nil {223- fmt.Printf("Alas, there's been an error: %v", err)224- os.Exit(1)225- }220+func main() {221+ p := tea.NewProgram(initialize, update, view)222+ if err := p.Start(); err != nil {223+ fmt.Printf("Alas, there's been an error: %v", err)224+ os.Exit(1)225 }226+}227 ```228 229 ### What's Next?230 54 unmodified lines