From 11c98f13e3dd3fecade215c431626de4d9ab77df Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Wed, 26 Dec 2018 03:52:05 +0100 Subject: [PATCH 1/2] feat(custom): Add format-icons to custom module This commit allows custom modules (json only) to set a percentage. This can be displayed either by using {percentage} or by using {icon} with format-icons set. --- include/modules/custom.hpp | 2 ++ src/modules/custom.cpp | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 937ab76..b429a84 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -19,12 +19,14 @@ class Custom : public ALabel { void continuousWorker(); void parseOutputRaw(); void parseOutputJson(); + bool isInteger(const std::string&); const std::string name_; std::string text_; std::string tooltip_; std::string class_; std::string prevclass_; + int percentage_; waybar::util::SleeperThread thread_; waybar::util::command::res output_; waybar::util::JsonParser parser_; diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 68d9846..2bcb35b 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -87,7 +87,9 @@ auto waybar::modules::Custom::update() -> void parseOutputRaw(); } - auto str = fmt::format(format_, text_); + auto str = fmt::format(format_, text_, + fmt::arg("icon", getIcon(percentage_)), + fmt::arg("percentage", percentage_)); label_.set_markup(str); if (text_ == tooltip_) { label_.set_tooltip_text(str); @@ -130,6 +132,19 @@ void waybar::modules::Custom::parseOutputRaw() } } +bool waybar::modules::Custom::isInteger(const std::string& n) +{ + if (std::isdigit(n[0]) || (n.size() > 1 && (n[0] == '-' || n[0] == '+'))) { + for (std::string::size_type i{ 1 }; i < n.size(); ++i) { + if (!std::isdigit(n[i])) { + return false; + } + } + return true; + } + return false; +} + void waybar::modules::Custom::parseOutputJson() { std::istringstream output(output_.out); @@ -139,6 +154,11 @@ void waybar::modules::Custom::parseOutputJson() text_ = parsed["text"].asString(); tooltip_ = parsed["tooltip"].asString(); class_ = parsed["class"].asString(); + if (!parsed["percentage"].asString().empty() && isInteger(parsed["percentage"].asString())) { + percentage_ = std::stoi(parsed["percentage"].asString(), nullptr); + } else { + percentage_ = 0; + } break; } } From 4698c9d2cf1ead77df77e4b4d74a62eb4df79e89 Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Wed, 26 Dec 2018 11:35:58 +0100 Subject: [PATCH 2/2] chore(custom): Change int conversion --- include/modules/custom.hpp | 1 - src/modules/custom.cpp | 17 ++--------------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index b429a84..074ed04 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -19,7 +19,6 @@ class Custom : public ALabel { void continuousWorker(); void parseOutputRaw(); void parseOutputJson(); - bool isInteger(const std::string&); const std::string name_; std::string text_; diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 2bcb35b..c75760d 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -132,19 +132,6 @@ void waybar::modules::Custom::parseOutputRaw() } } -bool waybar::modules::Custom::isInteger(const std::string& n) -{ - if (std::isdigit(n[0]) || (n.size() > 1 && (n[0] == '-' || n[0] == '+'))) { - for (std::string::size_type i{ 1 }; i < n.size(); ++i) { - if (!std::isdigit(n[i])) { - return false; - } - } - return true; - } - return false; -} - void waybar::modules::Custom::parseOutputJson() { std::istringstream output(output_.out); @@ -154,8 +141,8 @@ void waybar::modules::Custom::parseOutputJson() text_ = parsed["text"].asString(); tooltip_ = parsed["tooltip"].asString(); class_ = parsed["class"].asString(); - if (!parsed["percentage"].asString().empty() && isInteger(parsed["percentage"].asString())) { - percentage_ = std::stoi(parsed["percentage"].asString(), nullptr); + if (!parsed["percentage"].asString().empty() && parsed["percentage"].isUInt()) { + percentage_ = parsed["percentage"].asUInt(); } else { percentage_ = 0; }