feat(battery): Add {cycles} format replacement

This commit is contained in:
Kiri 2024-04-10 17:00:50 +02:00
parent 42dc9cb85f
commit 43511992d9
No known key found for this signature in database
GPG Key ID: F1BEE619CFF29825
3 changed files with 24 additions and 7 deletions

View File

@ -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();
const std::tuple<uint8_t, float, std::string, float, uint16_t> getInfos();
const std::string formatTimeRemaining(float hoursRemaining);
void setBarClass(std::string&);

View File

@ -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*.

View File

@ -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() {
const std::tuple<uint8_t, float, std::string, float, uint16_t> waybar::modules::Battery::getInfos() {
std::lock_guard<std::mutex> guard(battery_list_mutex_);
try {
@ -252,6 +252,9 @@ 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 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<uint8_t, float, std::string, float> 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<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, 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<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();