diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 38729ec4..375c10e9 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "util/chrono.hpp" #include "IModule.hpp" diff --git a/include/modules/pulseaudio.hpp b/include/modules/pulseaudio.hpp index b5b7e098..20ac7133 100644 --- a/include/modules/pulseaudio.hpp +++ b/include/modules/pulseaudio.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "IModule.hpp" namespace waybar::modules { @@ -13,6 +14,7 @@ namespace waybar::modules { auto update() -> void; operator Gtk::Widget &(); private: + std::string _getIcon(uint16_t percentage); static void _subscribeCb(pa_context *context, pa_subscription_event_type_t type, uint32_t idx, void *data); static void _contextStateCb(pa_context *c, void *data); diff --git a/resources/config b/resources/config index 35f96f2c..58aefe60 100644 --- a/resources/config +++ b/resources/config @@ -14,7 +14,7 @@ "format": "{}% " }, "battery": { - "format": "{value}% {icon}", + "format": "{capacity}% {icon}", "format-icons": ["", "", "", "", ""] }, "network": { @@ -22,8 +22,9 @@ "format": "{essid} ({signalStrength}%) " }, "pulseaudio": { - "format": "{}% ", - "format-muted": "" + "format": "{volume}% {icon}", + "format-muted": "", + "format-icons": ["", ""] }, "custom/spotify": { "format": " {}", diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 1457b4e3..fadebda4 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -48,8 +48,9 @@ auto waybar::modules::Battery::update() -> void total += capacity; } uint16_t capacity = total / _batteries.size(); - auto format = _config["format"] ? _config["format"].asString() : "{}%"; - _label.set_text(fmt::format(format, fmt::arg("value", capacity), + auto format = _config["format"] + ? _config["format"].asString() : "{capacity}%"; + _label.set_text(fmt::format(format, fmt::arg("capacity", capacity), fmt::arg("icon", _getIcon(capacity)))); _label.set_tooltip_text(status); if (charging) @@ -68,8 +69,9 @@ auto waybar::modules::Battery::update() -> void std::string waybar::modules::Battery::_getIcon(uint16_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(); + auto size = _config["format-icons"].size(); + auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); + return _config["format-icons"][idx].asString(); } waybar::modules::Battery::operator Gtk::Widget &() diff --git a/src/modules/network.cpp b/src/modules/network.cpp index b724b69a..6fea42b2 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -16,7 +16,7 @@ waybar::modules::Network::Network(Json::Value config) auto waybar::modules::Network::update() -> void { _getInfo(); - auto format = _config["format"] ? _config["format"].asString() : "{}"; + auto format = _config["format"] ? _config["format"].asString() : "{essid}"; _label.set_text(fmt::format(format, fmt::arg("essid", _essid), fmt::arg("signaldBm", _signalStrengthdBm), diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index 1fe5ad15..6068c07a 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -97,17 +97,27 @@ void waybar::modules::Pulseaudio::_serverInfoCb(pa_context *context, auto waybar::modules::Pulseaudio::update() -> void { - auto format = _config["format"] ? _config["format"].asString() : "{}%"; + auto format = _config["format"] ? _config["format"].asString() : "{volume}%"; if (_muted) { format = _config["format-muted"] ? _config["format-muted"].asString() : format; _label.get_style_context()->add_class("muted"); } else _label.get_style_context()->remove_class("muted"); - _label.set_label(fmt::format(format, _volume)); + _label.set_label(fmt::format(format, + fmt::arg("volume", _volume), + fmt::arg("icon", _getIcon(_volume)))); _label.set_tooltip_text(_desc); } +std::string waybar::modules::Pulseaudio::_getIcon(uint16_t percentage) +{ + if (!_config["format-icons"] || !_config["format-icons"].isArray()) return ""; + auto size = _config["format-icons"].size(); + auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); + return _config["format-icons"][idx].asString(); +} + waybar::modules::Pulseaudio::operator Gtk::Widget &() { return _label; }