Waybar/src/modules/custom.cpp

47 lines
1.2 KiB
C++
Raw Normal View History

#include "modules/custom.hpp"
waybar::modules::Custom::Custom(const std::string name,
2018-08-20 12:50:45 +00:00
const Json::Value& config)
2018-08-26 23:36:25 +00:00
: ALabel(config, "{}"), name_(name)
{
2018-08-16 12:29:41 +00:00
if (!config_["exec"]) {
throw std::runtime_error(name_ + " has no exec path.");
}
2018-08-20 12:50:45 +00:00
worker();
}
void waybar::modules::Custom::worker()
{
2018-08-16 12:29:41 +00:00
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"]) {
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;
label_.hide();
label_.set_name("");
2018-08-18 15:27:40 +00:00
}
}
if (can_update) {
output_ = waybar::util::command::exec(config_["exec"].asString());
2018-08-20 12:50:45 +00:00
dp.emit();
2018-08-18 15:27:40 +00:00
}
2018-08-16 12:29:41 +00:00
thread_.sleep_for(chrono::seconds(interval));
};
}
auto waybar::modules::Custom::update() -> void
{
// Hide label if output is empty
if (output_.out.empty() || output_.exit_code != 0) {
2018-08-16 12:29:41 +00:00
label_.hide();
label_.set_name("");
} else {
label_.set_name("custom-" + name_);
auto str = fmt::format(format_, output_.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();
}
}