waybar/src/modules/custom.cpp

43 lines
1.2 KiB
C++
Raw Normal View History

#include "modules/custom.hpp"
2018-08-18 17:27:40 +02:00
waybar::modules::Custom::Custom(std::string name, Json::Value config)
: ALabel(std::move(config)), name_(std::move(name))
{
2018-08-16 14:29:41 +02:00
if (!config_["exec"]) {
throw std::runtime_error(name_ + " has no exec path.");
}
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
thread_ = [this, interval] {
2018-08-18 17:27:40 +02:00
bool can_update = true;
if (config_["exec-if"]) {
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;
}
}
if (can_update) {
2018-08-19 13:39:57 +02:00
thread_.sig_update.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-19 13:39:57 +02:00
thread_.sig_update.connect(sigc::mem_fun(*this, &Custom::update));
}
auto waybar::modules::Custom::update() -> void
{
auto res = waybar::util::command::exec(config_["exec"].asString());
// Hide label if output is empty
if (res.out.empty() || res.exit_code != 0) {
2018-08-16 14:29:41 +02:00
label_.hide();
2018-08-16 17:59:45 +02:00
label_.set_name("");
} else {
2018-08-16 14:29:41 +02:00
label_.set_name("custom-" + name_);
auto format = config_["format"] ? config_["format"].asString() : "{}";
auto str = fmt::format(format, res.out);
2018-08-16 17:09:51 +02:00
label_.set_text(str);
label_.set_tooltip_text(str);
2018-08-16 14:29:41 +02:00
label_.show();
}
}