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
Provenance
- Commit
d503d5d- Tree
839308a- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
ac67237
4 files changed+49 added−27 removed
modified:examples/fullscreen/main.go+6 added-2 removed
10 unmodified lines11 )12 13 type model int14 15-type tickMsg struct{}15+type tickMsg time.Time16 17+func newTickMsg(t time.Time) tea.Msg {18+ return tickMsg(t)19+}20+21 func main() {22 tea.AltScreen()23 defer tea.ExitAltScreen()24 err := tea.NewProgram(initialize, update, view, subscriptions).Start()33 unmodified lines58 }59 60 func 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 }65 66 func view(mdl tea.Model) string {3 unmodified linesmodified:examples/simple/main.go+5 added-3 removed
15 unmodified lines16 type Model int17 18 // Messages are events that we respond to in our Update function. This19 // particular one indicates that the timer has ticked.20-type TickMsg struct{}20+type tickMsg time.Time21 22 func main() {23 // Initialize our program24 p := tea.NewProgram(initialize, update, view, subscriptions)15 unmodified lines40 41 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 lines60 // This is a subscription which we setup in NewProgram(). It waits for one61 // second, sends a tick, and then restarts.62 func 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 lines11 "github.com/charmbracelet/tea"12 "github.com/fogleman/ease"13 )14 15-// Model contains the data for our application.16-type Model struct {17- Choice int18- Chosen bool19- Ticks int20- Frames int21- Progress float6422- Loaded bool23-}24-25-type tickMsg struct{}26-27-type frameMsg struct{}28-15 func main() {16 p := tea.NewProgram(17 initialize,18 update,4 unmodified lines23 fmt.Println("could not start program:", err)24 }25 }26 27+// MSG28+29+type tickMsg time.Time30+31+func newTickMsg(t time.Time) tea.Msg {32+ return tickMsg(t)33+}34+35+type frameMsg time.Time36+37+func newFrameMsg(t time.Time) tea.Msg {38+ return frameMsg(t)39+}40+41+// MODEL42+43+// Model contains the data for our application.44+type Model struct {45+ Choice int46+ Chosen bool47+ Ticks int48+ Frames int49+ Progress float6450+ Loaded bool51+}52+53 // INIT54 55 func initialize() (tea.Model, tea.Cmd) {56 return Model{0, false, 10, 0, 0, false}, nil4 unmodified lines61 func 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 }72 61-// UPDATES73+// UPDATE74 75 func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {76 m, _ := model.(Model)77 78 unmodified lines156 157 return m, nil158 }159 148-// VIEWS160+// VIEW161 162 func view(model tea.Model) string {163 m, _ := model.(Model)164 if !m.Chosen {66 unmodified linesmodified:subscriptions.go+8 added-4 removed
2 unmodified lines3 import (4 "time"5 )6 7+// NewEverMsg is used by Every to create a new message. It contains the time8+// at which the timer finished.9+type NewEveryMsg func(time.Time) Msg10+11 // Every is a subscription that ticks with the system clock at the given8-// duration12+// duration.13 //14 // TODO: make it cancelable11-func Every(duration time.Duration, msg Msg) Sub {15+func 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 }