Skip to content
Myr

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

2 files+174 added−173 removed

modified:tutorials/basics/README.md+87 added-86 removed
1Bubble Tea Basics2=================34Bubble Tea is based on the functional design paradigms of [The Elm5Architecture][elm] which happens work nicely with Go.5Architecture][elm] which happens work nicely with Go. It's a delightful way to6build applications.78By 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 lines
19To 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.2122```go22 package main23package main2424 import (25 "fmt"26 "os"25import (26 "fmt"27 "os"2828 tea "github.com/charmbracelet/bubbletea"29 )29 tea "github.com/charmbracelet/bubbletea"30)31```3233Bubble Tea programs are comprised of a **model** that describes the application34state and three simple methods on that model:3435We36* **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 lines
42So let's start by defining our model which will store our application's state.43It can be any type, but a `struct` usually makes the most sense.4445```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 }46type 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```5253## Initialization54
3 unmodified lines
58do any I/O, so for the command we'll just return `nil`, which translates to "no59command."6061```go61 var initialModel = model{62 // Our to-do list is just a grocery list63 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},62var initialModel = model{63 // Our to-do list is just a grocery list64 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},6565 // 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}7171 func (m model) Init() (tea.Cmd) {72 // Just return `nil`, which means "no I/O right now, please."73 return m, nil74 }72func (m model) Init() (tea.Cmd) {73 // Just return `nil`, which means "no I/O right now, please."74 return m, nil75}76```7778## The Update Method79
15 unmodified lines
95For now, we'll just deal with `tea.KeyMsg` messages, which are automatically96sent to the update function when keys are pressed.9798```go98 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {99 switch msg := msg.(type) {99func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {100 switch msg := msg.(type) {101101 // Is it a key press?102 case tea.KeyMsg:102 // Is it a key press?103 case tea.KeyMsg:104104 // Cool, what was the actual key pressed?105 switch msg.String() {105 // Cool, what was the actual key pressed?106 switch msg.String() {107107 // 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.Quit111111 // 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 }117117 // 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 }123123 // 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 }134135 // Return the updated model to the Bubble Tea runtime for processing.136 // Note that we're not returning a command.137 return m, nil134 }135136 // 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```141142You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command143with the model. That's a special command which instructs the Bubble Tea runtime
9 unmodified lines
153to worry about redraw logic and stuff like that. Bubble Tea takes care of it154for you.155156```go156 func (m model) View() string {157 // The header158 s := "What should we buy at the market?\n\n"157func (m model) View() string {158 // The header159 s := "What should we buy at the market?\n\n"160160 // Iterate over our choices161 for i, choice := range m.choices {161 // Iterate over our choices162 for i, choice := range m.choices {163163 // 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 }169169 // Is this choice selected?170 checked := " " // not selected171 if _, ok := m.selected[i]; ok {172 checked = "x" // selected!173 }174175 // 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 }175179 // The footer180 s += "\nPress q to quit.\n"181182 // Send the UI for rendering183 return s176 // Render the row177 s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)178 }179180 // The footer181 s += "\nPress q to quit.\n"182183 // Send the UI for rendering184 return s185}186```187188## All Together Now189190The last step is to simply run our program. We pass our initial model to191`tea.NewProgram` and let it rip:192193```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 }194func 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```202203## What's Next?204
36 unmodified lines
modified:README.md+87 added-87 removed
37 unmodified lines
3839## Tutorial4041Bubble Tea is based on the functional design paradigms of [The Elm42Architecture][elm] which happens work nicely with Go.42Architecture][elm] which happens work nicely with Go. It's a delightful way to43build applications.4445By 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 lines
56To start we'll define our package and import some libraries. Our only external57import will be the Bubble Tea library, which we'll call `tea` for short.5859```go59 package main60package main6161 import (62 "fmt"63 "os"62import (63 "fmt"64 "os"6565 tea "github.com/charmbracelet/bubbletea"66 )66 tea "github.com/charmbracelet/bubbletea"67)68```6970Bubble Tea programs are comprised of a **model** that describes the application71state and three simple methods on that model:7172We73* **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 lines
79So let's start by defining our model which will store our application's state.80It can be any type, but a `struct` usually makes the most sense.8182```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 }83type 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```8990## Initialization91
3 unmodified lines
95do any I/O, so for the command we'll just return `nil`, which translates to "no96command."9798```go98 var initialModel = model{99 // Our to-do list is just a grocery list100 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},99var initialModel = model{100 // Our to-do list is just a grocery list101 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},102102 // 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}108108 func (m model) Init() (tea.Cmd) {109 // Just return `nil`, which means "no I/O right now, please."110 return m, nil111 }109func (m model) Init() (tea.Cmd) {110 // Just return `nil`, which means "no I/O right now, please."111 return m, nil112}113```114115## The Update Method116
15 unmodified lines
132For now, we'll just deal with `tea.KeyMsg` messages, which are automatically133sent to the update function when keys are pressed.134135```go135 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {136 switch msg := msg.(type) {136func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {137 switch msg := msg.(type) {138138 // Is it a key press?139 case tea.KeyMsg:139 // Is it a key press?140 case tea.KeyMsg:141141 // Cool, what was the actual key pressed?142 switch msg.String() {142 // Cool, what was the actual key pressed?143 switch msg.String() {144144 // 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.Quit148148 // 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 }154154 // 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 }160160 // 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 }171172 // Return the updated model to the Bubble Tea runtime for processing.173 // Note that we're not returning a command.174 return m, nil171 }172173 // 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```178179You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command180with the model. That's a special command which instructs the Bubble Tea runtime
9 unmodified lines
190to worry about redraw logic and stuff like that. Bubble Tea takes care of it191for you.192193```go193 func (m model) View() string {194 // The header195 s := "What should we buy at the market?\n\n"194func (m model) View() string {195 // The header196 s := "What should we buy at the market?\n\n"197197 // Iterate over our choices198 for i, choice := range m.choices {198 // Iterate over our choices199 for i, choice := range m.choices {200200 // 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 }206206 // Is this choice selected?207 checked := " " // not selected208 if _, ok := m.selected[i]; ok {209 checked = "x" // selected!210 }211212 // 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 }212216 // The footer217 s += "\nPress q to quit.\n"218219 // Send the UI for rendering220 return s213 // Render the row214 s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)215 }216217 // The footer218 s += "\nPress q to quit.\n"219220 // Send the UI for rendering221 return s222}223```224225## All Together Now226227The last step is to simply run our program. We pass our initial model to228`tea.NewProgram` and let it rip:229230```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 }231func 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```238239## What's Next?240241This tutorial covers the basics of building an interactive terminal UI, but242in the real world you'll also need to perform I/O. To learn about that have a
89 unmodified lines