Skip to content
Myr

Every sends the time at which the timer ticked

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 ac67237 · Browse files · 4 files+49 added−27 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+49 added−27 removed

  • other:+49 added−27 removed

4 files+49 added−27 removed

modified:examples/fullscreen/main.go+6 added-2 removed
10 unmodified lines
11)1213type model int1415type tickMsg struct{}15type tickMsg time.Time1617func newTickMsg(t time.Time) tea.Msg {18 return tickMsg(t)19}2021func main() {22 tea.AltScreen()23 defer tea.ExitAltScreen()24 err := tea.NewProgram(initialize, update, view, subscriptions).Start()
33 unmodified lines
58}5960func subscriptions(_ tea.Model) tea.Subs {61 return tea.Subs{58 "tick": tea.Every(time.Second, tickMsg{}),62 "tick": tea.Every(time.Second, newTickMsg),63 }64}6566func view(mdl tea.Model) string {
3 unmodified lines
modified:examples/simple/main.go+5 added-3 removed
15 unmodified lines
16type Model int1718// Messages are events that we respond to in our Update function. This19// particular one indicates that the timer has ticked.20type TickMsg struct{}20type tickMsg time.Time2122func main() {23 // Initialize our program24 p := tea.NewProgram(initialize, update, view, subscriptions)
15 unmodified lines
4041 switch msg.(type) {42 case tea.KeyMsg:43 return m, tea.Quit44 case TickMsg:44 case tickMsg:45 m -= 146 if m <= 0 {47 return m, tea.Quit48 }
11 unmodified lines
60// This is a subscription which we setup in NewProgram(). It waits for one61// second, sends a tick, and then restarts.62func subscriptions(_ tea.Model) tea.Subs {63 return tea.Subs{64 "tick": tea.Every(time.Second, TickMsg{}),64 "tick": tea.Every(time.Second, func(t time.Time) tea.Msg {65 return tickMsg(t)66 }),67 }68}
modified:examples/views/main.go+30 added-18 removed
10 unmodified lines
11 "github.com/charmbracelet/tea"12 "github.com/fogleman/ease"13)1415// Model contains the data for our application.16type Model struct {17 Choice int18 Chosen bool19 Ticks int20 Frames int21 Progress float6422 Loaded bool23}2425type tickMsg struct{}2627type frameMsg struct{}2815func main() {16 p := tea.NewProgram(17 initialize,18 update,
4 unmodified lines
23 fmt.Println("could not start program:", err)24 }25}2627// MSG2829type tickMsg time.Time3031func newTickMsg(t time.Time) tea.Msg {32 return tickMsg(t)33}3435type frameMsg time.Time3637func newFrameMsg(t time.Time) tea.Msg {38 return frameMsg(t)39}4041// MODEL4243// Model contains the data for our application.44type Model struct {45 Choice int46 Chosen bool47 Ticks int48 Frames int49 Progress float6450 Loaded bool51}5253// INIT5455func initialize() (tea.Model, tea.Cmd) {56 return Model{0, false, 10, 0, 0, false}, nil
4 unmodified lines
61func subscriptions(model tea.Model) tea.Subs {62 m, _ := model.(Model)63 if !m.Chosen || m.Loaded {64 return tea.Subs{53 "tick": tea.Every(time.Second, tickMsg{}),65 "tick": tea.Every(time.Second, newTickMsg),66 }67 }68 return tea.Subs{57 "frame": tea.Every(time.Second/60, frameMsg{}),69 "frame": tea.Every(time.Second/60, newFrameMsg),70 }71}7261// UPDATES73// UPDATE7475func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {76 m, _ := model.(Model)77
78 unmodified lines
156157 return m, nil158}159148// VIEWS160// VIEW161162func view(model tea.Model) string {163 m, _ := model.(Model)164 if !m.Chosen {
66 unmodified lines
modified:subscriptions.go+8 added-4 removed
2 unmodified lines
3import (4 "time"5)67// NewEverMsg is used by Every to create a new message. It contains the time8// at which the timer finished.9type NewEveryMsg func(time.Time) Msg1011// Every is a subscription that ticks with the system clock at the given8// duration12// duration.13//14// TODO: make it cancelable11func Every(duration time.Duration, msg Msg) Sub {15func Every(duration time.Duration, newMsg NewEveryMsg) Sub {16 return func() Msg {17 n := time.Now()18 d := n.Truncate(duration).Add(duration).Sub(n)19 t := time.NewTimer(d)20 select {17 case <-t.C:18 return msg21 case now := <-t.C:22 return newMsg(now)23 }24 }25}