style(battery): format

pull/364/head
Alex 2019-05-22 10:09:05 +02:00
parent 2fa581c7ea
commit 14ace24a26
1 changed files with 17 additions and 16 deletions

View File

@ -79,8 +79,8 @@ void waybar::modules::Battery::getBatteries() {
const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() const { const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() const {
try { try {
uint16_t total = 0; uint16_t total = 0;
uint32_t total_power = 0; // μW uint32_t total_power = 0; // μW
uint32_t total_energy = 0; // μWh uint32_t total_energy = 0; // μWh
uint32_t total_energy_full = 0; uint32_t total_energy_full = 0;
std::string status = "Unknown"; std::string status = "Unknown";
for (auto const& bat : batteries_) { for (auto const& bat : batteries_) {
@ -114,19 +114,19 @@ const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos
} }
float time_remaining = 0; float time_remaining = 0;
if (status == "Discharging" && total_power != 0) { if (status == "Discharging" && total_power != 0) {
time_remaining = (float)total_energy / total_power; time_remaining = (float)total_energy / total_power;
} else if (status == "Charging" && total_power != 0) { } else if (status == "Charging" && total_power != 0) {
time_remaining = -(float)(total_energy_full - total_energy) / total_power; time_remaining = -(float)(total_energy_full - total_energy) / total_power;
} }
uint16_t capacity = total / batteries_.size(); uint16_t capacity = total / batteries_.size();
return {capacity, time_remaining, status}; return {capacity, time_remaining, status};
} catch (const std::exception& e) { } catch (const std::exception& e) {
spdlog::error("Battery: {}", e.what()); spdlog::error("Battery: {}", e.what());
return {0, 0, "Unknown"}; return {0, 0, "Unknown"};
} }
} }
const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const { const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const {
if (!adapter_.empty()) { if (!adapter_.empty()) {
bool online; bool online;
std::ifstream(adapter_ / "online") >> online; std::ifstream(adapter_ / "online") >> online;
@ -142,10 +142,10 @@ const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity)
} }
const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemaining) { const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemaining) {
hoursRemaining = std::fabs(hoursRemaining); hoursRemaining = std::fabs(hoursRemaining);
uint16_t full_hours = static_cast<uint16_t>(hoursRemaining); uint16_t full_hours = static_cast<uint16_t>(hoursRemaining);
uint16_t minutes = static_cast<uint16_t>(60 * (hoursRemaining - full_hours)); uint16_t minutes = static_cast<uint16_t>(60 * (hoursRemaining - full_hours));
return std::to_string(full_hours) + " h " + std::to_string(minutes) + " min"; return std::to_string(full_hours) + " h " + std::to_string(minutes) + " min";
} }
auto waybar::modules::Battery::update() -> void { auto waybar::modules::Battery::update() -> void {
@ -156,11 +156,10 @@ auto waybar::modules::Battery::update() -> void {
if (tooltipEnabled()) { if (tooltipEnabled()) {
std::string tooltip_text; std::string tooltip_text;
if (time_remaining != 0) { if (time_remaining != 0) {
std::string time_to = std::string("Time to ") + std::string time_to = std::string("Time to ") + ((time_remaining > 0) ? "empty" : "full");
((time_remaining > 0) ? "empty" : "full"); tooltip_text = time_to + ": " + formatTimeRemaining(time_remaining);
tooltip_text = time_to + ": " + formatTimeRemaining(time_remaining);
} else { } else {
tooltip_text = status; tooltip_text = status;
} }
label_.set_tooltip_text(tooltip_text); label_.set_tooltip_text(tooltip_text);
} }
@ -183,7 +182,9 @@ auto waybar::modules::Battery::update() -> void {
event_box_.hide(); event_box_.hide();
} else { } else {
event_box_.show(); event_box_.show();
label_.set_markup(fmt::format( label_.set_markup(fmt::format(format,
format, fmt::arg("capacity", capacity), fmt::arg("icon", getIcon(capacity, state)), fmt::arg("time", formatTimeRemaining(time_remaining)))); fmt::arg("capacity", capacity),
fmt::arg("icon", getIcon(capacity, state)),
fmt::arg("time", formatTimeRemaining(time_remaining))));
} }
} }