diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index 187f26b..3dd31bf 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 bfc9c09..822d4b0 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