From efc7c6c4aec5c20a6e2f08864298780dcae3aef3 Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@lyda.ie>
Date: Fri, 2 Dec 2022 23:34:39 +0000
Subject: [PATCH] More battery tweaks.

---
 modules/battery.go | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/modules/battery.go b/modules/battery.go
index c3d491a..b3c249a 100644
--- a/modules/battery.go
+++ b/modules/battery.go
@@ -103,9 +103,13 @@ func (b *BatteryMod) Render() string {
 		delta := now.Sub(b.battStats[0].point)
 		if delta >= (60 * time.Second) {
 			discharge := math.Abs(batt.Current - b.battStats[0].charge)
-			timeLeft := time.Duration(batt.Current/discharge) * delta
-			left = fmt.Sprintf("%dh%dm", int64(timeLeft.Hours()), int64(timeLeft.Minutes()))
-			b.battStats = b.battStats[1:]
+			if discharge > 0 {
+				timeLeft := time.Duration(batt.Current/discharge) * delta
+				left = fmt.Sprintf("%dh%dm", int64(timeLeft.Hours()), int64(timeLeft.Minutes()-(timeLeft.Hours()*60)))
+				b.battStats = b.battStats[1:]
+			} else {
+				left = "0h0m"
+			}
 		}
 	}
 	b.battStats = append(b.battStats, battTS{
-- 
GitLab