Files
polybar/src/modules/tailscript.cpp
Michael Carlberg ccddf052ec refactor(script): Split non-/tail logic
Split the script module into one tailing output module and
one exec once module.

Refs #281
2017-01-01 09:05:18 +01:00

51 lines
1.4 KiB
C++

#include "modules/tailscript.hpp"
#include "drawtypes/label.hpp"
#include "modules/meta/base.inl"
POLYBAR_NS
namespace modules {
tailscript_module::tailscript_module(const bar_settings& bar, string name_) : script_module(bar, move(name_)) {
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 0s);
}
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);
m_command = command_util::make_command(exec);
try {
m_command->exec(false);
} catch (const exception& err) {
m_log.err("%s: %s", name(), err.what());
throw module_error("Failed to execute command, stopping module...");
}
}
if (io_util::poll(m_command->get_stdout(PIPE_READ), POLLIN, 0)) {
if ((m_output = m_command->readline()) != m_prev) {
m_prev = m_output;
broadcast();
}
}
guard.unlock();
if (m_command && !m_command->is_running()) {
sleep(std::max(m_command->get_exit_status() == 0 ? m_interval : 1s, m_interval));
} else {
sleep(m_interval);
}
}
}
POLYBAR_NS_END