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:
Michael Carlberg 2017-01-10 03:01:59 +01:00
parent 79856d7ed2
commit 946843ff59
6 changed files with 33 additions and 18 deletions

View File

@ -11,6 +11,7 @@ namespace modules {
protected:
void process();
chrono::duration<double> sleep_duration();
};
}

View File

@ -20,6 +20,7 @@ namespace modules {
protected:
virtual void process() = 0;
virtual chrono::duration<double> sleep_duration() = 0;
static constexpr const char* TAG_OUTPUT{"<output>"};
static constexpr const char* TAG_LABEL{"<label>"};
@ -27,6 +28,8 @@ namespace modules {
unique_ptr<command> m_command;
string m_exec;
string m_exec_if;
chrono::duration<double> m_interval{0};
map<mousebtn, string> m_actions;

View File

@ -11,6 +11,7 @@ namespace modules {
protected:
void process();
chrono::duration<double> sleep_duration();
};
}

View File

@ -11,12 +11,7 @@ namespace modules {
}
void cmdscript_module::process() {
if (!m_updatelock.try_lock()) {
return;
}
try {
std::unique_lock<mutex> guard(m_updatelock, std::adopt_lock);
auto exec = string_util::replace_all(m_exec, "%counter%", to_string(++m_counter));
m_log.info("%s: Invoking shell command: \"%s\"", name(), exec);
m_command = command_util::make_command(exec);
@ -30,8 +25,10 @@ namespace modules {
broadcast();
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);
}
}

View File

@ -10,6 +10,7 @@ namespace modules {
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_if = m_conf.get(name(), "exec-if", m_exec_if);
m_maxlen = m_conf.get(name(), "maxlen", m_maxlen);
m_ellipsis = m_conf.get(name(), "ellipsis", m_ellipsis);
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 5s);
@ -36,7 +37,25 @@ namespace modules {
m_mainthread = thread([this] {
try {
while (running() && !m_stopping) {
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) {
halt(err.what());

View File

@ -11,12 +11,6 @@ namespace modules {
}
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()) {
string exec{string_util::replace_all(m_exec, "%counter%", to_string(++m_counter))};
m_log.info("%s: Invoking shell command: \"%s\"", name(), exec);
@ -36,13 +30,13 @@ namespace modules {
broadcast();
}
}
}
guard.unlock();
chrono::duration<double> tailscript_module::sleep_duration() {
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 {
sleep(m_interval);
return m_interval;
}
}
}