-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
395 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Package stopwatch provides a simple stopwatch component | ||
package stopwatch | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
// Slightly adjusted version of https://github.com/charmbracelet/bubbles/blob/master/stopwatch/stopwatch.go | ||
|
||
var lastID int64 | ||
|
||
func nextID() int { | ||
return int(atomic.AddInt64(&lastID, 1)) | ||
} | ||
|
||
// TickMsg is a message that is sent on every stopwatch tick | ||
type TickMsg struct { | ||
id int | ||
} | ||
|
||
// StartStopMsg is a message that controls if the stopwatch is running or not | ||
type StartStopMsg struct { | ||
running bool | ||
startDuration time.Duration | ||
} | ||
|
||
// ResetMsg is a message that resets the stopwatch | ||
type ResetMsg struct { | ||
} | ||
|
||
// Model for the stopwatch component | ||
type Model struct { | ||
id int | ||
duration time.Duration | ||
running bool | ||
} | ||
|
||
// New created a new stopwatch with a given interval | ||
func New() Model { | ||
return Model{ | ||
id: nextID(), | ||
duration: 0, | ||
running: false, | ||
} | ||
} | ||
|
||
// Init initiates the stopwatch component | ||
func (m Model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
// Start starts the stopwatch | ||
func (m Model) Start(startDuration time.Duration) tea.Cmd { | ||
return func() tea.Msg { | ||
return StartStopMsg{running: true, startDuration: startDuration} | ||
} | ||
} | ||
|
||
// Stop stops the stopwatch | ||
func (m Model) Stop() tea.Cmd { | ||
return func() tea.Msg { | ||
return StartStopMsg{running: false} | ||
} | ||
} | ||
|
||
// Reset resets the stopwatch | ||
func (m Model) Reset() tea.Cmd { | ||
return func() tea.Msg { | ||
return ResetMsg{} | ||
} | ||
} | ||
|
||
// Update handles the stopwatch tick | ||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case TickMsg: | ||
if msg.id != m.id || !m.running { | ||
return m, nil | ||
} | ||
|
||
m.duration += time.Second | ||
return m, tick(m.id) | ||
|
||
case ResetMsg: | ||
m.duration = 0 | ||
m.running = false | ||
|
||
case StartStopMsg: | ||
if msg.running { | ||
// Start | ||
if m.running { | ||
// Already running | ||
return m, nil | ||
} | ||
|
||
m.id = nextID() | ||
m.duration = msg.startDuration | ||
m.running = true | ||
|
||
return m, tick(m.id) | ||
} | ||
|
||
// Stop | ||
m.running = false | ||
return m, nil | ||
} | ||
return m, nil | ||
} | ||
|
||
// View of the stopwatch component | ||
func (m Model) View() string { | ||
duration := m.duration.Round(time.Second) | ||
|
||
min := int(duration / time.Minute) | ||
sec := int((duration % time.Minute) / time.Second) | ||
|
||
return fmt.Sprintf("%02d:%02d", min, sec) | ||
} | ||
|
||
func tick(id int) tea.Cmd { | ||
return tea.Tick(time.Second, func(_ time.Time) tea.Msg { | ||
return TickMsg{id: id} | ||
}) | ||
} |
Oops, something went wrong.