Tidy up tutorials
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 ad19c66 · Browse files · 4 files·+108 added−110 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+108 added−110 removed
- other:+108 added−110 removed
Provenance
- Commit
9d0e1db- Tree
c9b3346- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
ad19c66
4 files changed+108 added−110 removed
modified:tutorials/basics/main.go+12 added-6 removed
11 unmodified lines12 choices []string13 selected map[int]struct{}14 }15 16+func initialModel() model {17+ return model{18+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},19+20+ // A map which indicates which choices are selected. We're using21+ // the map like a mathematical set. The keys refer to the indexes22+ // of the `choices` slice, above.23+ selected: make(map[int]struct{}),24+ }25+}26+27 func (m model) Init() tea.Cmd {28 return nil29 }30 46 unmodified lines77 return s78 }79 80 func main() {70- initialModel := model{71- choices: []string{"Carrots", "Celery", "Kohlrabi"},72- selected: make(map[int]struct{}),73- }74-75- p := tea.NewProgram(initialModel)81+ p := tea.NewProgram(initialModel())82 if err := p.Start(); err != nil {83 fmt.Printf("Alas, there's been an error: %v", err)84 os.Exit(1)85 }1 unmodified linemodified:tutorials/basics/README.md+43 added-48 removed
12 unmodified lines13 [elm]: https://guide.elm-lang.org/architecture/14 15 ## Enough! Let's get to it.16 17-For this tutorial we're making a to-do list.17+For this tutorial we're making a shopping list.18 19 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 29 unmodified lines51 ```52 53 ## Initialization54 55-Next we'll define our application’s initial state. We’ll store our initial56-model in a simple variable, and then define the `Init` method. `Init` can57-return a `Cmd` that could perform some initial I/O. For now, we don't need to58-do any I/O, so for the command we'll just return `nil`, which translates to "no59-command."55+Next we’ll define our application’s initial state. In this case we’re defining56+a function to return our initial model, however we could just as easily define57+the initial model as a variable elsewhere, too.58 59 ```go62-func main() {63- initialModel := model{64- // Our to-do list is just a grocery list65- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},60+func initialModel() model {61+ return model{62+ // Our shopping list is a grocery list63+ choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},64 67- // A map which indicates which choices are selected. We're using68- // the map like a mathematical set. The keys refer to the indexes69- // of the `choices` slice, above.70- selected: make(map[int]struct{}),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 }70 }71+```72 73+Next we define the `Init` method. `Init` can return a `Cmd` that could perform74+some initial I/O. For now, we don't need to do any I/O, so for the command75+we'll just return `nil`, which translates to "no command."76+77+```go78 func (m model) Init() tea.Cmd {79 // Just return `nil`, which means "no I/O right now, please."80 return nil81 }82 ```83 84 ## The Update Method85 82-Next we'll define the update method. The update function is called when83-"things happen." Its job is to look at what has happened and return an updated84-model in response to whatever happened. It can also return a `Cmd` and make85-more things happen, but for now don't worry about that part.86+Next up is the update method. The update function is called when ”things87+happen.” Its job is to look at what has happened and return an updated model in88+response. It can also return a `Cmd` to make more things happen, but for now89+don't worry about that part.90 87-In our case, when a user presses the down arrow, `update`'s job is to notice91+In our case, when a user presses the down arrow, `Update`’s job is to notice92 that the down arrow was pressed and move the cursor accordingly (or not).93 90-The "something happened" comes in the form of a `Msg`, which can be any type.94+The “something happened” comes in the form of a `Msg`, which can be any type.95 Messages are the result of some I/O that took place, such as a keypress, timer96 tick, or a response from a server.97 98 We usually figure out which type of `Msg` we received with a type switch, but45 unmodified lines144 return m, nil145 }146 ```147 144-You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command145-with the model. That's a special command which instructs the Bubble Tea runtime146-to quit, exiting the program.148+You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return149+a `tea.Quit` command with the model. That’s a special command which instructs150+the Bubble Tea runtime to quit, exiting the program.151 152 ## The View Method153 150-At last, it's time to render our UI. Of all the methods, the view is the154+At last, it’s time to render our UI. Of all the methods, the view is the155 simplest. We look at the model in it's current state and use it to return156 a `string`. That string is our UI!157 154-Because the view describes the entire UI of your application, you don't have155-to worry about redraw logic and stuff like that. Bubble Tea takes care of it158+Because the view describes the entire UI of your application, you don’t have to159+worry about redrawing logic and stuff like that. Bubble Tea takes care of it160 for you.161 162 ```go163 func (m model) View() string {33 unmodified lines197 `tea.NewProgram` and let it rip:198 199 ```go200 func main() {197- initialModel := model{198- choices: []string{"Carrots", "Celery", "Kohlrabi"},199- selected: make(map[int]struct{}),200- }201-202- p := tea.NewProgram(initialModel)201+ p := tea.NewProgram(initialModel())202 if err := p.Start(); err != nil {203 fmt.Printf("Alas, there's been an error: %v", err)204 os.Exit(1)205 }206 }207 ```208 210-## What's Next?209+## What’s Next?210 211 This tutorial covers the basics of building an interactive terminal UI, but212 in the real world you'll also need to perform I/O. To learn about that have a213 look at the [Command Tutorial][cmd]. It's pretty simple.4 unmodified lines218 [cmd]: http://github.com/charmbracelet/bubbletea/tree/master/tutorials/commands/219 [examples]: http://github.com/charmbracelet/bubbletea/tree/master/examples220 [docs]: https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc221 223-### Bubble Tea in the Wild222+## Additional Resources223 225-For some Bubble Tea programs in production, see:224+* [Libraries we use with Bubble Tea](https://github.com/charmbracelet/bubbletea/#libraries-we-use-with-bubble-tea)225+* [Bubble Tea in the Wild](https://github.com/charmbracelet/bubbletea/#bubble-tea-in-the-wild)226 227-* [Glow](https://github.com/charmbracelet/glow): a markdown reader, browser and online markdown stash228-* [The Charm Tool](https://github.com/charmbracelet/charm): the Charm user account manager229-230-### Libraries we use with Bubble Tea231-232-* [Bubbles][bubbles]: various Bubble Tea components233-* [Termenv][termenv]: Advanced ANSI styling for terminal applications234-* [Reflow][reflow]: ANSI-aware methods for formatting and generally working with text. Of particular note is `PrintableRuneWidth` in the `ansi` sub-package which measures the physical widths of strings. Many runes, such as East Asian characters, emojis, and various unicode symbols are two cells wide, so measuring a layout with `len()` often won't cut it. Reflow is particularly nice for this as it measures character widths while ignoring any ANSI sequences present.235-236-[termenv]: https://github.com/muesli/termenv237-[reflow]: https://github.com/muesli/reflow238-[bubbles]: https://github.com/charmbracelet/bubbles239-227 ### Feedback228 229 We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!230 231 * [Twitter](https://twitter.com/charmcli)232 * [The Fediverse](https://mastodon.technology/@charm)233+234+***235+236+Part of [Charm](https://charm.sh).237+238+<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>239+240+Charm热爱开源 • Charm loves open sourcemodified:tutorials/commands/README.md+26 added-26 removed
181 unmodified lines182 ## One More Thing About Commands183 184 `Cmd`s are defined in Bubble Tea as `type Cmd func() Msg`. So they're just185 functions that don't take any arguments and return a `Msg`, which can be186-anything. If you need to pass arguments to a command, you just make a function186+any type. If you need to pass arguments to a command, you just make a function187 that returns a command. For example:188 189 ```go190+func cmdWithArg(id int) tea.Cmd {191+ return func() tea.Msg {192+ return someMsg{id: int}193+ }194+}195+```196+197+A more real-world example looks like:198+199+```go200 func checkSomeUrl(url string) tea.Cmd {201 return func() tea.Msg {202 c := &http.Client{Timeout: 10 * time.Second}203 res, err := c.Get(url)4 unmodified lines208 }209 }210 ```211 202-Just make sure you do as much stuff as you can in the innermost function,203-because that's the one that runs asynchronously.212+Anyway, just make sure you do as much stuff as you can in the innermost213+function, because that's the one that runs asynchronously.214 205-## Anyway, Now What?215+## Now What?216 217 After doing this tutorial and [the previous one][basics] you should be ready to208-build a Bubble Tea program of your own. We also recommend that you look at the218+build a Bubble Tea program of your own. We also recommend that you look at the219 Bubble Tea [example programs][examples] as well as [Bubbles][bubbles],220 a component library for Bubble Tea.221 222 And, of course, check out the [Go Docs][docs].223 214-### Bubble Tea in the Wild224+## Additional Resources225 216-For some Bubble Tea programs in production, see:226+* [Libraries we use with Bubble Tea](https://github.com/charmbracelet/bubbletea/#libraries-we-use-with-bubble-tea)227+* [Bubble Tea in the Wild](https://github.com/charmbracelet/bubbletea/#bubble-tea-in-the-wild)228 218-* [Glow](https://github.com/charmbracelet/glow): a markdown reader, browser and online markdown stash219-* [The Charm Tool](https://github.com/charmbracelet/charm): the Charm user account manager220-221-[examples]: http://github.com/charmbracelet/bubbletea/tree/master/examples222-[docs]: https://pkg.go.dev/github.com/charmbracelet/bubbletea?tab=doc223-[bubbles]: https://github.com/charmbracelet/bubbles224-225-### Libraries we use with Bubble Tea226-227-* [Bubbles][bubbles] various Bubble Tea components we've built228-* [Termenv][termenv]: Advanced ANSI styling for terminal applications229-* [Reflow][reflow]: ANSI-aware methods for reflowing blocks of text230-* [go-runewidth][runewidth]: Get the physical width of strings in terms of terminal cells. Many runes, such as East Asian charcters and emojis, are two cells wide, so measuring a layout with `len()` often won't cut it!231-232-[termenv]: https://github.com/muesli/termenv233-[reflow]: https://github.com/muesli/reflow234-[bubbles]: https://github.com/charmbracelet/bubbles235-[runewidth]: https://github.com/mattn/go-runewidth236-229 ### Feedback230 231 We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!232 233 * [Twitter](https://twitter.com/charmcli)234 * [The Fediverse](https://mastodon.technology/@charm)235+236+***237+238+Part of [Charm](https://charm.sh).239+240+<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>241+242+Charm热爱开源 • Charm loves open sourcemodified:README.md+27 added-30 removed
48 unmodified lines49 [elm]: https://guide.elm-lang.org/architecture/50 51 ## Enough! Let's get to it.52 53-For this tutorial we're making a to-do list.53+For this tutorial we're making a shopping list.54 55 To start we'll define our package and import some libraries. Our only external56 import will be the Bubble Tea library, which we'll call `tea` for short.57 29 unmodified lines87 ```88 89 ## Initialization90 91-Next we'll define our application’s initial state. We’ll store our initial92-model in a simple variable, and then define the `Init` method. `Init` can93-return a `Cmd` that could perform some initial I/O. For now, we don't need to94-do any I/O, so for the command we'll just return `nil`, which translates to "no95-command."91+Next we’ll define our application’s initial state. In this case we’re defining92+a function to return our initial model, however we could just as easily define93+the initial model as a variable elsewhere, too.94 95 ```go98-func main() {99- initialModel := model{100- // Our to-do list is just a grocery list96+func initialModel() model {97+ return model{98+ // Our shopping list is a grocery list99 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},100 101 // A map which indicates which choices are selected. We're using102 // the map like a mathematical set. The keys refer to the indexes103 // of the `choices` slice, above.104 selected: make(map[int]struct{}),105 }106 }107+```108 109+Next we define the `Init` method. `Init` can return a `Cmd` that could perform110+some initial I/O. For now, we don't need to do any I/O, so for the command111+we'll just return `nil`, which translates to "no command."112+113+```go114 func (m model) Init() tea.Cmd {115 // Just return `nil`, which means "no I/O right now, please."116 return nil117 }118 ```119 120 ## The Update Method121 118-Next we'll define the update method. The update function is called when119-"things happen." Its job is to look at what has happened and return an updated120-model in response to whatever happened. It can also return a `Cmd` and make121-more things happen, but for now don't worry about that part.122+Next up is the update method. The update function is called when ”things123+happen.” Its job is to look at what has happened and return an updated model in124+response. It can also return a `Cmd` to make more things happen, but for now125+don't worry about that part.126 123-In our case, when a user presses the down arrow, `update`'s job is to notice127+In our case, when a user presses the down arrow, `Update`’s job is to notice128 that the down arrow was pressed and move the cursor accordingly (or not).129 126-The "something happened" comes in the form of a `Msg`, which can be any type.130+The “something happened” comes in the form of a `Msg`, which can be any type.131 Messages are the result of some I/O that took place, such as a keypress, timer132 tick, or a response from a server.133 134 We usually figure out which type of `Msg` we received with a type switch, but45 unmodified lines180 return m, nil181 }182 ```183 180-You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command181-with the model. That's a special command which instructs the Bubble Tea runtime182-to quit, exiting the program.184+You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return185+a `tea.Quit` command with the model. That’s a special command which instructs186+the Bubble Tea runtime to quit, exiting the program.187 188 ## The View Method189 186-At last, it's time to render our UI. Of all the methods, the view is the190+At last, it’s time to render our UI. Of all the methods, the view is the191 simplest. We look at the model in it's current state and use it to return192 a `string`. That string is our UI!193 190-Because the view describes the entire UI of your application, you don't have191-to worry about redraw logic and stuff like that. Bubble Tea takes care of it194+Because the view describes the entire UI of your application, you don’t have to195+worry about redrawing logic and stuff like that. Bubble Tea takes care of it196 for you.197 198 ```go199 func (m model) View() string {33 unmodified lines233 `tea.NewProgram` and let it rip:234 235 ```go236 func main() {233- initialModel := model{234- choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},235- selected: make(map[int]struct{}),236- }237-238- p := tea.NewProgram(initialModel)237+ p := tea.NewProgram(initialModel())238 if err := p.Start(); err != nil {239 fmt.Printf("Alas, there's been an error: %v", err)240 os.Exit(1)241 }242 }243 ```244 246-## What's Next?245+## What’s Next?246 247 This tutorial covers the basics of building an interactive terminal UI, but248 in the real world you'll also need to perform I/O. To learn about that have a249 look at the [Command Tutorial][cmd]. It's pretty simple.56 unmodified lines306 307 [elm]: https://guide.elm-lang.org/architecture/308 [gotea]: https://github.com/tj/go-tea309 311-310 ## License311 312 [MIT](https://github.com/charmbracelet/bubbletea/raw/master/LICENSE)313 316-314 ***315 316 Part of [Charm](https://charm.sh).317 3 unmodified lines