mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
feat(Custom): handle continuous script
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user