2018-08-10 14:26:46 +00:00
|
|
|
#include "modules/custom.hpp"
|
|
|
|
|
2018-08-18 15:27:40 +00:00
|
|
|
waybar::modules::Custom::Custom(std::string name, Json::Value config)
|
|
|
|
: ALabel(std::move(config)), name_(std::move(name))
|
2018-08-10 14:26:46 +00:00
|
|
|
{
|
2018-08-16 12:29:41 +00: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 15:27:40 +00:00
|
|
|
bool can_update = true;
|
|
|
|
if (config_["exec-if"]) {
|
2018-08-18 15:54:20 +00:00
|
|
|
auto res = waybar::util::command::exec(config_["exec-if"].asString());
|
|
|
|
if (res.exit_code != 0) {
|
2018-08-18 15:27:40 +00:00
|
|
|
can_update = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_update) {
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_.sig_update.emit();
|
2018-08-18 15:27:40 +00:00
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_.sleep_for(chrono::seconds(interval));
|
2018-08-10 14:26:46 +00:00
|
|
|
};
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_.sig_update.connect(sigc::mem_fun(*this, &Custom::update));
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-10 14:26:46 +00:00
|
|
|
|
|
|
|
auto waybar::modules::Custom::update() -> void
|
|
|
|
{
|
2018-08-18 15:54:20 +00:00
|
|
|
auto res = waybar::util::command::exec(config_["exec"].asString());
|
2018-08-10 14:26:46 +00:00
|
|
|
|
|
|
|
// Hide label if output is empty
|
2018-08-18 15:54:20 +00:00
|
|
|
if (res.out.empty() || res.exit_code != 0) {
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.hide();
|
2018-08-16 15:59:45 +00:00
|
|
|
label_.set_name("");
|
2018-08-10 15:05:12 +00:00
|
|
|
} else {
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_name("custom-" + name_);
|
|
|
|
auto format = config_["format"] ? config_["format"].asString() : "{}";
|
2018-08-18 15:54:20 +00:00
|
|
|
auto str = fmt::format(format, res.out);
|
2018-08-16 15:09:51 +00:00
|
|
|
label_.set_text(str);
|
|
|
|
label_.set_tooltip_text(str);
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.show();
|
2018-08-10 14:26:46 +00:00
|
|
|
}
|
|
|
|
}
|