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() {
|
2020-10-15 19:48:42 -04:00
|
|
|
p := tea.NewProgram(model(5))
|
2020-08-22 06:32:12 -04:00
|
|
|
|
2021-03-08 12:48:34 -05:00
|
|
|
// Bubble Tea will automatically exit the alternate screen buffer.
|
2020-08-22 06:32:12 -04:00
|
|
|
p.EnterAltScreen()
|
|
|
|
err := p.Start()
|
|
|
|
|
2020-01-17 15:37:04 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return tick()
|
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() {
|
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-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)
|
|
|
|
})
|
|
|
|
}
|