From b381e2a596a744fa7668fed25a0d42ae6b0ab563 Mon Sep 17 00:00:00 2001 From: Alexis Date: Sat, 11 Aug 2018 13:15:31 +0200 Subject: [PATCH] feat(battery): capacity icons --- include/modules/battery.hpp | 1 + resources/config | 3 ++- src/modules/battery.cpp | 10 +++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index dc86d511..f1439002 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -19,6 +19,7 @@ namespace waybar::modules { auto update() -> void; operator Gtk::Widget&(); private: + std::string _getIcon(uint32_t percentage); static inline const fs::path _data_dir = "/sys/class/power_supply/"; std::vector _batteries; util::SleeperThread _thread; diff --git a/resources/config b/resources/config index 34ee4353..35f96f2c 100644 --- a/resources/config +++ b/resources/config @@ -14,7 +14,8 @@ "format": "{}% " }, "battery": { - "format": "{}% " + "format": "{value}% {icon}", + "format-icons": ["", "", "", "", ""] }, "network": { "interface": "wlp2s0", diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 97a8c9bf..fe76b8ee 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -44,7 +44,8 @@ auto waybar::modules::Battery::update() -> void } auto format = _config["format"] ? _config["format"].asString() : "{}%"; auto value = total / _batteries.size(); - _label.set_text(fmt::format(format, value)); + _label.set_text(fmt::format(format, fmt::arg("value", value), + fmt::arg("icon", _getIcon(value)))); _label.set_tooltip_text(charging ? "Charging" : "Discharging"); if (charging) _label.get_style_context()->add_class("charging"); @@ -59,6 +60,13 @@ auto waybar::modules::Battery::update() -> void } } +std::string waybar::modules::Battery::_getIcon(uint32_t percentage) +{ + if (!_config["format-icons"] || !_config["format-icons"].isArray()) return ""; + auto step = 100 / _config["format-icons"].size(); + return _config["format-icons"][percentage / step].asString(); +} + waybar::modules::Battery::operator Gtk::Widget &() { return _label;