Skip to content

Commit

Permalink
Merge pull request #3130 from kiriDevs/kiridevs/battery-cycles
Browse files Browse the repository at this point in the history
battery: Add {cycles}, {health} format replacements
  • Loading branch information
Alexays committed Apr 18, 2024
2 parents dd092a5 + 67bf98a commit f26efae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
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 @@ static bool status_gt(const std::string& a, const std::string& b) {
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() {
std::lock_guard<std::mutex> guard(battery_list_mutex_);

try {
Expand Down Expand Up @@ -234,7 +234,7 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
}

// 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 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
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 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
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) {
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 @@ const std::tuple<uint8_t, float, std::string, float> 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, 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};
}
}

Expand Down Expand Up @@ -633,7 +656,7 @@ auto waybar::modules::Battery::update() -> void {
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 @@ 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),
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 waybar::modules::Battery::update() -> void {
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

0 comments on commit f26efae

Please sign in to comment.