1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package main
// This example illustrates how to debounce commands.
//
// When the user presses a key we increment the "tag" value on the model and,
// after a short delay, we include that tag value in the message produced
// by the Tick command.
//
// In a subsequent Update, if the tag in the Msg matches current tag on the
// model's state we know that the debouncing is complete and we can proceed as
// normal. If not, we simply ignore the inbound message.
import (
"fmt"
"os"
"time"
tea "github.com/charmbracelet/bubbletea"
)
const debounceDuration = time.Second
type exitMsg int
type model struct {
tag int
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
// Increment the tag on the model...
m.tag++
return m, tea.Tick(debounceDuration, func(_ time.Time) tea.Msg {
// ...and include a copy of that tag value in the message.
return exitMsg(m.tag)
})
case exitMsg:
// If the tag in the message doesn't match the tag on the model then we
// know that this message was not the last one sent and another is on
// the way. If that's the case we know, we can ignore this message.
// Otherwise, the debounce timeout has passed and this message is a
// valid debounced one.
if int(msg) == m.tag {
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
return fmt.Sprintf("Key presses: %d", m.tag) +
"\nTo exit press any key, then wait for one second without pressing anything."
}
func main() {
if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("uh oh:", err)
os.Exit(1)
}
}
|