Added return-type json to custom module

This commit is contained in:
Robinhuett 2018-11-01 00:40:44 +01:00
parent 341d3300fa
commit e23fbd0add
2 changed files with 24 additions and 3 deletions

View File

@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include "util/chrono.hpp" #include "util/chrono.hpp"
#include "util/command.hpp" #include "util/command.hpp"
#include "util/json.hpp"
#include "ALabel.hpp" #include "ALabel.hpp"
namespace waybar::modules { namespace waybar::modules {
@ -15,7 +16,8 @@ class Custom : public ALabel {
private: private:
void delayWorker(); void delayWorker();
void continuousWorker(); void continuousWorker();
void parseOutput(); void parseOutputRaw();
void parseOutputJson();
const std::string name_; const std::string name_;
std::string text_; std::string text_;
@ -24,6 +26,7 @@ class Custom : public ALabel {
std::string prevclass_; std::string prevclass_;
waybar::util::SleeperThread thread_; waybar::util::SleeperThread thread_;
waybar::util::command::res output_; waybar::util::command::res output_;
waybar::util::JsonParser parser_;
}; };
} }

View File

@ -73,7 +73,12 @@ auto waybar::modules::Custom::update() -> void
} else { } else {
label_.set_name("custom-" + name_); label_.set_name("custom-" + name_);
parseOutput(); if (config_["return-type"].asString() == "json") {
parseOutputJson();
} else {
parseOutputRaw();
}
auto str = fmt::format(format_, text_); auto str = fmt::format(format_, text_);
label_.set_text(str); label_.set_text(str);
if (text_ == tooltip_) { 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::istringstream output(output_.out);
std::string line; std::string line;
@ -115,4 +120,17 @@ void waybar::modules::Custom::parseOutput()
} }
i++; 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;
}
} }