2018-08-10 16:26:46 +02:00
|
|
|
#include "modules/custom.hpp"
|
|
|
|
|
2018-08-21 10:50:09 +02:00
|
|
|
waybar::modules::Custom::Custom(const std::string name,
|
2018-08-20 14:50:45 +02:00
|
|
|
const Json::Value& config)
|
2018-08-27 01:36:25 +02:00
|
|
|
: ALabel(config, "{}"), name_(name)
|
2018-08-10 16:26:46 +02:00
|
|
|
{
|
2018-10-26 09:27:16 +02:00
|
|
|
if (!config_["exec"].isString()) {
|
2018-08-16 14:29:41 +02:00
|
|
|
throw std::runtime_error(name_ + " has no exec path.");
|
|
|
|
}
|
2018-10-26 09:27:16 +02:00
|
|
|
if (config_["interval"].isUInt()) {
|
2018-09-18 23:15:37 +02:00
|
|
|
delayWorker();
|
|
|
|
} else {
|
|
|
|
continuousWorker();
|
|
|
|
}
|
2018-08-20 14:50:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 23:15:37 +02:00
|
|
|
void waybar::modules::Custom::delayWorker()
|
2018-08-20 14:50:45 +02:00
|
|
|
{
|
2018-09-18 23:15:37 +02:00
|
|
|
auto interval = config_["interval"].asUInt();
|
2018-08-16 14:29:41 +02:00
|
|
|
thread_ = [this, interval] {
|
2018-08-18 17:27:40 +02:00
|
|
|
bool can_update = true;
|
2018-10-26 09:27:16 +02:00
|
|
|
if (config_["exec-if"].isString()) {
|
2018-08-18 17:54:20 +02:00
|
|
|
auto res = waybar::util::command::exec(config_["exec-if"].asString());
|
|
|
|
if (res.exit_code != 0) {
|
2018-08-18 17:27:40 +02:00
|
|
|
can_update = false;
|
2018-08-26 21:41:34 +02:00
|
|
|
label_.hide();
|
2018-08-28 11:10:36 +02:00
|
|
|
label_.set_name("");
|
2018-08-18 17:27:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_update) {
|
2018-09-05 19:20:19 +02:00
|
|
|
output_ = waybar::util::command::exec(config_["exec"].asString());
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2018-08-18 17:27:40 +02:00
|
|
|
}
|
2018-08-16 14:29:41 +02:00
|
|
|
thread_.sleep_for(chrono::seconds(interval));
|
2018-08-10 16:26:46 +02:00
|
|
|
};
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-10 16:26:46 +02:00
|
|
|
|
2018-09-18 23:15:37 +02:00
|
|
|
void waybar::modules::Custom::continuousWorker()
|
|
|
|
{
|
|
|
|
auto cmd = config_["exec"].asString();
|
|
|
|
FILE* fp(popen(cmd.c_str(), "r"));
|
|
|
|
if (!fp) {
|
|
|
|
throw std::runtime_error("Unable to open " + cmd);
|
|
|
|
}
|
|
|
|
thread_ = [this, fp] {
|
|
|
|
char* buff = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
if (getline(&buff, &len, fp) == -1) {
|
2018-09-18 23:21:08 +02:00
|
|
|
pclose(fp);
|
2018-09-18 23:15:37 +02:00
|
|
|
thread_.stop();
|
|
|
|
output_ = { 1, "" };
|
|
|
|
dp.emit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string output = buff;
|
|
|
|
|
|
|
|
// Remove last newline
|
|
|
|
if (!output.empty() && output[output.length()-1] == '\n') {
|
|
|
|
output.erase(output.length()-1);
|
|
|
|
}
|
|
|
|
output_ = { 0, output };
|
|
|
|
dp.emit();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:26:46 +02:00
|
|
|
auto waybar::modules::Custom::update() -> void
|
|
|
|
{
|
|
|
|
// Hide label if output is empty
|
2018-09-05 19:20:19 +02:00
|
|
|
if (output_.out.empty() || output_.exit_code != 0) {
|
2018-08-16 14:29:41 +02:00
|
|
|
label_.hide();
|
2018-08-28 11:10:36 +02:00
|
|
|
label_.set_name("");
|
2018-08-10 17:05:12 +02:00
|
|
|
} else {
|
2018-08-28 11:10:36 +02:00
|
|
|
label_.set_name("custom-" + name_);
|
2018-10-30 21:28:31 +01:00
|
|
|
|
2018-11-01 00:40:44 +01:00
|
|
|
if (config_["return-type"].asString() == "json") {
|
|
|
|
parseOutputJson();
|
|
|
|
} else {
|
|
|
|
parseOutputRaw();
|
|
|
|
}
|
|
|
|
|
2018-10-30 21:28:31 +01:00
|
|
|
auto str = fmt::format(format_, text_);
|
2018-08-16 17:09:51 +02:00
|
|
|
label_.set_text(str);
|
2018-10-30 21:28:31 +01:00
|
|
|
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_ = "";
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
label_.show();
|
2018-08-10 16:26:46 +02:00
|
|
|
}
|
2018-10-30 21:28:31 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 00:40:44 +01:00
|
|
|
void waybar::modules::Custom::parseOutputRaw()
|
2018-10-30 21:28:31 +01:00
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
2018-11-01 00:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-09-05 19:20:19 +02:00
|
|
|
}
|