2020-01-15 16:44:11 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
// A simple program that counts down from 5 and then exits.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"tea"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type model int
|
|
|
|
|
|
|
|
type tickMsg struct{}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := tea.NewProgram(model(5), update, view, []tea.Sub{tick}).Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
|
|
|
m, _ := mdl.(model)
|
|
|
|
|
|
|
|
switch msg.(type) {
|
|
|
|
case tickMsg:
|
|
|
|
m -= 1
|
|
|
|
if m <= 0 {
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func view(mdl tea.Model) string {
|
|
|
|
m, _ := mdl.(model)
|
2020-01-16 14:47:44 -05:00
|
|
|
return fmt.Sprintf("Hi. This program will exit in %d seconds...", m)
|
2020-01-15 16:44:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func tick(_ tea.Model) tea.Msg {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
return tickMsg{}
|
|
|
|
}
|