From 2b8c9d845b3fbc470824bdc4f416d433f84aa611 Mon Sep 17 00:00:00 2001 From: Samuel Grahn Date: Sun, 27 Nov 2022 17:13:47 +0100 Subject: [PATCH] Reimplement format & getIcon --- include/modules/custom.hpp | 4 ++-- src/modules/custom.cpp | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 0e13d076..77a9533e 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -33,7 +33,6 @@ class Custom : public AModule { std::string text_; std::string alt_; std::string tooltip_; - std::string format_; std::string onclick_; bool hide_; std::vector class_; @@ -42,7 +41,6 @@ class Custom : public AModule { text_(""), alt_(""), tooltip_(""), - format_(""), onclick_(""), hide_(false), class_(std::vector()), @@ -59,6 +57,7 @@ class Custom : public AModule { void handleClick(std::string name); Node parseItem(Json::Value &parsed); Gtk::Button &addButton(const Node &node); + std::string getIcon(uint16_t percentage, const std::string& alt = "", uint16_t max = 0); const std::chrono::seconds interval_; util::command::res output_; @@ -68,6 +67,7 @@ class Custom : public AModule { std::unordered_map buttons_; Gtk::Box box_; + std::string format_; FILE* fp_; int pid_; diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index fc6480f7..e81acfa6 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -9,6 +9,7 @@ waybar::modules::Custom::Custom(const std::string& name, : AModule(config, "custom-" + name, id, false, !config["disable-scroll"].asBool()), interval_(config["interval"].isUInt() ? config_["interval"].asUInt() : 0), box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), + format_(config["format"].isString() ? config["format"].asString() : "{}"), fp_(nullptr), pid_(-1) { box_.set_name("custom-" + name); @@ -142,11 +143,9 @@ auto waybar::modules::Custom::update() -> void { auto &button = bit == buttons_.end() ? addButton(*res) : bit->second; - auto str = res->text_; - //auto str = fmt::format(res->format_, res->text_, fmt::arg("alt", res->alt_), - //fmt::arg("icon", getIcon(res->percentage_, res->alt_)), TODO - // fmt::arg("percentage", res->percentage_)); - + auto str = fmt::format(format_, res->text_, fmt::arg("alt", res->alt_), + fmt::arg("icon", getIcon(res->percentage_, res->alt_)), + fmt::arg("percentage", res->percentage_)); if(str.empty() || res->hide_) { button.hide(); } else { @@ -202,6 +201,28 @@ auto waybar::modules::Custom::update() -> void { AModule::update(); } +std::string waybar::modules::Custom::getIcon(uint16_t percentage, const std::string& alt, uint16_t max) { + auto format_icons = config_["format-icons"]; + if (format_icons.isObject()) { + if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) { + format_icons = format_icons[alt]; + } else { + format_icons = format_icons["default"]; + } + } + if (format_icons.isArray()) { + auto size = format_icons.size(); + if (size) { + auto idx = std::clamp(percentage / ((max == 0 ? 100 : max) / size), 0U, size - 1); + format_icons = format_icons[idx]; + } + } + if (format_icons.isString()) { + return format_icons.asString(); + } + return ""; +} + void waybar::modules::Custom::parseOutputRaw() { Node n = Node(); // Retain name if there is only one node @@ -262,6 +283,12 @@ waybar::modules::Custom::Node waybar::modules::Custom::parseItem(Json::Value &pa if (!parsed["onclick"].asString().empty() && parsed["onclick"].isString()) { n.onclick_ = parsed["onclick"].asString(); } + if(parsed["hide"].isBool()) { + n.hide_ = parsed["hide"].asBool(); + } + + + return n; }