mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
Every sends the time at which the timer ticked
This commit is contained in:
@@ -12,7 +12,11 @@ import (
|
||||
|
||||
type model int
|
||||
|
||||
type tickMsg struct{}
|
||||
type tickMsg time.Time
|
||||
|
||||
func newTickMsg(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
}
|
||||
|
||||
func main() {
|
||||
tea.AltScreen()
|
||||
@@ -55,7 +59,7 @@ func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
||||
|
||||
func subscriptions(_ tea.Model) tea.Subs {
|
||||
return tea.Subs{
|
||||
"tick": tea.Every(time.Second, tickMsg{}),
|
||||
"tick": tea.Every(time.Second, newTickMsg),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ type Model int
|
||||
|
||||
// Messages are events that we respond to in our Update function. This
|
||||
// particular one indicates that the timer has ticked.
|
||||
type TickMsg struct{}
|
||||
type tickMsg time.Time
|
||||
|
||||
func main() {
|
||||
// Initialize our program
|
||||
@@ -41,7 +41,7 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
||||
switch msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
return m, tea.Quit
|
||||
case TickMsg:
|
||||
case tickMsg:
|
||||
m -= 1
|
||||
if m <= 0 {
|
||||
return m, tea.Quit
|
||||
@@ -61,6 +61,8 @@ func view(model tea.Model) string {
|
||||
// second, sends a tick, and then restarts.
|
||||
func subscriptions(_ tea.Model) tea.Subs {
|
||||
return tea.Subs{
|
||||
"tick": tea.Every(time.Second, TickMsg{}),
|
||||
"tick": tea.Every(time.Second, func(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,20 +12,6 @@ import (
|
||||
"github.com/fogleman/ease"
|
||||
)
|
||||
|
||||
// Model contains the data for our application.
|
||||
type Model struct {
|
||||
Choice int
|
||||
Chosen bool
|
||||
Ticks int
|
||||
Frames int
|
||||
Progress float64
|
||||
Loaded bool
|
||||
}
|
||||
|
||||
type tickMsg struct{}
|
||||
|
||||
type frameMsg struct{}
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(
|
||||
initialize,
|
||||
@@ -38,6 +24,32 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// MSG
|
||||
|
||||
type tickMsg time.Time
|
||||
|
||||
func newTickMsg(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
}
|
||||
|
||||
type frameMsg time.Time
|
||||
|
||||
func newFrameMsg(t time.Time) tea.Msg {
|
||||
return frameMsg(t)
|
||||
}
|
||||
|
||||
// MODEL
|
||||
|
||||
// Model contains the data for our application.
|
||||
type Model struct {
|
||||
Choice int
|
||||
Chosen bool
|
||||
Ticks int
|
||||
Frames int
|
||||
Progress float64
|
||||
Loaded bool
|
||||
}
|
||||
|
||||
// INIT
|
||||
|
||||
func initialize() (tea.Model, tea.Cmd) {
|
||||
@@ -50,15 +62,15 @@ func subscriptions(model tea.Model) tea.Subs {
|
||||
m, _ := model.(Model)
|
||||
if !m.Chosen || m.Loaded {
|
||||
return tea.Subs{
|
||||
"tick": tea.Every(time.Second, tickMsg{}),
|
||||
"tick": tea.Every(time.Second, newTickMsg),
|
||||
}
|
||||
}
|
||||
return tea.Subs{
|
||||
"frame": tea.Every(time.Second/60, frameMsg{}),
|
||||
"frame": tea.Every(time.Second/60, newFrameMsg),
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATES
|
||||
// UPDATE
|
||||
|
||||
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
|
||||
m, _ := model.(Model)
|
||||
@@ -145,7 +157,7 @@ func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// VIEWS
|
||||
// VIEW
|
||||
|
||||
func view(model tea.Model) string {
|
||||
m, _ := model.(Model)
|
||||
|
||||
Reference in New Issue
Block a user