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
@@ -149,6 +154,7 @@ func (w *TimerWidget) Update() error {
149154
150155type Timespan time.Duration
151156
157+ // Format returns the formatted version of the timespan.
152158func (t Timespan ) Format (format string , adaptive bool ) string {
153159 formatStr := format
154160 tm := map [string ]string {
@@ -193,13 +199,15 @@ func (t Timespan) Format(format string, adaptive bool) string {
193199 return timeStr
194200}
195201
202+ // ReplaceAll does a replacement with all entries of a map.
196203func ReplaceAll (str string , tm map [string ]string ) string {
197204 for k , v := range tm {
198205 str = strings .ReplaceAll (str , k , v )
199206 }
200207 return str
201208}
202209
210+ // TriggerAction updates the timer state.
203211func (w * TimerWidget ) TriggerAction (hold bool ) {
204212 if hold {
205213 if w .data .IsPaused () {
@@ -211,7 +219,7 @@ func (w *TimerWidget) TriggerAction(hold bool) {
211219 if w .data .IsRunning () {
212220 w .data .pausedTime = time .Now ()
213221 } else if w .data .IsPaused () && w .data .HasDeadline () {
214- pausedDuration := time .Now (). Sub (w .data .pausedTime )
222+ pausedDuration := time .Since (w .data .pausedTime )
215223 w .data .startTime = w .data .startTime .Add (pausedDuration )
216224 w .data .pausedTime = time.Time {}
217225 } else {
0 commit comments