From 341d3300fa25ad06a5ff357fd492f5c36beb872d Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Tue, 30 Oct 2018 21:28:31 +0100 Subject: [PATCH 1/2] Custom modules can control tooltip and CSS class --- include/modules/custom.hpp | 5 +++++ src/modules/custom.cpp | 42 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index f0553831..187f26b7 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -15,8 +15,13 @@ class Custom : public ALabel { private: void delayWorker(); void continuousWorker(); + void parseOutput(); const std::string name_; + std::string text_; + std::string tooltip_; + std::string class_; + std::string prevclass_; waybar::util::SleeperThread thread_; waybar::util::command::res output_; }; diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 5a38e35d..bfc9c09b 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -72,9 +72,47 @@ auto waybar::modules::Custom::update() -> void label_.set_name(""); } else { label_.set_name("custom-" + name_); - auto str = fmt::format(format_, output_.out); + + parseOutput(); + auto str = fmt::format(format_, text_); label_.set_text(str); - label_.set_tooltip_text(str); + if (text_ == tooltip_) { + label_.set_tooltip_text(str); + } else { + label_.set_tooltip_text(tooltip_); + } + if (class_ != "") { + if (prevclass_ != "") { + label_.get_style_context()->remove_class(prevclass_); + } + label_.get_style_context()->add_class(class_); + prevclass_ = class_; + } else { + label_.get_style_context()->remove_class(prevclass_); + prevclass_ = ""; + } + label_.show(); } +} + +void waybar::modules::Custom::parseOutput() +{ + std::istringstream output(output_.out); + std::string line; + int i = 0; + while (getline(output, line)) { + if (i == 0) { + text_ = line; + tooltip_ = line; + class_ = ""; + } else if (i == 1) { + tooltip_ = line; + } else if (i == 2) { + class_ = line; + } else { + break; + } + i++; + } } \ No newline at end of file From e23fbd0addccf5f68c04a83405455919fee364af Mon Sep 17 00:00:00 2001 From: Robinhuett <5955614+Robinhuett@users.noreply.github.com> Date: Thu, 1 Nov 2018 00:40:44 +0100 Subject: [PATCH 2/2] Added return-type json to custom module --- include/modules/custom.hpp | 5 ++++- src/modules/custom.cpp | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 187f26b7..3dd31bff 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -4,6 +4,7 @@ #include #include "util/chrono.hpp" #include "util/command.hpp" +#include "util/json.hpp" #include "ALabel.hpp" namespace waybar::modules { @@ -15,7 +16,8 @@ class Custom : public ALabel { private: void delayWorker(); void continuousWorker(); - void parseOutput(); + void parseOutputRaw(); + void parseOutputJson(); const std::string name_; std::string text_; @@ -24,6 +26,7 @@ class Custom : public ALabel { std::string prevclass_; 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 bfc9c09b..822d4b0a 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -73,7 +73,12 @@ auto waybar::modules::Custom::update() -> void } else { label_.set_name("custom-" + name_); - parseOutput(); + if (config_["return-type"].asString() == "json") { + parseOutputJson(); + } else { + parseOutputRaw(); + } + auto str = fmt::format(format_, text_); label_.set_text(str); if (text_ == tooltip_) { @@ -96,7 +101,7 @@ auto waybar::modules::Custom::update() -> void } } -void waybar::modules::Custom::parseOutput() +void waybar::modules::Custom::parseOutputRaw() { std::istringstream output(output_.out); std::string line; @@ -115,4 +120,17 @@ void waybar::modules::Custom::parseOutput() } i++; } +} + +void waybar::modules::Custom::parseOutputJson() +{ + std::istringstream output(output_.out); + std::string line; + while (getline(output, line)) { + auto parsed = parser_.parse(line); + text_ = parsed["text"].asString(); + tooltip_ = parsed["tooltip"].asString(); + class_ = parsed["class"].asString(); + break; + } } \ No newline at end of file