feat(ipc): allow receiving arbitrary text on IPC socket (#2463)

This adds the 'send' action to the ipc module that can be used to send arbitrary text to the module:

    polybar-msg action "#ipc.send.%{F#4444ff}hello%{F-}"

* feat(ipc): allow receiving arbitrary text on IPC socket

Instead of just allowing hook numbers to be executed, the user can
send arbitrary text and the IPC module will put it in the bar. The IPC
payload format is extended to accept an arbitrary string if the first
character after the module name is ':'.

    polybar-msg hook test :'%{F#4444ff}hello%{F-}'

Fix #2455

* Use actions for sending data to ipc module

* ipc: Don't use exceptions when no hooks are defined

* Update src/modules/ipc.cpp

Co-authored-by: patrick96 <p.ziegler96@gmail.com>
This commit is contained in:
Vincent Bernat
2021-07-11 20:42:28 +02:00
committed by GitHub
parent 523e4d6313
commit 5f3462240c
4 changed files with 23 additions and 4 deletions

View File

@ -13,15 +13,15 @@ namespace modules {
* create formatting tags
*/
ipc_module::ipc_module(const bar_settings& bar, string name_) : static_module<ipc_module>(bar, move(name_)) {
m_router->register_action_with_data(EVENT_SEND, &ipc_module::action_send);
size_t index = 0;
for (auto&& command : m_conf.get_list<string>(name(), "hook")) {
for (auto&& command : m_conf.get_list<string>(name(), "hook", {})) {
m_hooks.emplace_back(std::make_unique<hook>(hook{name() + to_string(++index), command}));
}
if (m_hooks.empty()) {
throw module_error("No hooks defined");
}
m_log.info("%s: Loaded %d hooks", name(), m_hooks.size());
if ((m_initial = m_conf.get(name(), "initial", 0_z)) && m_initial > m_hooks.size()) {
throw module_error("Initial hook out of bounds (defined: " + to_string(m_hooks.size()) + ")");
@ -125,6 +125,11 @@ namespace modules {
broadcast();
}
}
void ipc_module::action_send(const string& data) {
m_output = data;
broadcast();
}
} // namespace modules
POLYBAR_NS_END