Update example in README

This commit is contained in:
Christian Rocha 2020-05-27 14:21:54 -04:00
parent 35731d4046
commit a454089275
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 13 additions and 11 deletions

View File

@ -18,20 +18,25 @@ import (
"fmt" "fmt"
"log" "log"
"time" "time"
"github.com/charmbracelet/tea" tea "github.com/charmbracelet/bubbletea"
) )
type model int type model int
type tickMsg time.Time type tickMsg struct{}
func main() { func main() {
p := tea.NewProgram(init, update, view, subscriptions) p := tea.NewProgram(initialize, update, view, subscriptions)
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
// Return the initial model and initial command
func initialize() (tea.Model, tea.Cmd) {
return 5, tick
}
// Listen for messages and update the model accordingly // Listen for messages and update the model accordingly
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
m, _ := mdl.(model) m, _ := mdl.(model)
@ -52,13 +57,10 @@ func view(mdl tea.Model) string {
return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m) return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
} }
// Subscribe to events // A simple command which Bubble Tea runs asynchronously.
func subscriptions(_ tea.Model) tea.Subs { func tick() tea.Msg {
return tea.Subs{ time.Sleep(time.Second)
"tick": time.Every(time.Second, func(t time.Time) tea.Msg { return tickMsg{}
return tickMsg(t)
},
}
} }
``` ```