New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: stopwatch #68
feat: stopwatch #68
Conversation
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
So in cases like this, package main
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/bubbles/stopwatch"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
stopwatch stopwatch.Model
}
func (m model) Init() tea.Cmd {
return m.stopwatch.Init()
}
func (m model) View() string {
return "Elapsed: " + m.stopwatch.View()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "s":
return m, func() tea.Msg {
return stopwatch.StopMsg{}
}
}
}
var cmd tea.Cmd
m.stopwatch, cmd = m.stopwatch.Update(msg)
return m, cmd
}
func main() {
m := model{
stopwatch: stopwatch.NewWithInternval(time.Millisecond),
}
if err := tea.NewProgram(m).Start(); err != nil {
fmt.Println("Oh no, it didn't work:", err)
os.Exit(1)
}
} Though technically we can stop the timer without package main
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/bubbles/stopwatch"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
stopwatch stopwatch.Model
runTimer bool
}
func (m model) Init() tea.Cmd {
return m.stopwatch.Init()
}
func (m model) View() string {
return "Elapsed: " + m.stopwatch.View()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "s":
// Toggle start/stop
m.runTimer = !m.runTimer
if m.runTimer {
return m, m.stopwatch.Init()
}
}
}
var cmd tea.Cmd
if m.runTimer {
m.stopwatch, cmd = m.stopwatch.Update(msg)
}
return m, cmd
}
func main() {
m := model{
stopwatch: stopwatch.NewWithInterval(time.Millisecond),
runTimer: true,
}
if err := tea.NewProgram(m).Start(); err != nil {
fmt.Println("Oh no, it didn't work:", err)
os.Exit(1)
}
} …so I wonder if we need it at all? If we do, maybe |
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
Make it a bit more feature-full:
usage now looks like this: package main
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/bubbles/stopwatch"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
stopwatch stopwatch.Model
}
func (m model) Init() tea.Cmd {
return m.stopwatch.Init()
}
func (m model) View() string {
return "Elapsed: " + m.stopwatch.View()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "r":
return m, m.stopwatch.Reset()
case "s":
if m.stopwatch.Running() {
return m, m.stopwatch.Stop()
}
return m, m.stopwatch.Start()
}
}
var cmd tea.Cmd
m.stopwatch, cmd = m.stopwatch.Update(msg)
return m, cmd
}
func main() {
m := model{
stopwatch: stopwatch.NewWithInterval(time.Second),
}
if err := tea.NewProgram(m).Start(); err != nil {
fmt.Println("Oh no, it didn't work:", err)
os.Exit(1)
}
} thoughts? I wonder if we should also have a |
Looks great! Yeah, we might as well add a |
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
follow up of #67, a simple stopwatch component