feat(ipc): Initial exec of configured hook

Adds a new config parameter `initial = N` that will
make the hook at defined index N execute on start.
This commit is contained in:
Michael Carlberg 2017-01-09 23:06:39 +01:00
parent 56b89d5a44
commit 7a26254844
2 changed files with 20 additions and 3 deletions

View File

@ -28,6 +28,7 @@ namespace modules {
public: public:
explicit ipc_module(const bar_settings&, string); explicit ipc_module(const bar_settings&, string);
void start();
void update() {} void update() {}
string get_output(); string get_output();
bool build(builder* builder, const string& tag) const; bool build(builder* builder, const string& tag) const;
@ -37,7 +38,7 @@ namespace modules {
static constexpr auto TAG_OUTPUT = "<output>"; static constexpr auto TAG_OUTPUT = "<output>";
vector<unique_ptr<hook>> m_hooks; vector<unique_ptr<hook>> m_hooks;
string m_output; string m_output;
size_t m_initial{0_z};
map<mousebtn, string> m_actions; map<mousebtn, string> m_actions;
}; };
} }

View File

@ -21,7 +21,11 @@ namespace modules {
} }
if (m_hooks.empty()) { if (m_hooks.empty()) {
throw module_error("No ipc hooks defined"); throw module_error("No hooks defined");
}
if ((m_initial = m_conf.get(name(), "initial", m_initial)) && m_initial > m_hooks.size()) {
throw module_error("Initial hook out of bounds (defined: " + to_string(m_hooks.size()) + ")");
} }
m_actions[mousebtn::LEFT] = m_conf.get(name(), "click-left", ""s); m_actions[mousebtn::LEFT] = m_conf.get(name(), "click-left", ""s);
@ -33,6 +37,18 @@ namespace modules {
m_formatter->add(DEFAULT_FORMAT, TAG_OUTPUT, {TAG_OUTPUT}); m_formatter->add(DEFAULT_FORMAT, TAG_OUTPUT, {TAG_OUTPUT});
} }
/**
* Start module and run first defined hook if configured to
*/
void ipc_module::start() {
if (m_initial > 0_z) {
auto command = command_util::make_command(m_hooks.at(m_initial - 1)->command);
command->exec(false);
command->tail([this](string line) { m_output = line; });
}
static_module::start();
}
/** /**
* Wrap the output with defined mouse actions * Wrap the output with defined mouse actions
*/ */
@ -69,10 +85,10 @@ namespace modules {
bool ipc_module::build(builder* builder, const string& tag) const { bool ipc_module::build(builder* builder, const string& tag) const {
if (tag == TAG_OUTPUT) { if (tag == TAG_OUTPUT) {
builder->node(m_output); builder->node(m_output);
return true;
} else { } else {
return false; return false;
} }
return true;
} }
/** /**