Skip to content
Myr

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

4 files+108 added−110 removed

modified:tutorials/basics/main.go+12 added-6 removed
11 unmodified lines
12 choices []string13 selected map[int]struct{}14}1516func initialModel() model {17 return model{18 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},1920 // 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}2627func (m model) Init() tea.Cmd {28 return nil29}30
46 unmodified lines
77 return s78}7980func main() {70 initialModel := model{71 choices: []string{"Carrots", "Celery", "Kohlrabi"},72 selected: make(map[int]struct{}),73 }7475 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 line
modified:tutorials/basics/README.md+43 added-48 removed
12 unmodified lines
13[elm]: https://guide.elm-lang.org/architecture/1415## Enough! Let's get to it.1617For this tutorial we're making a to-do list.17For this tutorial we're making a shopping list.1819To 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.21
29 unmodified lines
51```5253## Initialization5455Next we'll define our application’s initial state. We’ll store our initial56model in a simple variable, and then define the `Init` method. `Init` can57return a `Cmd` that could perform some initial I/O. For now, we don't need to58do any I/O, so for the command we'll just return `nil`, which translates to "no59command."55Next we’ll define our application’s initial state. In this case we’re defining56a function to return our initial model, however we could just as easily define57the initial model as a variable elsewhere, too.5859```go62func main() {63 initialModel := model{64 // Our to-do list is just a grocery list65 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},60func initialModel() model {61 return model{62 // Our shopping list is a grocery list63 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},6467 // 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```7273Next we define the `Init` method. `Init` can return a `Cmd` that could perform74some initial I/O. For now, we don't need to do any I/O, so for the command75we'll just return `nil`, which translates to "no command."7677```go78func (m model) Init() tea.Cmd {79 // Just return `nil`, which means "no I/O right now, please."80 return nil81}82```8384## The Update Method8582Next 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 updated84model in response to whatever happened. It can also return a `Cmd` and make85more things happen, but for now don't worry about that part.86Next up is the update method. The update function is called when ”things87happen.” Its job is to look at what has happened and return an updated model in88response. It can also return a `Cmd` to make more things happen, but for now89don't worry about that part.9087In our case, when a user presses the down arrow, `update`'s job is to notice91In our case, when a user presses the down arrow, `Update`’s job is to notice92that the down arrow was pressed and move the cursor accordingly (or not).9390The "something happened" comes in the form of a `Msg`, which can be any type.94The “something happened” comes in the form of a `Msg`, which can be any type.95Messages are the result of some I/O that took place, such as a keypress, timer96tick, or a response from a server.9798We usually figure out which type of `Msg` we received with a type switch, but
45 unmodified lines
144 return m, nil145}146```147144You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command145with the model. That's a special command which instructs the Bubble Tea runtime146to quit, exiting the program.148You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return149a `tea.Quit` command with the model. That’s a special command which instructs150the Bubble Tea runtime to quit, exiting the program.151152## The View Method153150At last, it's time to render our UI. Of all the methods, the view is the154At last, it’s time to render our UI. Of all the methods, the view is the155simplest. We look at the model in it's current state and use it to return156a `string`. That string is our UI!157154Because the view describes the entire UI of your application, you don't have155to worry about redraw logic and stuff like that. Bubble Tea takes care of it158Because the view describes the entire UI of your application, you don’t have to159worry about redrawing logic and stuff like that. Bubble Tea takes care of it160for you.161162```go163func (m model) View() string {
33 unmodified lines
197`tea.NewProgram` and let it rip:198199```go200func main() {197 initialModel := model{198 choices: []string{"Carrots", "Celery", "Kohlrabi"},199 selected: make(map[int]struct{}),200 }201202 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```208210## What's Next?209## What’s Next?210211This tutorial covers the basics of building an interactive terminal UI, but212in the real world you'll also need to perform I/O. To learn about that have a213look at the [Command Tutorial][cmd]. It's pretty simple.
4 unmodified lines
218[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=doc221223### Bubble Tea in the Wild222## Additional Resources223225For 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)226227* [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 manager229230### Libraries we use with Bubble Tea231232* [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.235236[termenv]: https://github.com/muesli/termenv237[reflow]: https://github.com/muesli/reflow238[bubbles]: https://github.com/charmbracelet/bubbles239227### Feedback228229We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!230231* [Twitter](https://twitter.com/charmcli)232* [The Fediverse](https://mastodon.technology/@charm)233234***235236Part of [Charm](https://charm.sh).237238<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>239240Charm热爱开源 • Charm loves open source
modified:tutorials/commands/README.md+26 added-26 removed
181 unmodified lines
182## One More Thing About Commands183184`Cmd`s are defined in Bubble Tea as `type Cmd func() Msg`. So they're just185functions that don't take any arguments and return a `Msg`, which can be186anything. If you need to pass arguments to a command, you just make a function186any type. If you need to pass arguments to a command, you just make a function187that returns a command. For example:188189```go190func cmdWithArg(id int) tea.Cmd {191 return func() tea.Msg {192 return someMsg{id: int}193 }194}195```196197A more real-world example looks like:198199```go200func 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 lines
208 }209}210```211202Just make sure you do as much stuff as you can in the innermost function,203because that's the one that runs asynchronously.212Anyway, just make sure you do as much stuff as you can in the innermost213function, because that's the one that runs asynchronously.214205## Anyway, Now What?215## Now What?216217After doing this tutorial and [the previous one][basics] you should be ready to208build a Bubble Tea program of your own. We also recommend that you look at the218build a Bubble Tea program of your own. We also recommend that you look at the219Bubble Tea [example programs][examples] as well as [Bubbles][bubbles],220a component library for Bubble Tea.221222And, of course, check out the [Go Docs][docs].223214### Bubble Tea in the Wild224## Additional Resources225216For 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)228218* [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 manager220221[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/bubbles224225### Libraries we use with Bubble Tea226227* [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!231232[termenv]: https://github.com/muesli/termenv233[reflow]: https://github.com/muesli/reflow234[bubbles]: https://github.com/charmbracelet/bubbles235[runewidth]: https://github.com/mattn/go-runewidth236229### Feedback230231We'd love to hear your thoughts on this tutorial. Feel free to drop us a note!232233* [Twitter](https://twitter.com/charmcli)234* [The Fediverse](https://mastodon.technology/@charm)235236***237238Part of [Charm](https://charm.sh).239240<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>241242Charm热爱开源 • Charm loves open source
modified:README.md+27 added-30 removed
48 unmodified lines
49[elm]: https://guide.elm-lang.org/architecture/5051## Enough! Let's get to it.5253For this tutorial we're making a to-do list.53For this tutorial we're making a shopping list.5455To start we'll define our package and import some libraries. Our only external56import will be the Bubble Tea library, which we'll call `tea` for short.57
29 unmodified lines
87```8889## Initialization9091Next we'll define our application’s initial state. We’ll store our initial92model in a simple variable, and then define the `Init` method. `Init` can93return a `Cmd` that could perform some initial I/O. For now, we don't need to94do any I/O, so for the command we'll just return `nil`, which translates to "no95command."91Next we’ll define our application’s initial state. In this case we’re defining92a function to return our initial model, however we could just as easily define93the initial model as a variable elsewhere, too.9495```go98func main() {99 initialModel := model{100 // Our to-do list is just a grocery list96func initialModel() model {97 return model{98 // Our shopping list is a grocery list99 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},100101 // 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```108109Next we define the `Init` method. `Init` can return a `Cmd` that could perform110some initial I/O. For now, we don't need to do any I/O, so for the command111we'll just return `nil`, which translates to "no command."112113```go114func (m model) Init() tea.Cmd {115 // Just return `nil`, which means "no I/O right now, please."116 return nil117}118```119120## The Update Method121118Next 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 updated120model in response to whatever happened. It can also return a `Cmd` and make121more things happen, but for now don't worry about that part.122Next up is the update method. The update function is called when ”things123happen.” Its job is to look at what has happened and return an updated model in124response. It can also return a `Cmd` to make more things happen, but for now125don't worry about that part.126123In our case, when a user presses the down arrow, `update`'s job is to notice127In our case, when a user presses the down arrow, `Update`’s job is to notice128that the down arrow was pressed and move the cursor accordingly (or not).129126The "something happened" comes in the form of a `Msg`, which can be any type.130The “something happened” comes in the form of a `Msg`, which can be any type.131Messages are the result of some I/O that took place, such as a keypress, timer132tick, or a response from a server.133134We usually figure out which type of `Msg` we received with a type switch, but
45 unmodified lines
180 return m, nil181}182```183180You may have noticed that "ctrl+c" and "q" above return a `tea.Quit` command181with the model. That's a special command which instructs the Bubble Tea runtime182to quit, exiting the program.184You may have noticed that <kbd>ctrl+c</kbd> and <kbd>q</kbd> above return185a `tea.Quit` command with the model. That’s a special command which instructs186the Bubble Tea runtime to quit, exiting the program.187188## The View Method189186At last, it's time to render our UI. Of all the methods, the view is the190At last, it’s time to render our UI. Of all the methods, the view is the191simplest. We look at the model in it's current state and use it to return192a `string`. That string is our UI!193190Because the view describes the entire UI of your application, you don't have191to worry about redraw logic and stuff like that. Bubble Tea takes care of it194Because the view describes the entire UI of your application, you don’t have to195worry about redrawing logic and stuff like that. Bubble Tea takes care of it196for you.197198```go199func (m model) View() string {
33 unmodified lines
233`tea.NewProgram` and let it rip:234235```go236func main() {233 initialModel := model{234 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},235 selected: make(map[int]struct{}),236 }237238 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```244246## What's Next?245## What’s Next?246247This tutorial covers the basics of building an interactive terminal UI, but248in the real world you'll also need to perform I/O. To learn about that have a249look at the [Command Tutorial][cmd]. It's pretty simple.
56 unmodified lines
306307[elm]: https://guide.elm-lang.org/architecture/308[gotea]: https://github.com/tj/go-tea309311310## License311312[MIT](https://github.com/charmbracelet/bubbletea/raw/master/LICENSE)313316314***315316Part of [Charm](https://charm.sh).317
3 unmodified lines