mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
Add timer countdown example
This commit is contained in:
70
examples/countdown/main.go
Normal file
70
examples/countdown/main.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
var (
|
||||
duration = time.Second * 10
|
||||
interval = time.Millisecond
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := model{
|
||||
timeout: time.Now().Add(duration),
|
||||
}
|
||||
|
||||
if err := tea.NewProgram(m).Start(); err != nil {
|
||||
fmt.Println("Oh no, it didn't work:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
type tickMsg time.Time
|
||||
|
||||
type model struct {
|
||||
timeout time.Time
|
||||
lastTick time.Time
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return m.tick()
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
|
||||
case tickMsg:
|
||||
t := time.Time(msg)
|
||||
if t.After(m.timeout) {
|
||||
return m, tea.Quit
|
||||
}
|
||||
m.lastTick = t
|
||||
return m, m.tick()
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
t := m.timeout.Sub(m.lastTick).Milliseconds()
|
||||
secs := t / 1000
|
||||
millis := t % 1000 / 10
|
||||
return fmt.Sprintf("This program will quit in %02d:%02d\n", secs, millis)
|
||||
}
|
||||
|
||||
func (m model) tick() tea.Cmd {
|
||||
return tea.Tick(time.Duration(interval), func(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user