From 3bcf3904849bc3cb91799d992336d1b7fe59dd1e Mon Sep 17 00:00:00 2001 From: Martin Pittermann Date: Sun, 24 Jan 2021 21:39:14 +0100 Subject: [PATCH 1/2] add power to battery formatter --- include/modules/battery.hpp | 10 +++++----- src/modules/battery.cpp | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 08dd79db..41bc0ad3 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -31,11 +31,11 @@ class Battery : public ALabel { private: static inline const fs::path data_dir_ = "/sys/class/power_supply/"; - void refreshBatteries(); - void worker(); - const std::string getAdapterStatus(uint8_t capacity) const; - const std::tuple getInfos(); - const std::string formatTimeRemaining(float hoursRemaining); + void refreshBatteries(); + void worker(); + const std::string getAdapterStatus(uint8_t capacity) const; + const std::tuple getInfos(); + const std::string formatTimeRemaining(float hoursRemaining); int global_watch; std::map batteries_; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 86f24b2e..85774697 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -135,7 +135,7 @@ void waybar::modules::Battery::refreshBatteries() { } } -const std::tuple waybar::modules::Battery::getInfos() { +const std::tuple waybar::modules::Battery::getInfos() { std::lock_guard guard(battery_list_mutex_); try { @@ -210,10 +210,10 @@ const std::tuple 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{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))); } From cd97bdb30f1d6d5b6ebc9553bb9ff1e13ccefa28 Mon Sep 17 00:00:00 2001 From: Martin Pittermann Date: Sun, 24 Jan 2021 21:49:00 +0100 Subject: [PATCH 2/2] document power formatter in battery module --- man/waybar-battery.5.scd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/man/waybar-battery.5.scd b/man/waybar-battery.5.scd index 869df32c..a4650cd6 100644 --- a/man/waybar-battery.5.scd +++ b/man/waybar-battery.5.scd @@ -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.