diff --git a/Makefile b/Makefile
index eb0a2db..c87b9eb 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,7 @@ build:
go build ${BUILDOPTS} -o ${BINARY} ./cmd/${BINARY}
battery-test:
+ rm -rf ${TEST1}
go build ${BUILDOPTS} -o ${TEST1} ./cmd/${TEST1}
## Remove binary with golang compiler' means
diff --git a/cmd/battery-test/main.go b/cmd/battery-test/main.go
index ef47d02..14a9239 100644
--- a/cmd/battery-test/main.go
+++ b/cmd/battery-test/main.go
@@ -11,13 +11,12 @@ import (
// Program entry point.
func main() {
for {
- batteries, err := battery.GetAll()
+ batteries, _ := battery.GetAll()
- if err != nil {
- fmt.Printf("Unable to get battery info: %w", err)
+ for index, battery := range batteries {
+ fmt.Printf("Battery %s: %s\n", index, spew.Sdump(battery))
}
- spew.Dump(batteries)
time.Sleep(1 * time.Second)
}
}
diff --git a/internal/lib/batteries.go b/internal/lib/batteries.go
index 1be34c2..bde6e37 100644
--- a/internal/lib/batteries.go
+++ b/internal/lib/batteries.go
@@ -14,152 +14,156 @@ func (c *MyConfig) UpdateBatteryInfo() {
ch int
status string
Batts string
+ InitialDelay time.Duration = 100 * time.Millisecond
+ LoopIterationDelay time.Duration = 5 * time.Second
)
- for {
- batteries, err := battery.GetAll()
+ Delay := InitialDelay
+ ticker := time.NewTicker(Delay)
- if err != nil {
- Batts = fmt.Sprintf(
- "%s no batt",
+ for range ticker.C {
+ if Delay == InitialDelay {
+ Delay = LoopIterationDelay
+ ticker.Reset(Delay)
+ }
+
+ batteries, _ := battery.GetAll()
+ // In theory, this module should give for each entry its separate err, but in practice it gives
+ // one single err for all entries, so we cannot detemine whist exatly entry errored.
+
+ var (
+ battsInfo string
+ chStr string
+ )
+
+ for i, b := range batteries {
+ switch b.State.Raw {
+ case battery.Charging:
+ status = `▲`
+ case battery.Discharging:
+ status = `▼`
+ case battery.Empty:
+ status = `✘`
+ default:
+ status = `•`
+ }
+
+ // N.B. there can be case when battery is overcharged and shows >100%. It also can indicate that
+ // calibration data is out of date and battery should be re-calibrated.
+ ch = int(math.Round((b.Full - (b.Full - b.Current)) * (100 / b.Full)))
+
+ switch {
+ case ch <= 500 && ch >= 84:
+ if c.Battery.ChargeColor.Full == "" {
+ chStr = fmt.Sprintf(
+ "% 3d%%",
+ c.Battery.Color,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ } else {
+ chStr = fmt.Sprintf(
+ `% 3d%%`,
+ c.Battery.ChargeColor.Full,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ }
+
+ case ch < 85 && ch > 40:
+ if c.Battery.ChargeColor.AlmostFull == "" {
+ chStr = fmt.Sprintf(
+ "% 3d%%",
+ c.Battery.Color,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ } else {
+ chStr = fmt.Sprintf(
+ `% 3d%%`,
+ c.Battery.ChargeColor.AlmostFull,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ }
+
+ case ch <= 40 && ch >= 10:
+ if c.Battery.ChargeColor.AlmostEmpty == "" {
+ chStr = fmt.Sprintf(
+ "% 3d%%",
+ c.Battery.Color,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ } else {
+ chStr = fmt.Sprintf(
+ `% 3d%%`,
+ c.Battery.ChargeColor.AlmostEmpty,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ }
+
+ case ch < 10 && ch >= 0:
+ if c.Battery.ChargeColor.Empty == "" {
+ chStr = fmt.Sprintf(
+ "% 3d%%",
+ c.Battery.Color,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ } else {
+ chStr = fmt.Sprintf(
+ `% 3d%%`,
+ c.Battery.ChargeColor.Empty,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ ch,
+ )
+ }
+
+ default:
+ continue
+ }
+
+ battsInfo += fmt.Sprintf(
+ "%sB%d %s %s",
+ c.Battery.Color,
+ c.Battery.Background,
c.Battery.SymbolFont,
c.Battery.SymbolFontSize,
c.Battery.Symbol,
+ c.Battery.Color,
+ c.Battery.Background,
c.Battery.Font,
c.Battery.FontSize,
+ i,
+ chStr,
+ c.Battery.Color,
+ c.Battery.Background,
+ c.Battery.Font,
+ c.Battery.FontSize,
+ status,
)
- } else {
- var (
- battsInfo string
- chStr string
- )
+ }
- for i, b := range batteries {
- switch b.State.Raw {
- case battery.Charging:
- status = `▲`
- case battery.Discharging:
- status = `▼`
- case battery.Empty:
- status = `✘`
- default:
- status = `•`
- }
-
- // N.B. there can be case when battery is overcharged and shows >100%. It also can indicate that
- // calibration data is out of date and battery should be re-calibrated.
- ch = int(math.Round((b.Full - (b.Full - b.Current)) * (100 / b.Full)))
-
- switch {
- case ch > 85:
- if c.Battery.ChargeColor.Full == "" {
- chStr = fmt.Sprintf(
- "% 3d%%",
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- } else {
- chStr = fmt.Sprintf(
- `% 3d%%`,
- c.Battery.ChargeColor.Full,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- }
-
- case ch < 85 && ch > 40:
- if c.Battery.ChargeColor.AlmostFull == "" {
- chStr = fmt.Sprintf(
- "% 3d%%",
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- } else {
- chStr = fmt.Sprintf(
- `% 3d%%`,
- c.Battery.ChargeColor.AlmostFull,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- }
-
- case ch <= 40 && ch >= 10:
- if c.Battery.ChargeColor.AlmostEmpty == "" {
- chStr = fmt.Sprintf(
- "% 3d%%",
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- } else {
- chStr = fmt.Sprintf(
- `% 3d%%`,
- c.Battery.ChargeColor.AlmostEmpty,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- }
-
- case ch < 10:
- if c.Battery.ChargeColor.Empty == "" {
- chStr = fmt.Sprintf(
- "% 3d%%",
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- } else {
- chStr = fmt.Sprintf(
- `% 3d%%`,
- c.Battery.ChargeColor.Empty,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- ch,
- )
- }
- }
-
- battsInfo += fmt.Sprintf(
- "%sB%d %s %s",
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.SymbolFont,
- c.Battery.SymbolFontSize,
- c.Battery.Symbol,
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- i,
- chStr,
- c.Battery.Color,
- c.Battery.Background,
- c.Battery.Font,
- c.Battery.FontSize,
- status,
- )
- }
-
- if battsInfo != "" {
- Batts = battsInfo
- }
+ if battsInfo != "" {
+ Batts = battsInfo
}
if c.Values.BatteryString != Batts {