Merge pull request #996 from martin2250/master

add power formatter to battery module
pull/1001/head
Alex 2021-01-24 23:33:12 +01:00 committed by GitHub
commit 69a366dced
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 9 deletions

View File

@ -34,7 +34,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> getInfos();
const std::tuple<uint8_t, float, std::string, float> getInfos();
const std::string formatTimeRemaining(float hoursRemaining);
int global_watch;

View File

@ -96,6 +96,8 @@ The *battery* module displays the current capacity and state (eg. charging) of y
*{capacity}*: Capacity in percentage
*{power}*: Power in watts
*{icon}*: Icon, as defined in *format-icons*.
*{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.

View File

@ -135,7 +135,7 @@ void waybar::modules::Battery::refreshBatteries() {
}
}
const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos() {
const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::getInfos() {
std::lock_guard<std::mutex> guard(battery_list_mutex_);
try {
@ -210,10 +210,10 @@ const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos
status = "Full";
}
return {cap, time_remaining, status};
return {cap, time_remaining, status, total_power / 1e6};
} catch (const std::exception& e) {
spdlog::error("Battery: {}", e.what());
return {0, 0, "Unknown"};
return {0, 0, "Unknown", 0};
}
}
@ -248,7 +248,7 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai
}
auto waybar::modules::Battery::update() -> void {
auto [capacity, time_remaining, status] = getInfos();
auto [capacity, time_remaining, status, power] = getInfos();
if (status == "Unknown") {
status = getAdapterStatus(capacity);
}
@ -302,6 +302,7 @@ auto waybar::modules::Battery::update() -> void {
auto icons = std::vector<std::string>{status + "-" + state, status, state};
label_.set_markup(fmt::format(format,
fmt::arg("capacity", capacity),
fmt::arg("power", power),
fmt::arg("icon", getIcon(capacity, icons)),
fmt::arg("time", time_remaining_formatted)));
}