feat(Custom): handle continuous script

This commit is contained in:
Alexis
2018-09-18 23:15:37 +02:00
parent d5d620e72d
commit 00959c7d65
4 changed files with 44 additions and 5 deletions

View File

@ -7,12 +7,16 @@ waybar::modules::Custom::Custom(const std::string name,
if (!config_["exec"]) {
throw std::runtime_error(name_ + " has no exec path.");
}
worker();
if (config_["interval"]) {
delayWorker();
} else {
continuousWorker();
}
}
void waybar::modules::Custom::worker()
void waybar::modules::Custom::delayWorker()
{
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
auto interval = config_["interval"].asUInt();
thread_ = [this, interval] {
bool can_update = true;
if (config_["exec-if"]) {
@ -31,6 +35,34 @@ void waybar::modules::Custom::worker()
};
}
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) {
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();
};
}
auto waybar::modules::Custom::update() -> void
{
// Hide label if output is empty