Skip to content

Instantly share code, notes, and snippets.

@meowgorithm
Created May 18, 2022 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meowgorithm/db4bbb2cffcf66205efd73f272a5364d to your computer and use it in GitHub Desktop.
Save meowgorithm/db4bbb2cffcf66205efd73f272a5364d to your computer and use it in GitHub Desktop.
69 FPS in Bubble Tea
package main
import (
"fmt"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type stepMsg time.Time
func stepAnimation() tea.Cmd {
return tea.Tick(time.Second/69, func(t time.Time) tea.Msg {
return stepMsg(t)
})
}
type model struct {
frame int
}
func (m model) Init() tea.Cmd {
return stepAnimation()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tea.KeyMsg:
return m, tea.Quit
case stepMsg:
m.frame++
return m, stepAnimation()
}
return m, nil
}
func (m model) View() string {
return fmt.Sprintf("Frame: %d", m.frame)
}
func main() {
if err := tea.NewProgram(model{}).Start(); err != nil {
fmt.Println("Oh no!", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment