Rename 'countdown' example to 'timer' and use timer Bubble
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent 99ba63f · Browse files · 2 files·+122 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+122 added−69 removed
- other:+122 added−69 removed
Provenance
- Commit
03d1aa6- Tree
4cdeb65- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
99ba63f
2 files changed+122 added−69 removed
deleted:examples/countdown/main.go-69 removed
1-package main2-3-import (4- "fmt"5- "os"6- "time"7-8- tea "github.com/charmbracelet/bubbletea"9-)10-11-var (12- duration = time.Second * 1013- interval = time.Millisecond14-)15-16-func main() {17- m := model{18- timeout: time.Now().Add(duration),19- }20-21- if err := tea.NewProgram(m).Start(); err != nil {22- fmt.Println("Oh no, it didn't work:", err)23- os.Exit(1)24- }25-}26-27-type tickMsg time.Time28-29-type model struct {30- timeout time.Time31- lastTick time.Time32-}33-34-func (m model) Init() tea.Cmd {35- return tick()36-}37-38-func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {39- switch msg := msg.(type) {40- case tea.KeyMsg:41- switch msg.String() {42- case "ctrl+c", "q", "esc":43- return m, tea.Quit44- }45-46- case tickMsg:47- t := time.Time(msg)48- if t.After(m.timeout) {49- return m, tea.Quit50- }51- m.lastTick = t52- return m, tick()53- }54-55- return m, nil56-}57-58-func (m model) View() string {59- t := m.timeout.Sub(m.lastTick).Milliseconds()60- secs := t / 100061- millis := t % 1000 / 1062- return fmt.Sprintf("This program will quit in %02d:%02d\n", secs, millis)63-}64-65-func tick() tea.Cmd {66- return tea.Tick(time.Duration(interval), func(t time.Time) tea.Msg {67- return tickMsg(t)68- })69-}added:examples/timer/main.go+122 added
1+package main2+3+import (4+ "fmt"5+ "os"6+ "time"7+8+ "github.com/charmbracelet/bubbles/help"9+ "github.com/charmbracelet/bubbles/key"10+ "github.com/charmbracelet/bubbles/timer"11+ tea "github.com/charmbracelet/bubbletea"12+)13+14+const timeout = time.Second * 515+16+type model struct {17+ timer timer.Model18+ keymap keymap19+ help help.Model20+ quitting bool21+}22+23+type keymap struct {24+ start key.Binding25+ stop key.Binding26+ reset key.Binding27+ quit key.Binding28+}29+30+func (m model) Init() tea.Cmd {31+ return m.timer.Init()32+}33+34+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {35+ switch msg := msg.(type) {36+ case timer.TickMsg:37+ var cmd tea.Cmd38+ m.timer, cmd = m.timer.Update(msg)39+ return m, cmd40+41+ case timer.StartStopMsg:42+ var cmd tea.Cmd43+ m.timer, cmd = m.timer.Update(msg)44+ m.keymap.stop.SetEnabled(m.timer.Running())45+ m.keymap.start.SetEnabled(!m.timer.Running())46+ return m, cmd47+48+ case timer.TimeoutMsg:49+ m.quitting = true50+ return m, tea.Quit51+52+ case tea.KeyMsg:53+ switch {54+ case key.Matches(msg, m.keymap.quit):55+ m.quitting = true56+ return m, tea.Quit57+ case key.Matches(msg, m.keymap.reset):58+ m.timer.Timeout = timeout59+ case key.Matches(msg, m.keymap.start, m.keymap.stop):60+ return m, m.timer.Toggle()61+ }62+ }63+64+ return m, nil65+}66+67+func (m model) helpView() string {68+ return "\n" + m.help.ShortHelpView([]key.Binding{69+ m.keymap.start,70+ m.keymap.stop,71+ m.keymap.reset,72+ m.keymap.quit,73+ })74+}75+76+func (m model) View() string {77+ // For a more detailed timer view you could read m.timer.Timeout to get78+ // the remaining time as a time.Duration and skip calling m.timer.View()79+ // entirely.80+ s := m.timer.View()81+82+ if m.timer.Timedout() {83+ s = "All done!"84+ }85+ s += "\n"86+ if !m.quitting {87+ s = "Exiting in " + s88+ s += m.helpView()89+ }90+ return s91+}92+93+func main() {94+ m := model{95+ timer: timer.NewWithInterval(timeout, time.Millisecond),96+ keymap: keymap{97+ start: key.NewBinding(98+ key.WithKeys("s"),99+ key.WithHelp("s", "start"),100+ ),101+ stop: key.NewBinding(102+ key.WithKeys("s"),103+ key.WithHelp("s", "stop"),104+ ),105+ reset: key.NewBinding(106+ key.WithKeys("r"),107+ key.WithHelp("r", "reset"),108+ ),109+ quit: key.NewBinding(110+ key.WithKeys("q", "ctrl+c"),111+ key.WithHelp("q", "quit"),112+ ),113+ },114+ help: help.NewModel(),115+ }116+ m.keymap.start.SetEnabled(false)117+118+ if err := tea.NewProgram(m).Start(); err != nil {119+ fmt.Println("Uh oh, we encountered an error:", err)120+ os.Exit(1)121+ }122+}