feat(script): Configurable condition using exec-if
Add new config parameter `exec-if = cmd` that needs to exit successfully before the main exec command will be invoked. Closes #298
This commit is contained in:
parent
79856d7ed2
commit
946843ff59
@ -11,6 +11,7 @@ namespace modules {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void process();
|
void process();
|
||||||
|
chrono::duration<double> sleep_duration();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ namespace modules {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void process() = 0;
|
virtual void process() = 0;
|
||||||
|
virtual chrono::duration<double> sleep_duration() = 0;
|
||||||
|
|
||||||
static constexpr const char* TAG_OUTPUT{"<output>"};
|
static constexpr const char* TAG_OUTPUT{"<output>"};
|
||||||
static constexpr const char* TAG_LABEL{"<label>"};
|
static constexpr const char* TAG_LABEL{"<label>"};
|
||||||
@ -27,6 +28,8 @@ namespace modules {
|
|||||||
unique_ptr<command> m_command;
|
unique_ptr<command> m_command;
|
||||||
|
|
||||||
string m_exec;
|
string m_exec;
|
||||||
|
string m_exec_if;
|
||||||
|
|
||||||
chrono::duration<double> m_interval{0};
|
chrono::duration<double> m_interval{0};
|
||||||
map<mousebtn, string> m_actions;
|
map<mousebtn, string> m_actions;
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ namespace modules {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void process();
|
void process();
|
||||||
|
chrono::duration<double> sleep_duration();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,12 +11,7 @@ namespace modules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cmdscript_module::process() {
|
void cmdscript_module::process() {
|
||||||
if (!m_updatelock.try_lock()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
std::unique_lock<mutex> guard(m_updatelock, std::adopt_lock);
|
|
||||||
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
|
||||||
m_log.info("%s: Invoking shell command: \"%s\"", name(), exec);
|
m_log.info("%s: Invoking shell command: \"%s\"", name(), exec);
|
||||||
m_command = command_util::make_command(exec);
|
m_command = command_util::make_command(exec);
|
||||||
@ -30,8 +25,10 @@ namespace modules {
|
|||||||
broadcast();
|
broadcast();
|
||||||
m_prev = m_output;
|
m_prev = m_output;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sleep(std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval));
|
chrono::duration<double> cmdscript_module::sleep_duration() {
|
||||||
|
return std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ namespace modules {
|
|||||||
|
|
||||||
script_module::script_module(const bar_settings& bar, string name_) : module<script_module>(bar, move(name_)) {
|
script_module::script_module(const bar_settings& bar, string name_) : module<script_module>(bar, move(name_)) {
|
||||||
m_exec = m_conf.get(name(), "exec", m_exec);
|
m_exec = m_conf.get(name(), "exec", m_exec);
|
||||||
|
m_exec_if = m_conf.get(name(), "exec-if", m_exec_if);
|
||||||
m_maxlen = m_conf.get(name(), "maxlen", m_maxlen);
|
m_maxlen = m_conf.get(name(), "maxlen", m_maxlen);
|
||||||
m_ellipsis = m_conf.get(name(), "ellipsis", m_ellipsis);
|
m_ellipsis = m_conf.get(name(), "ellipsis", m_ellipsis);
|
||||||
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 5s);
|
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 5s);
|
||||||
@ -36,7 +37,25 @@ namespace modules {
|
|||||||
m_mainthread = thread([this] {
|
m_mainthread = thread([this] {
|
||||||
try {
|
try {
|
||||||
while (running() && !m_stopping) {
|
while (running() && !m_stopping) {
|
||||||
this->process();
|
std::unique_lock<mutex> guard(m_updatelock);
|
||||||
|
|
||||||
|
// Execute the condition command if specified
|
||||||
|
if (!m_exec_if.empty() && command_util::make_command(m_exec_if)->exec(true) != 0) {
|
||||||
|
if (!m_output.empty()) {
|
||||||
|
broadcast();
|
||||||
|
m_output.clear();
|
||||||
|
m_prev.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_interval >= 1s) {
|
||||||
|
sleep(m_interval);
|
||||||
|
} else {
|
||||||
|
sleep(1s);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this->process();
|
||||||
|
this->sleep(this->sleep_duration());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (const exception& err) {
|
} catch (const exception& err) {
|
||||||
halt(err.what());
|
halt(err.what());
|
||||||
@ -62,7 +81,7 @@ namespace modules {
|
|||||||
|
|
||||||
string script_module::get_output() {
|
string script_module::get_output() {
|
||||||
if (m_output.empty()) {
|
if (m_output.empty()) {
|
||||||
return " ";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_label) {
|
if (m_label) {
|
||||||
|
@ -11,12 +11,6 @@ namespace modules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void tailscript_module::process() {
|
void tailscript_module::process() {
|
||||||
if (!m_updatelock.try_lock()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_lock<mutex> guard(m_updatelock, std::adopt_lock);
|
|
||||||
|
|
||||||
if (!m_command || !m_command->is_running()) {
|
if (!m_command || !m_command->is_running()) {
|
||||||
string exec{string_util::replace_all(m_exec, "%counter%", to_string(++m_counter))};
|
string exec{string_util::replace_all(m_exec, "%counter%", to_string(++m_counter))};
|
||||||
m_log.info("%s: Invoking shell command: \"%s\"", name(), exec);
|
m_log.info("%s: Invoking shell command: \"%s\"", name(), exec);
|
||||||
@ -36,13 +30,13 @@ namespace modules {
|
|||||||
broadcast();
|
broadcast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
guard.unlock();
|
chrono::duration<double> tailscript_module::sleep_duration() {
|
||||||
|
|
||||||
if (m_command && !m_command->is_running()) {
|
if (m_command && !m_command->is_running()) {
|
||||||
sleep(std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval));
|
return std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval);
|
||||||
} else {
|
} else {
|
||||||
sleep(m_interval);
|
return m_interval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user