From 418f3c21651305f2c091a9e7d450c757a7fe9985 Mon Sep 17 00:00:00 2001 From: Moritz Biering Date: Tue, 8 Feb 2022 15:05:40 +0100 Subject: [PATCH] Resolve conflict and fixes according to linter --- decks/main.deck | 13 ------------- widget_timer.go | 15 ++++++++++++--- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/decks/main.deck b/decks/main.deck index a7f1ef0..c73c985 100644 --- a/decks/main.deck +++ b/decks/main.deck @@ -60,19 +60,6 @@ [keys.action_hold] keycode = "Mute" -[[keys]] - index = 7 - [keys.widget] - id = "timer" - [keys.widget.config] - times = "5s;10m;30m;1h5m" # optional - format = "%Hh;%Im;%Ss" - font = "bold;regular;thin" # optional - #color = "#fefefe" # optional - underflow = "false" # optional - underflowColor = "#ff0000;#ff0000;#ff0000" # optional - - [[keys]] index = 8 [keys.widget] diff --git a/widget_timer.go b/widget_timer.go index 2ba1cdf..f4155e8 100644 --- a/widget_timer.go +++ b/widget_timer.go @@ -7,7 +7,7 @@ import ( "time" ) -// TimerWidget is a widget displaying a timer +// TimerWidget is a widget displaying a timer. type TimerWidget struct { *BaseWidget @@ -26,29 +26,34 @@ type TimerWidget struct { data TimerData } +// TimerData represents the current state of the timer. type TimerData struct { startTime time.Time pausedTime time.Time } +// IsPaused returns whether the timer is paused. func (d *TimerData) IsPaused() bool { return !d.pausedTime.IsZero() } +// IsRunning returns whether the timer is running. func (d *TimerData) IsRunning() bool { return !d.IsPaused() && d.HasDeadline() } +// HasDeadline returns whether the start time is set. func (d *TimerData) HasDeadline() bool { return !d.startTime.IsZero() } +// Clear resets the state of the timer. func (d *TimerData) Clear() { d.startTime = time.Time{} d.pausedTime = time.Time{} } -// NewTimerWidget returns a new TimerWidget +// NewTimerWidget returns a new TimerWidget. func NewTimerWidget(bw *BaseWidget, opts WidgetConfig) *TimerWidget { bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second/2) @@ -147,8 +152,10 @@ func (w *TimerWidget) Update() error { return w.render(w.dev, img) } +// Timespan represents the duration between two events. type Timespan time.Duration +// Format returns the formatted version of the timespan. func (t Timespan) Format(format string, adaptive bool) string { formatStr := format tm := map[string]string{ @@ -193,6 +200,7 @@ func (t Timespan) Format(format string, adaptive bool) string { return timeStr } +// ReplaceAll does a replacement with all entries of a map. func ReplaceAll(str string, tm map[string]string) string { for k, v := range tm { str = strings.ReplaceAll(str, k, v) @@ -200,6 +208,7 @@ func ReplaceAll(str string, tm map[string]string) string { return str } +// TriggerAction updates the timer state. func (w *TimerWidget) TriggerAction(hold bool) { if hold { if w.data.IsPaused() { @@ -211,7 +220,7 @@ func (w *TimerWidget) TriggerAction(hold bool) { if w.data.IsRunning() { w.data.pausedTime = time.Now() } else if w.data.IsPaused() && w.data.HasDeadline() { - pausedDuration := time.Now().Sub(w.data.pausedTime) + pausedDuration := time.Since(w.data.pausedTime) w.data.startTime = w.data.startTime.Add(pausedDuration) w.data.pausedTime = time.Time{} } else {