Skip to content
Myr

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

2 files+122 added−69 removed

deleted:examples/countdown/main.go-69 removed
1package main23import (4 "fmt"5 "os"6 "time"78 tea "github.com/charmbracelet/bubbletea"9)1011var (12 duration = time.Second * 1013 interval = time.Millisecond14)1516func main() {17 m := model{18 timeout: time.Now().Add(duration),19 }2021 if err := tea.NewProgram(m).Start(); err != nil {22 fmt.Println("Oh no, it didn't work:", err)23 os.Exit(1)24 }25}2627type tickMsg time.Time2829type model struct {30 timeout time.Time31 lastTick time.Time32}3334func (m model) Init() tea.Cmd {35 return tick()36}3738func (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 }4546 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 }5455 return m, nil56}5758func (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}6465func 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
1package main23import (4 "fmt"5 "os"6 "time"78 "github.com/charmbracelet/bubbles/help"9 "github.com/charmbracelet/bubbles/key"10 "github.com/charmbracelet/bubbles/timer"11 tea "github.com/charmbracelet/bubbletea"12)1314const timeout = time.Second * 51516type model struct {17 timer timer.Model18 keymap keymap19 help help.Model20 quitting bool21}2223type keymap struct {24 start key.Binding25 stop key.Binding26 reset key.Binding27 quit key.Binding28}2930func (m model) Init() tea.Cmd {31 return m.timer.Init()32}3334func (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, cmd4041 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, cmd4748 case timer.TimeoutMsg:49 m.quitting = true50 return m, tea.Quit5152 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 }6364 return m, nil65}6667func (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}7576func (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()8182 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}9293func 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)117118 if err := tea.NewProgram(m).Start(); err != nil {119 fmt.Println("Uh oh, we encountered an error:", err)120 os.Exit(1)121 }122}