Skip to content
Myr

Exit demo after 10 seconds to demonstrate command usage

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 147c8df · Browse files · 1 file+25 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−11 removed

  • other:+25 added−11 removed

1 file+25 added−11 removed

modified:example/main.go+25 added-11 removed
4 unmodified lines
5 "tea"6 "time"7)89// Model contains the data for our application.10type Model struct {11 Choice int12 Ticks int13}1415// TickMsg signals that the timer has ticked16type TickMsg struct{}1716const tpl = `What to do today?1718%s1920Elapsed: %d seconds.2122(press j/k or up/down to select, q or esc to quit)`2318func main() {25 p := tea.NewProgram(Model{0, 0}, update, view, []tea.Sub{tick})19 p := tea.NewProgram(20 Model{0, 10},21 update,22 view,23 []tea.Sub{tick},24 )25 if err := p.Start(); err != nil {26 fmt.Println("could not start program:", err)27 }28}2930// Update. Triggered when new messages arrive.31func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {32 m, _ := model.(Model)3334 switch msg := msg.(type) {
22 unmodified lines
57 return m, tea.Quit58 }5960 case TickMsg:61 if m.Ticks == 0 {62 return m, tea.Quit63 }61 m.Ticks += 164 m.Ticks -= 165 }6667 return m, nil68}6970// Subscription71func tick(_ tea.Model) tea.Msg {69 time.Sleep(time.Second * 1)72 time.Sleep(time.Second)73 return TickMsg{}74}7576// View template77const tpl = `What to do today?7879%s8081Program quits in %d seconds.8283(press j/k or up/down to select, q or esc to quit)`8485// View function. Called after an Update().86func view(model tea.Model) string {87 m, _ := model.(Model)88 c := m.Choice89
7 unmodified lines
9798 return fmt.Sprintf(tpl, choices, m.Ticks)99}100101// Checkbox widget102func checkbox(label string, checked bool) string {103 check := " "104 if checked {105 check = "x"
3 unmodified lines