2020-01-17 15:37:04 -05:00
|
|
|
package main
|
|
|
|
|
2020-10-14 11:51:04 -04:00
|
|
|
// A simple program that opens the alternate screen buffer then counts down
|
|
|
|
// from 5 and then exits.
|
2020-01-17 15:37:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2020-01-18 11:15:55 -05:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
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() {
|
2021-05-19 20:52:01 -04:00
|
|
|
p := tea.NewProgram(model(5), tea.WithAltScreen())
|
|
|
|
if err := p.Start(); err != nil {
|
2020-01-17 15:37:04 -05:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
2021-05-01 09:28:58 -04:00
|
|
|
return tea.Batch(tick(), tea.EnterAltScreen)
|
2020-01-18 22:18:19 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
|
2020-01-17 20:46:34 -05:00
|
|
|
switch msg := message.(type) {
|
2020-01-17 15:37:04 -05:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
case tea.KeyMsg:
|
2020-01-17 20:46:34 -05:00
|
|
|
switch msg.String() {
|
2021-05-01 09:28:58 -04:00
|
|
|
case "q", "esc", "ctrl+c":
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Quit
|
2020-01-17 15:37:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case tickMsg:
|
|
|
|
m -= 1
|
|
|
|
if m <= 0 {
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.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-10-15 19:48:42 -04:00
|
|
|
func (m model) View() string {
|
2020-01-17 15:37:04 -05:00
|
|
|
return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)
|
|
|
|
}
|
2020-05-12 17:56:30 -04:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func tick() tea.Cmd {
|
|
|
|
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
|
2020-05-12 17:56:30 -04:00
|
|
|
return tickMsg(t)
|
|
|
|
})
|
|
|
|
}
|