From 7a262548441c5ef4a781344ea881b7244b09ef97 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Mon, 9 Jan 2017 23:06:39 +0100 Subject: [PATCH] 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. --- include/modules/ipc.hpp | 3 ++- src/modules/ipc.cpp | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/modules/ipc.hpp b/include/modules/ipc.hpp index 35454997..09312ccf 100644 --- a/include/modules/ipc.hpp +++ b/include/modules/ipc.hpp @@ -28,6 +28,7 @@ namespace modules { public: explicit ipc_module(const bar_settings&, string); + void start(); void update() {} string get_output(); bool build(builder* builder, const string& tag) const; @@ -37,7 +38,7 @@ namespace modules { static constexpr auto TAG_OUTPUT = ""; vector> m_hooks; string m_output; - + size_t m_initial{0_z}; map m_actions; }; } diff --git a/src/modules/ipc.cpp b/src/modules/ipc.cpp index 5ad99868..c4dce368 100644 --- a/src/modules/ipc.cpp +++ b/src/modules/ipc.cpp @@ -21,7 +21,11 @@ namespace modules { } 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); @@ -33,6 +37,18 @@ namespace modules { 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 */ @@ -69,10 +85,10 @@ namespace modules { bool ipc_module::build(builder* builder, const string& tag) const { if (tag == TAG_OUTPUT) { builder->node(m_output); + return true; } else { return false; } - return true; } /**