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_);