2018-08-10 16:26:46 +02:00
|
|
|
#include "modules/custom.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
waybar::modules::Custom::Custom(std::string name, Json::Value config)
|
|
|
|
: _name(name), _config(config)
|
|
|
|
{
|
|
|
|
if (!_config["exec"]) {
|
|
|
|
std::cerr << name + " has no exec path." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_thread = [this] {
|
|
|
|
update();
|
|
|
|
int interval = _config["interval"] ? _config["inveral"].asInt() : 30;
|
|
|
|
_thread.sleep_for(chrono::seconds(interval));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
auto waybar::modules::Custom::update() -> void
|
|
|
|
{
|
|
|
|
std::array<char, 128> buffer;
|
|
|
|
std::string output;
|
|
|
|
std::shared_ptr<FILE> fp(popen(_config["exec"].asCString(), "r"), pclose);
|
|
|
|
if (!fp) {
|
|
|
|
std::cerr << _name + " can't exec " + _config["exec"].asString() << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (!feof(fp.get())) {
|
|
|
|
if (fgets(buffer.data(), 128, fp.get()) != nullptr)
|
|
|
|
output += buffer.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove last newline
|
|
|
|
if (!output.empty() && output[output.length()-1] == '\n') {
|
|
|
|
output.erase(output.length()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide label if output is empty
|
2018-08-10 17:05:12 +02:00
|
|
|
if (output.empty()) {
|
|
|
|
_label.get_style_context()->remove_class("custom-" + _name);
|
2018-08-10 16:26:46 +02:00
|
|
|
_label.hide();
|
2018-08-10 17:05:12 +02:00
|
|
|
} else {
|
|
|
|
_label.get_style_context()->add_class("custom-" + _name);
|
2018-08-10 16:26:46 +02:00
|
|
|
auto format = _config["format"] ? _config["format"].asString() : "{}";
|
|
|
|
_label.set_text(fmt::format(format, output));
|
|
|
|
_label.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
waybar::modules::Custom::operator Gtk::Widget &() {
|
|
|
|
return _label;
|
|
|
|
}
|