Skip to content
Myr

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

2 files+27 added−26 removed

modified:examples/views/main.go+24 added-26 removed
1package main23// TODO: The views feel messy. Clean 'em up.43import (4 "fmt"5 "math"6 "strings"
13 unmodified lines
20 fmt.Println("could not start program:", err)21 }22}2326// MSG24// MESSAGES2526type tickMsg struct{}2927type frameMsg struct{}2829// MODEL3034// Model contains the data for our application.35type Model struct {31type model struct {32 Choice int33 Chosen bool34 Ticks int35 Frames int36 Progress float6437 Loaded bool38}3944// INIT4540func 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}4344// CMDS45
10 unmodified lines
56}5758// UPDATE5960// Main update function. This just hands off the message and model to the61// approprate update loop based on the current state.66func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {67 m, _ := model.(Model)62func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {63 m, _ := mdl.(model)6465 if !m.Chosen {66 return updateChoices(msg, m)67 }68 return updateChosen(msg, m)69}7071// Update loop for the first view where you're choosing a task75func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {72func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) {73 switch msg := msg.(type) {7475 case tea.KeyMsg:76 switch msg.String() {
32 unmodified lines
109110 return m, nil111}112113// Update loop for the second view after a choice has been made116func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {114func updateChosen(msg tea.Msg, m model) (tea.Model, tea.Cmd) {115 switch msg := msg.(type) {116117 case tea.KeyMsg:118 switch msg.String() {
32 unmodified lines
151}152153// VIEW154155// The main view, which just calls the approprate sub-view157func view(model tea.Model) string {158 m, _ := model.(Model)156func 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}163165const choicesTpl = `What to do today?166167%s168169Program quits in %d seconds.170171(press j/k or up/down to select, enter to choose, and q or esc to quit)`172173func choicesView(m Model) string {164// The first view, where you're choosing a task165func choicesView(m model) string {166 c := m.Choice167168 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)`"172173 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 )180184 return fmt.Sprintf(choicesTpl, choices, m.Ticks)181 return fmt.Sprintf(tpl, choices, m.Ticks)182}183184// The second view, after a task has been chosen187func chosenView(m Model) string {185func chosenView(m model) string {186 var msg string187188 switch m.Choice {189 case 0:
34 unmodified lines
modified:README.md+3 added