From 43511992d94ef5524c8a251373a70fc9eb1ff970 Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:00:50 +0200 Subject: [PATCH 01/10] feat(battery): Add {cycles} format replacement --- include/modules/battery.hpp | 2 +- man/waybar-battery.5.scd | 2 ++ src/modules/battery.cpp | 27 +++++++++++++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 7955e598..6f09043b 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -32,7 +32,7 @@ class Battery : public ALabel { void refreshBatteries(); void worker(); const std::string getAdapterStatus(uint8_t capacity) const; - const std::tuple getInfos(); + const std::tuple getInfos(); const std::string formatTimeRemaining(float hoursRemaining); void setBarClass(std::string&); diff --git a/man/waybar-battery.5.scd b/man/waybar-battery.5.scd index e359ea2e..284803b0 100644 --- a/man/waybar-battery.5.scd +++ b/man/waybar-battery.5.scd @@ -119,6 +119,8 @@ The *battery* module displays the current capacity and state (eg. charging) of y *{time}*: Estimate of time until full or empty. Note that this is based on the power draw at the last refresh time, not an average. +*{cycles}*: Amount of charge cycles the highest-capacity battery has seen. *(Linux only)* + # TIME FORMAT The *battery* module allows you to define how time should be formatted via *format-time*. diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 9003db6e..06df5db2 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -181,7 +181,7 @@ static bool status_gt(const std::string& a, const std::string& b) { return false; } -const std::tuple waybar::modules::Battery::getInfos() { +const std::tuple waybar::modules::Battery::getInfos() { std::lock_guard guard(battery_list_mutex_); try { @@ -252,6 +252,9 @@ const std::tuple waybar::modules::Battery::g uint32_t time_to_full_now = 0; bool time_to_full_now_exists = false; + uint32_t largest_design_capacity = 0; + uint16_t main_bat_cycle_count = 0; + std::string status = "Unknown"; for (auto const& item : batteries_) { auto bat = item.first; @@ -353,6 +356,16 @@ const std::tuple waybar::modules::Battery::g std::ifstream(bat / "energy_full_design") >> energy_full_design; } + bool is_main_battery = charge_full_design >= largest_design_capacity; + uint16_t cycle_count = 0; + if (fs::exists(bat / "cycle_count")) { + std::ifstream(bat / "cycle_count") >> cycle_count; + } + if (is_main_battery && (cycle_count > main_bat_cycle_count)) { + largest_design_capacity = charge_full_design; + main_bat_cycle_count = cycle_count; + } + if (!voltage_now_exists) { if (power_now_exists && current_now_exists && current_now != 0) { voltage_now_exists = true; @@ -573,11 +586,11 @@ const std::tuple waybar::modules::Battery::g // still charging but not yet done if (cap == 100 && status == "Charging") status = "Full"; - return {cap, time_remaining, status, total_power / 1e6}; + return {cap, time_remaining, status, total_power / 1e6, main_bat_cycle_count}; #endif } catch (const std::exception& e) { spdlog::error("Battery: {}", e.what()); - return {0, 0, "Unknown", 0}; + return {0, 0, "Unknown", 0, 0}; } } @@ -633,7 +646,7 @@ auto waybar::modules::Battery::update() -> void { return; } #endif - auto [capacity, time_remaining, status, power] = getInfos(); + auto [capacity, time_remaining, status, power, cycles] = getInfos(); if (status == "Unknown") { status = getAdapterStatus(capacity); } @@ -666,7 +679,8 @@ auto waybar::modules::Battery::update() -> void { label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format), fmt::arg("timeTo", tooltip_text_default), fmt::arg("power", power), fmt::arg("capacity", capacity), - fmt::arg("time", time_remaining_formatted))); + fmt::arg("time", time_remaining_formatted), + fmt::arg("cycles", cycles))); } if (!old_status_.empty()) { label_.get_style_context()->remove_class(old_status_); @@ -687,7 +701,8 @@ auto waybar::modules::Battery::update() -> void { auto icons = std::vector{status + "-" + state, status, state}; label_.set_markup(fmt::format( fmt::runtime(format), fmt::arg("capacity", capacity), fmt::arg("power", power), - fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", time_remaining_formatted))); + fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", time_remaining_formatted), + fmt::arg("cycles", cycles))); } // Call parent update ALabel::update(); From 7f1e623f77a6b99e38a5cfc2839d0dcf59f2c393 Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:30:47 +0200 Subject: [PATCH 02/10] style: Refactor battery cycle count choosing --- src/modules/battery.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 06df5db2..cc6595dc 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -361,9 +361,12 @@ const std::tuple waybar::modules:: if (fs::exists(bat / "cycle_count")) { std::ifstream(bat / "cycle_count") >> cycle_count; } - if (is_main_battery && (cycle_count > main_bat_cycle_count)) { + if (is_main_battery) { largest_design_capacity = charge_full_design; - main_bat_cycle_count = cycle_count; + + if (cycle_count > main_bat_cycle_count) { + main_bat_cycle_count = cycle_count; + } } if (!voltage_now_exists) { From a59593fde1ccb7caef46f69a1d1fa7a9fc13f7fb Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:19:55 +0200 Subject: [PATCH 03/10] feat(battery): Add {health} format replacement --- include/modules/battery.hpp | 2 +- man/waybar-battery.5.scd | 2 ++ src/modules/battery.cpp | 22 +++++++++++++++++----- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 6f09043b..0468bff1 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -32,7 +32,7 @@ class Battery : public ALabel { void refreshBatteries(); void worker(); const std::string getAdapterStatus(uint8_t capacity) const; - const std::tuple getInfos(); + const std::tuple getInfos(); const std::string formatTimeRemaining(float hoursRemaining); void setBarClass(std::string&); diff --git a/man/waybar-battery.5.scd b/man/waybar-battery.5.scd index 284803b0..25c7caca 100644 --- a/man/waybar-battery.5.scd +++ b/man/waybar-battery.5.scd @@ -121,6 +121,8 @@ The *battery* module displays the current capacity and state (eg. charging) of y *{cycles}*: Amount of charge cycles the highest-capacity battery has seen. *(Linux only)* +*{health}*: The percentage of the highest-capacity battery's original maximum charge it can still hold. + # TIME FORMAT The *battery* module allows you to define how time should be formatted via *format-time*. diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index cc6595dc..6d3caa27 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -181,7 +181,7 @@ static bool status_gt(const std::string& a, const std::string& b) { return false; } -const std::tuple waybar::modules::Battery::getInfos() { +const std::tuple waybar::modules::Battery::getInfos() { std::lock_guard guard(battery_list_mutex_); try { @@ -254,6 +254,7 @@ const std::tuple waybar::modules:: uint32_t largest_design_capacity = 0; uint16_t main_bat_cycle_count = 0; + float main_bat_health_percent = 0.0f; std::string status = "Unknown"; for (auto const& item : batteries_) { @@ -367,6 +368,16 @@ const std::tuple waybar::modules:: if (cycle_count > main_bat_cycle_count) { main_bat_cycle_count = cycle_count; } + + std::string name; + std::ifstream(bat / "model_name") >> name; + spdlog::info("{} | full: {}, full_design: {}", name, energy_full, energy_full_design); + if (charge_full_exists && charge_full_design_exists) { + float bat_health_percent = ((float)charge_full_design / charge_full) * 100; + if (main_bat_health_percent == 0.0f || bat_health_percent < main_bat_health_percent) { + main_bat_health_percent = bat_health_percent; + } + } } if (!voltage_now_exists) { @@ -589,11 +600,11 @@ const std::tuple waybar::modules:: // still charging but not yet done if (cap == 100 && status == "Charging") status = "Full"; - return {cap, time_remaining, status, total_power / 1e6, main_bat_cycle_count}; + return {cap, time_remaining, status, total_power / 1e6, main_bat_cycle_count, main_bat_health_percent}; #endif } catch (const std::exception& e) { spdlog::error("Battery: {}", e.what()); - return {0, 0, "Unknown", 0, 0}; + return {0, 0, "Unknown", 0, 0, 0.0f}; } } @@ -649,7 +660,7 @@ auto waybar::modules::Battery::update() -> void { return; } #endif - auto [capacity, time_remaining, status, power, cycles] = getInfos(); + auto [capacity, time_remaining, status, power, cycles, health] = getInfos(); if (status == "Unknown") { status = getAdapterStatus(capacity); } @@ -683,7 +694,8 @@ auto waybar::modules::Battery::update() -> void { fmt::arg("timeTo", tooltip_text_default), fmt::arg("power", power), fmt::arg("capacity", capacity), fmt::arg("time", time_remaining_formatted), - fmt::arg("cycles", cycles))); + fmt::arg("cycles", cycles), + fmt::arg("health", fmt::format("{:.3}", health)))); } if (!old_status_.empty()) { label_.get_style_context()->remove_class(old_status_); From 805faa47e63686d359843535e4557358f18c2c0e Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:48:03 +0200 Subject: [PATCH 04/10] style: Remove debug output Oops --- src/modules/battery.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 6d3caa27..e65a2dcf 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -369,9 +369,6 @@ const std::tuple waybar::mo main_bat_cycle_count = cycle_count; } - std::string name; - std::ifstream(bat / "model_name") >> name; - spdlog::info("{} | full: {}, full_design: {}", name, energy_full, energy_full_design); if (charge_full_exists && charge_full_design_exists) { float bat_health_percent = ((float)charge_full_design / charge_full) * 100; if (main_bat_health_percent == 0.0f || bat_health_percent < main_bat_health_percent) { From 24690248dbdb33323de3b99c51f5706bf8652736 Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Thu, 11 Apr 2024 02:40:04 +0200 Subject: [PATCH 05/10] fix: Calculate battery health the right way around I even did this originally, then got confused when my battery in particular showed 102% and, instead of checking the values I calculate with, just decided to do the stupid thing and do maths the wrong around --- src/modules/battery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index e65a2dcf..df3712a6 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -370,7 +370,7 @@ const std::tuple waybar::mo } if (charge_full_exists && charge_full_design_exists) { - float bat_health_percent = ((float)charge_full_design / charge_full) * 100; + float bat_health_percent = ((float)charge_full / charge_full_design) * 100; if (main_bat_health_percent == 0.0f || bat_health_percent < main_bat_health_percent) { main_bat_health_percent = bat_health_percent; } From 421ba6e31a6c1d454a6b6f5ab9e61209682b1418 Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:48:54 +0200 Subject: [PATCH 06/10] fix: Add dummy information for battery cycles,health on FreeBSD --- src/modules/battery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index df3712a6..4facaaca 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -234,7 +234,7 @@ const std::tuple waybar::mo } // spdlog::info("{} {} {} {}", capacity,time,status,rate); - return {capacity, time / 60.0, status, rate}; + return {capacity, time / 60.0, status, rate, 0, 0.0F}; #elif defined(__linux__) uint32_t total_power = 0; // μW From 986b348bc721fb9d78221a5dc720556e08a8c9fb Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:11:14 +0200 Subject: [PATCH 07/10] style: Change new variables to camelCase --- src/modules/battery.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 4facaaca..d2a24457 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -252,9 +252,9 @@ const std::tuple waybar::mo uint32_t time_to_full_now = 0; bool time_to_full_now_exists = false; - uint32_t largest_design_capacity = 0; - uint16_t main_bat_cycle_count = 0; - float main_bat_health_percent = 0.0f; + uint32_t largestDesignCapacity = 0; + uint16_t mainBatCycleCount = 0; + float mainBatHealthPercent = 0.0F; std::string status = "Unknown"; for (auto const& item : batteries_) { @@ -357,22 +357,22 @@ const std::tuple waybar::mo std::ifstream(bat / "energy_full_design") >> energy_full_design; } - bool is_main_battery = charge_full_design >= largest_design_capacity; + bool is_main_battery = charge_full_design >= largestDesignCapacity; uint16_t cycle_count = 0; if (fs::exists(bat / "cycle_count")) { std::ifstream(bat / "cycle_count") >> cycle_count; } if (is_main_battery) { - largest_design_capacity = charge_full_design; + largestDesignCapacity = charge_full_design; - if (cycle_count > main_bat_cycle_count) { - main_bat_cycle_count = cycle_count; + if (cycle_count > mainBatCycleCount) { + mainBatCycleCount = cycle_count; } if (charge_full_exists && charge_full_design_exists) { float bat_health_percent = ((float)charge_full / charge_full_design) * 100; - if (main_bat_health_percent == 0.0f || bat_health_percent < main_bat_health_percent) { - main_bat_health_percent = bat_health_percent; + if (mainBatHealthPercent == 0.0f || bat_health_percent < main_bat_health_percent) { + mainBatHealthPercent = bat_health_percent; } } } @@ -597,7 +597,7 @@ const std::tuple waybar::mo // still charging but not yet done if (cap == 100 && status == "Charging") status = "Full"; - return {cap, time_remaining, status, total_power / 1e6, main_bat_cycle_count, main_bat_health_percent}; + return {cap, time_remaining, status, total_power / 1e6, mainBatCycleCount, mainBatHealthPercent}; #endif } catch (const std::exception& e) { spdlog::error("Battery: {}", e.what()); From da47c94480459085ef1214639ae88d2bbe3f694c Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:18:50 +0200 Subject: [PATCH 08/10] fix: Also use camelCase for usages of new vars --- src/modules/battery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index d2a24457..f9f3084e 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -371,7 +371,7 @@ const std::tuple waybar::mo if (charge_full_exists && charge_full_design_exists) { float bat_health_percent = ((float)charge_full / charge_full_design) * 100; - if (mainBatHealthPercent == 0.0f || bat_health_percent < main_bat_health_percent) { + if (mainBatHealthPercent == 0.0f || bat_health_percent < mainBatHealthPercent) { mainBatHealthPercent = bat_health_percent; } } From 3d54a6002d9521ab835570522aa514e2816d2477 Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:19:54 +0200 Subject: [PATCH 09/10] style: Remove superfluous 'const' on getInfo() was here before, but is an easy fix for a clang-tidy warning --- include/modules/battery.hpp | 2 +- src/modules/battery.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 0468bff1..8e1a2ad2 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -32,7 +32,7 @@ class Battery : public ALabel { void refreshBatteries(); void worker(); const std::string getAdapterStatus(uint8_t capacity) const; - const std::tuple getInfos(); + std::tuple getInfos(); const std::string formatTimeRemaining(float hoursRemaining); void setBarClass(std::string&); diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index f9f3084e..d4bd3e2d 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -181,7 +181,7 @@ static bool status_gt(const std::string& a, const std::string& b) { return false; } -const std::tuple waybar::modules::Battery::getInfos() { +std::tuple waybar::modules::Battery::getInfos() { std::lock_guard guard(battery_list_mutex_); try { From 67bf98a93e60c6db4f24f54d7505fe6a9f2dfca7 Mon Sep 17 00:00:00 2001 From: Kiri <56218513+kiriDevs@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:46:35 +0200 Subject: [PATCH 10/10] style: Change more var names to camelCase --- src/modules/battery.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index d4bd3e2d..69f1b84d 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -357,22 +357,21 @@ std::tuple waybar::modules: std::ifstream(bat / "energy_full_design") >> energy_full_design; } - bool is_main_battery = charge_full_design >= largestDesignCapacity; - uint16_t cycle_count = 0; + uint16_t cycleCount = 0; if (fs::exists(bat / "cycle_count")) { - std::ifstream(bat / "cycle_count") >> cycle_count; + std::ifstream(bat / "cycle_count") >> cycleCount; } - if (is_main_battery) { + if (charge_full_design >= largestDesignCapacity) { largestDesignCapacity = charge_full_design; - if (cycle_count > mainBatCycleCount) { - mainBatCycleCount = cycle_count; + if (cycleCount > mainBatCycleCount) { + mainBatCycleCount = cycleCount; } if (charge_full_exists && charge_full_design_exists) { - float bat_health_percent = ((float)charge_full / charge_full_design) * 100; - if (mainBatHealthPercent == 0.0f || bat_health_percent < mainBatHealthPercent) { - mainBatHealthPercent = bat_health_percent; + float batHealthPercent = ((float)charge_full / charge_full_design) * 100; + if (mainBatHealthPercent == 0.0f || batHealthPercent < mainBatHealthPercent) { + mainBatHealthPercent = batHealthPercent; } } }