Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

battery: Add {cycles}, {health} format replacements #3130

Merged
merged 10 commits into from
Apr 18, 2024
2 changes: 1 addition & 1 deletion include/modules/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Battery : public ALabel {
void refreshBatteries();
void worker();
const std::string getAdapterStatus(uint8_t capacity) const;
const std::tuple<uint8_t, float, std::string, float> getInfos();
std::tuple<uint8_t, float, std::string, float, uint16_t, float> getInfos();
const std::string formatTimeRemaining(float hoursRemaining);
void setBarClass(std::string&);

Expand Down
4 changes: 4 additions & 0 deletions man/waybar-battery.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ 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)*

*{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*.
Expand Down
40 changes: 33 additions & 7 deletions src/modules/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
return false;
}

const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::getInfos() {
std::tuple<uint8_t, float, std::string, float, uint16_t, float> waybar::modules::Battery::getInfos() {

Check warning on line 184 in src/modules/battery.cpp

View workflow job for this annotation

GitHub Actions / build

src/modules/battery.cpp:184:91 [readability-function-cognitive-complexity]

function 'getInfos' has cognitive complexity of 224 (threshold 25)
std::lock_guard<std::mutex> guard(battery_list_mutex_);

try {
Expand Down Expand Up @@ -234,7 +234,7 @@
}

// 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
Expand All @@ -252,6 +252,10 @@
uint32_t time_to_full_now = 0;
bool time_to_full_now_exists = false;

uint32_t largestDesignCapacity = 0;
uint16_t mainBatCycleCount = 0;
float mainBatHealthPercent = 0.0F;

std::string status = "Unknown";
for (auto const& item : batteries_) {
auto bat = item.first;
Expand Down Expand Up @@ -353,6 +357,25 @@
std::ifstream(bat / "energy_full_design") >> energy_full_design;
}

uint16_t cycleCount = 0;
if (fs::exists(bat / "cycle_count")) {
std::ifstream(bat / "cycle_count") >> cycleCount;
}
if (charge_full_design >= largestDesignCapacity) {
largestDesignCapacity = charge_full_design;

if (cycleCount > mainBatCycleCount) {
mainBatCycleCount = cycleCount;
}

if (charge_full_exists && charge_full_design_exists) {
float batHealthPercent = ((float)charge_full / charge_full_design) * 100;
if (mainBatHealthPercent == 0.0f || batHealthPercent < mainBatHealthPercent) {

Check warning on line 373 in src/modules/battery.cpp

View workflow job for this annotation

GitHub Actions / build

src/modules/battery.cpp:373:39 [readability-uppercase-literal-suffix]

floating point literal has suffix 'f', which is not uppercase
mainBatHealthPercent = batHealthPercent;
}
}
}

if (!voltage_now_exists) {
if (power_now_exists && current_now_exists && current_now != 0) {
voltage_now_exists = true;
Expand Down Expand Up @@ -573,11 +596,11 @@
// 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, mainBatCycleCount, mainBatHealthPercent};
#endif
} catch (const std::exception& e) {
spdlog::error("Battery: {}", e.what());
return {0, 0, "Unknown", 0};
return {0, 0, "Unknown", 0, 0, 0.0f};

Check warning on line 603 in src/modules/battery.cpp

View workflow job for this annotation

GitHub Actions / build

src/modules/battery.cpp:603:36 [readability-uppercase-literal-suffix]

floating point literal has suffix 'f', which is not uppercase
}
}

Expand Down Expand Up @@ -626,14 +649,14 @@
fmt::arg("m", zero_pad_minutes));
}

auto waybar::modules::Battery::update() -> void {

Check warning on line 652 in src/modules/battery.cpp

View workflow job for this annotation

GitHub Actions / build

src/modules/battery.cpp:652:32 [readability-function-cognitive-complexity]

function 'update' has cognitive complexity of 26 (threshold 25)
#if defined(__linux__)
if (batteries_.empty()) {
event_box_.hide();
return;
}
#endif
auto [capacity, time_remaining, status, power] = getInfos();
auto [capacity, time_remaining, status, power, cycles, health] = getInfos();
if (status == "Unknown") {
status = getAdapterStatus(capacity);
}
Expand Down Expand Up @@ -666,7 +689,9 @@
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),
fmt::arg("health", fmt::format("{:.3}", health))));
}
if (!old_status_.empty()) {
label_.get_style_context()->remove_class(old_status_);
Expand All @@ -687,7 +712,8 @@
auto icons = std::vector<std::string>{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();
Expand Down
Loading