bubbletea/examples/countdown/main.go

70 lines
1.1 KiB
Go
Raw Normal View History

2020-10-25 16:56:39 -04:00
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 tick()
2020-10-25 16:56:39 -04:00
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
2021-05-01 09:28:58 -04:00
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
}
2020-10-25 16:56:39 -04:00
case tickMsg:
t := time.Time(msg)
if t.After(m.timeout) {
return m, tea.Quit
}
m.lastTick = t
return m, tick()
2020-10-25 16:56:39 -04:00
}
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 tick() tea.Cmd {
2020-10-25 16:56:39 -04:00
return tea.Tick(time.Duration(interval), func(t time.Time) tea.Msg {
return tickMsg(t)
})
}