forked from Mirrors/bubbletea
De-indent code blocks in the tutorial
This commit is contained in:
parent
118f2da75c
commit
a2a85d3c73
26
README.md
26
README.md
|
@ -41,14 +41,14 @@ We'll start by defining our package and import some libraries. Our only external
|
|||
import will be the Bubble Tea library, which we'll call `tea` for short.
|
||||
|
||||
```go
|
||||
package main
|
||||
package main
|
||||
|
||||
import (
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
Bubble Tea programs are comprised of a **model** that describes the application
|
||||
|
@ -64,11 +64,11 @@ So let's start by defining our model which will store our application's state.
|
|||
It can be any type, but a `struct` usually makes the most sense.
|
||||
|
||||
```go
|
||||
type model struct {
|
||||
type model struct {
|
||||
choices []string // items on the to-do list
|
||||
cursor int // which to-do list item our cursor is pointing at
|
||||
selected map[int]struct{} // which to-do items are selected
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### The Initialization Function
|
||||
|
@ -80,7 +80,7 @@ don't need to do any I/O, so for the command we'll just return `nil`, which
|
|||
translates to "no command."
|
||||
|
||||
```go
|
||||
func initialize() (tea.Model, tea.Cmd) {
|
||||
func initialize() (tea.Model, tea.Cmd) {
|
||||
m := model{
|
||||
|
||||
// Our to-do list is just a grocery list
|
||||
|
@ -94,7 +94,7 @@ translates to "no command."
|
|||
|
||||
// Return the model and `nil`, which means "no I/O right now, please."
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### The Update Function
|
||||
|
@ -118,7 +118,7 @@ For now, we'll just deal with `tea.KeyMsg` messages, which are automatically
|
|||
sent to the update function when keys are pressed.
|
||||
|
||||
```go
|
||||
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
||||
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
||||
m, _ := mdl.(model)
|
||||
|
||||
switch msg := msg.(type) {
|
||||
|
@ -160,7 +160,7 @@ sent to the update function when keys are pressed.
|
|||
// Return the updated model to the Bubble Tea runtime for processing.
|
||||
// Note that we're not returning a command.
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command
|
||||
|
@ -178,7 +178,7 @@ to worry about redraw logic and stuff like that. Bubble Tea takes care of it
|
|||
for you.
|
||||
|
||||
```go
|
||||
func view(mdl tea.Model) string {
|
||||
func view(mdl tea.Model) string {
|
||||
m, _ := mdl.(model)
|
||||
|
||||
// The header
|
||||
|
@ -208,7 +208,7 @@ for you.
|
|||
|
||||
// Send the UI for rendering
|
||||
return s
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### All Together Now
|
||||
|
@ -217,13 +217,13 @@ The last step is to simply run our program. We pass our functions to
|
|||
`tea.NewProgram` and let it rip:
|
||||
|
||||
```go
|
||||
func main() {
|
||||
func main() {
|
||||
p := tea.NewProgram(initialize, update, view)
|
||||
if err := p.Start(); err != nil {
|
||||
fmt.Printf("Alas, there's been an error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### What's Next?
|
||||
|
|
Loading…
Reference in New Issue