77 "time"
88)
99
10- // TimerWidget is a widget displaying a timer
10+ // TimerWidget is a widget displaying a timer.
1111type TimerWidget struct {
1212 * BaseWidget
1313
@@ -26,29 +26,34 @@ type TimerWidget struct {
2626 data TimerData
2727}
2828
29+ // TimerData represents the current state of the timer.
2930type TimerData struct {
3031 startTime time.Time
3132 pausedTime time.Time
3233}
3334
35+ // IsPaused returns whether the timer is paused.
3436func (d * TimerData ) IsPaused () bool {
3537 return ! d .pausedTime .IsZero ()
3638}
3739
40+ // IsRunning returns whether the timer is running.
3841func (d * TimerData ) IsRunning () bool {
3942 return ! d .IsPaused () && d .HasDeadline ()
4043}
4144
45+ // HasDeadline returns whether the start time is set.
4246func (d * TimerData ) HasDeadline () bool {
4347 return ! d .startTime .IsZero ()
4448}
4549
50+ // Clear resets the state of the timer.
4651func (d * TimerData ) Clear () {
4752 d .startTime = time.Time {}
4853 d .pausedTime = time.Time {}
4954}
5055
51- // NewTimerWidget returns a new TimerWidget
56+ // NewTimerWidget returns a new TimerWidget.
5257func NewTimerWidget (bw * BaseWidget , opts WidgetConfig ) * TimerWidget {
5358 bw .setInterval (time .Duration (opts .Interval )* time .Millisecond , time .Second / 2 )
5459
@@ -147,8 +152,10 @@ func (w *TimerWidget) Update() error {
147152 return w .render (w .dev , img )
148153}
149154
155+ // Timespan represents the duration between two events.
150156type Timespan time.Duration
151157
158+ // Format returns the formatted version of the timespan.
152159func (t Timespan ) Format (format string , adaptive bool ) string {
153160 formatStr := format
154161 tm := map [string ]string {
@@ -193,13 +200,15 @@ func (t Timespan) Format(format string, adaptive bool) string {
193200 return timeStr
194201}
195202
203+ // ReplaceAll does a replacement with all entries of a map.
196204func ReplaceAll (str string , tm map [string ]string ) string {
197205 for k , v := range tm {
198206 str = strings .ReplaceAll (str , k , v )
199207 }
200208 return str
201209}
202210
211+ // TriggerAction updates the timer state.
203212func (w * TimerWidget ) TriggerAction (hold bool ) {
204213 if hold {
205214 if w .data .IsPaused () {
@@ -211,7 +220,7 @@ func (w *TimerWidget) TriggerAction(hold bool) {
211220 if w .data .IsRunning () {
212221 w .data .pausedTime = time .Now ()
213222 } else if w .data .IsPaused () && w .data .HasDeadline () {
214- pausedDuration := time .Now (). Sub (w .data .pausedTime )
223+ pausedDuration := time .Since (w .data .pausedTime )
215224 w .data .startTime = w .data .startTime .Add (pausedDuration )
216225 w .data .pausedTime = time.Time {}
217226 } else {
0 commit comments