fix(battery): proper handle multiple

pull/1/head
Alexis 2018-08-09 11:21:08 +02:00
parent 4659f04454
commit 4cec0f390b
1 changed files with 14 additions and 10 deletions

View File

@ -4,8 +4,8 @@ waybar::modules::Battery::Battery()
{ {
try { try {
for (auto &node : fs::directory_iterator(_data_dir)) { for (auto &node : fs::directory_iterator(_data_dir)) {
if (fs::is_directory(node) && fs::exists(node / "charge_now") && if (fs::is_directory(node) && fs::exists(node / "capacity")
fs::exists(node / "charge_full")) { && fs::exists(node / "status")) {
_batteries.push_back(node); _batteries.push_back(node);
} }
} }
@ -24,20 +24,24 @@ waybar::modules::Battery::Battery()
auto waybar::modules::Battery::update() -> void auto waybar::modules::Battery::update() -> void
{ {
try { try {
int total = 0;
bool charging = false;
for (auto &bat : _batteries) { for (auto &bat : _batteries) {
int full, now; int capacity;
std::string status; std::string status;
std::ifstream(bat / "charge_now") >> now; std::ifstream(bat / "capacity") >> capacity;
std::ifstream(bat / "charge_full") >> full; total += capacity;
std::ifstream(bat / "status") >> status; std::ifstream(bat / "status") >> status;
if (status == "Charging") { if (status == "Charging") {
charging = true;
}
}
if (charging == true) {
_label.get_style_context()->add_class("charging"); _label.get_style_context()->add_class("charging");
} else { } else {
_label.get_style_context()->remove_class("charging"); _label.get_style_context()->remove_class("charging");
} }
int pct = float(now) / float(full) * 100.f; _label.set_text(fmt::format("{}% ", total / _batteries.size()));
_label.set_text_with_mnemonic(fmt::format("{}% {}", pct, ""));
}
} catch (std::exception &e) { } catch (std::exception &e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }