Clean up 'views' example
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 6fcdf99 · Browse files · 2 files·+27 added−26 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+27 added−26 removed
- other:+27 added−26 removed
Provenance
- Commit
43bcbfc- Tree
367aa5f- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
6fcdf99
2 files changed+27 added−26 removed
modified:examples/views/main.go+24 added-26 removed
1 package main2 3-// TODO: The views feel messy. Clean 'em up.4-3 import (4 "fmt"5 "math"6 "strings"13 unmodified lines20 fmt.Println("could not start program:", err)21 }22 }23 26-// MSG24+// MESSAGES25 26 type tickMsg struct{}29-27 type frameMsg struct{}28 29 // MODEL30 34-// Model contains the data for our application.35-type Model struct {31+type model struct {32 Choice int33 Chosen bool34 Ticks int35 Frames int36 Progress float6437 Loaded bool38 }39 44-// INIT45-40 func initialize() (tea.Model, tea.Cmd) {47- return Model{0, false, 10, 0, 0, false}, tick()41+ return model{0, false, 10, 0, 0, false}, tick()42 }43 44 // CMDS45 10 unmodified lines56 }57 58 // UPDATE59 60+// Main update function. This just hands off the message and model to the61+// approprate update loop based on the current state.66-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {67- m, _ := model.(Model)62+func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {63+ m, _ := mdl.(model)64 65 if !m.Chosen {66 return updateChoices(msg, m)67 }68 return updateChosen(msg, m)69 }70 71+// Update loop for the first view where you're choosing a task75-func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {72+func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) {73 switch msg := msg.(type) {74 75 case tea.KeyMsg:76 switch msg.String() {32 unmodified lines109 110 return m, nil111 }112 113+// Update loop for the second view after a choice has been made116-func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {114+func updateChosen(msg tea.Msg, m model) (tea.Model, tea.Cmd) {115 switch msg := msg.(type) {116 117 case tea.KeyMsg:118 switch msg.String() {32 unmodified lines151 }152 153 // VIEW154 155+// The main view, which just calls the approprate sub-view157-func view(model tea.Model) string {158- m, _ := model.(Model)156+func view(mdl tea.Model) string {157+ m, _ := mdl.(model)158 if !m.Chosen {159 return choicesView(m) + "\n"160 }161 return chosenView(m) + "\n"162 }163 165-const choicesTpl = `What to do today?166-167-%s168-169-Program quits in %d seconds.170-171-(press j/k or up/down to select, enter to choose, and q or esc to quit)`172-173-func choicesView(m Model) string {164+// The first view, where you're choosing a task165+func choicesView(m model) string {166 c := m.Choice167 168+ tpl := "What to do today?\n\n"169+ tpl += "%s\n\n"170+ tpl += "Program quits in %d seconds\n\n"171+ tpl += "(press j/k or up/down to select, enter to choose, and q or esc to quit)`"172+173 choices := fmt.Sprintf(174 "%s\n%s\n%s\n%s",175 checkbox("Plant carrots", c == 0),176 checkbox("Go to the market", c == 1),177 checkbox("Read something", c == 2),178 checkbox("See friends", c == 3),179 )180 184- return fmt.Sprintf(choicesTpl, choices, m.Ticks)181+ return fmt.Sprintf(tpl, choices, m.Ticks)182 }183 184+// The second view, after a task has been chosen187-func chosenView(m Model) string {185+func chosenView(m model) string {186 var msg string187 188 switch m.Choice {189 case 0:34 unmodified linesmodified:README.md+3 added
10 unmodified lines111213141516171819202144 unmodified lines10 unmodified linesBubble Tea is well-suited for simple and complex terminal applications,either inline, full-window, or a mix of both. It's been battle-tested inseveral large projects and is production-ready.
It features a standard framerate-based renderer which is used by default aswell as support for high-performance scrollable regions when needed.
To get started, see the [tutorials][tutorials] and [examples][examples].
[tutorials]: https://github.com/charmbracelet/tea/tree/master/tutorials[examples]: https://github.com/charmbracelet/tea/tree/master/examples44 unmodified lines