Skip to content
Myr

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

1 file+86 added−86 removed

modified:README.md+86 added-86 removed
39 unmodified lines
40We'll start by defining our package and import some libraries. Our only external41import will be the Bubble Tea library, which we'll call `tea` for short.4243```go44 package main44package main4546 import (47 "fmt"48 "os"46import (47 "fmt"48 "os"4950 tea "github.com/charmbracelet/bubbletea"51 )50 tea "github.com/charmbracelet/bubbletea"51)52```5354Bubble Tea programs are comprised of a **model** that describes the application55state and three simple functions that are centered around that model:
7 unmodified lines
63So let's start by defining our model which will store our application's state.64It can be any type, but a `struct` usually makes the most sense.6566```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 }67type 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```7374### The Initialization Function75
3 unmodified lines
79don't need to do any I/O, so for the command we'll just return `nil`, which80translates to "no command."8182```go83 func initialize() (tea.Model, tea.Cmd) {84 m := model{83func initialize() (tea.Model, tea.Cmd) {84 m := model{8586 // 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"},8889 // 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 }9495 // 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 }9495 // Return the model and `nil`, which means "no I/O right now, please."96 return m, nil97}98```99100### The Update Function101
15 unmodified lines
117For now, we'll just deal with `tea.KeyMsg` messages, which are automatically118sent to the update function when keys are pressed.119120```go121 func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {122 m, _ := mdl.(model)121func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {122 m, _ := mdl.(model)123124 switch msg := msg.(type) {124 switch msg := msg.(type) {125126 // Is it a key press?127 case tea.KeyMsg:126 // Is it a key press?127 case tea.KeyMsg:128129 // Cool, what was the actual key pressed?130 switch msg.String() {129 // Cool, what was the actual key pressed?130 switch msg.String() {131132 // 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.Quit135136 // 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 }141142 // 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 }147148 // 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 }159160 // Return the updated model to the Bubble Tea runtime for processing.161 // Note that we're not returning a command.162 return m, nil158 }159160 // 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```165166You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command167with the model. That's a special command which instructs the Bubble Tea runtime
9 unmodified lines
177to worry about redraw logic and stuff like that. Bubble Tea takes care of it178for you.179180```go181 func view(mdl tea.Model) string {182 m, _ := mdl.(model)181func view(mdl tea.Model) string {182 m, _ := mdl.(model)183184 // 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"186187 // Iterate over our choices188 for i, choice := range m.choices {187 // Iterate over our choices188 for i, choice := range m.choices {189190 // 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 }195196 // Is this choice selected?197 checked := " " // not selected198 if _, ok := m.selected[i]; ok {199 checked = "x" // selected!200 }201202 // 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 }201206 // The footer207 s += "\nPress q to quit.\n"208209 // Send the UI for rendering210 return s202 // Render the row203 s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)204 }205206 // The footer207 s += "\nPress q to quit.\n"208209 // Send the UI for rendering210 return s211}212```213214### All Together Now215216The last step is to simply run our program. We pass our functions to217`tea.NewProgram` and let it rip:218219```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 }220func 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```228229### What's Next?230
54 unmodified lines