From fcac0b9c26a6ffd4118c3129e27ae59a82015133 Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@lyda.ie>
Date: Fri, 2 Dec 2022 23:18:18 +0000
Subject: [PATCH] Tweaks to the battery module.  Still not right

---
 bar/clicks.go      | 17 ++++++++++++++++-
 config/config.go   |  4 ++++
 modules/battery.go | 10 +++++-----
 modules/modules.go |  4 ++++
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/bar/clicks.go b/bar/clicks.go
index 896a76b..bd51031 100644
--- a/bar/clicks.go
+++ b/bar/clicks.go
@@ -12,7 +12,22 @@ import (
 	"gopkg.in/yaml.v2"
 )
 
-//{"button":1,"modifiers":[],"x":1952,"y":1144,"relative_x":62,"relative_y":14,"width":112,"height":22}
+/*
+An example click:
+
+  {
+		"name":       "thing",
+    "button":           1,
+    "modifiers":       [],
+    "x":             1952,
+    "y":             1144,
+    "relative_x":      62,
+    "relative_y":      14,
+    "width":          112,
+    "height":          22
+  }
+*/
+
 func handleCommands(clicks chan modules.Click) {
 	logger, _ := os.Create("/tmp/i3going-on.log")
 
diff --git a/config/config.go b/config/config.go
index bb06e38..770d596 100644
--- a/config/config.go
+++ b/config/config.go
@@ -36,6 +36,10 @@ func ReadConfig(configFile string) *Config {
 				modules.NewErrorModule("decode", err),
 			},
 		}
+	} else {
+		for _, module := range cfg.Modules {
+			module.SetDefaults()
+		}
 	}
 
 	return cfg
diff --git a/modules/battery.go b/modules/battery.go
index 4ea57cd..c3d491a 100644
--- a/modules/battery.go
+++ b/modules/battery.go
@@ -100,10 +100,6 @@ func (b *BatteryMod) Render() string {
 	}
 	left := "(?)"
 	if len(b.battStats) >= 1 {
-		b.battStats = append(b.battStats, battTS{
-			charge: batt.Current,
-			point:  now,
-		})
 		delta := now.Sub(b.battStats[0].point)
 		if delta >= (60 * time.Second) {
 			discharge := math.Abs(batt.Current - b.battStats[0].charge)
@@ -112,6 +108,10 @@ func (b *BatteryMod) Render() string {
 			b.battStats = b.battStats[1:]
 		}
 	}
+	b.battStats = append(b.battStats, battTS{
+		charge: batt.Current,
+		point:  now,
+	})
 
 	states := [...]string{
 		battery.Unknown:     b.StatusUnk,
@@ -122,7 +122,7 @@ func (b *BatteryMod) Render() string {
 	}
 
 	color := b.ColorOK
-	percent := batt.Current / batt.Full
+	percent := 100 * (batt.Current / batt.Full)
 	if percent < 20 {
 		if percent < 10 {
 			color = b.Color10
diff --git a/modules/modules.go b/modules/modules.go
index 2322c63..249210e 100644
--- a/modules/modules.go
+++ b/modules/modules.go
@@ -60,3 +60,7 @@ func (m *Module) UnmarshalYAML(node *yaml.Node) error {
 func (m *Module) Render() string {
 	return m.Params.Render()
 }
+
+func (m *Module) SetDefaults() {
+	m.Params.SetDefaults()
+}
-- 
GitLab