2020-01-17 15:37:04 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
// A simple program that counts down from 5 and then exits.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2020-01-18 11:15:55 -05:00
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
"github.com/charmbracelet/boba"
|
2020-01-17 15:37:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type model int
|
|
|
|
|
2020-05-05 14:26:06 -04:00
|
|
|
type tickMsg time.Time
|
|
|
|
|
2020-01-17 15:37:04 -05:00
|
|
|
func main() {
|
2020-05-12 16:39:08 -04:00
|
|
|
boba.AltScreen()
|
|
|
|
defer boba.ExitAltScreen()
|
2020-05-12 17:56:30 -04:00
|
|
|
err := boba.NewProgram(initialize, update, view).Start()
|
2020-01-17 15:37:04 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
func initialize() (boba.Model, boba.Cmd) {
|
2020-05-12 17:56:30 -04:00
|
|
|
return model(5), tick()
|
2020-01-18 22:18:19 -05:00
|
|
|
}
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
func update(message boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
|
2020-01-17 15:37:04 -05:00
|
|
|
m, _ := mdl.(model)
|
|
|
|
|
2020-01-17 20:46:34 -05:00
|
|
|
switch msg := message.(type) {
|
2020-01-17 15:37:04 -05:00
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
case boba.KeyMsg:
|
2020-01-17 20:46:34 -05:00
|
|
|
switch msg.String() {
|
2020-01-26 16:46:30 -05:00
|
|
|
case "ctrl+c":
|
2020-01-17 15:37:04 -05:00
|
|
|
fallthrough
|
|
|
|
case "esc":
|
|
|
|
fallthrough
|
|
|
|
case "q":
|
2020-05-12 16:39:08 -04:00
|
|
|
return m, boba.Quit
|
2020-01-17 15:37:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case tickMsg:
|
|
|
|
m -= 1
|
|
|
|
if m <= 0 {
|
2020-05-12 16:39:08 -04:00
|
|
|
return m, boba.Quit
|
2020-01-17 15:37:04 -05:00
|
|
|
}
|
2020-05-12 17:56:30 -04:00
|
|
|
return m, tick()
|
2020-01-17 15:37:04 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:39:08 -04:00
|
|
|
func view(mdl boba.Model) string {
|
2020-01-17 15:37:04 -05:00
|
|
|
m, _ := mdl.(model)
|
|
|
|
return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)
|
|
|
|
}
|
2020-05-12 17:56:30 -04:00
|
|
|
|
|
|
|
func tick() boba.Cmd {
|
|
|
|
return boba.Tick(time.Second, func(t time.Time) boba.Msg {
|
|
|
|
return tickMsg(t)
|
|
|
|
})
|
|
|
|
}
|