From 54b75f23ab6bc363ffebb10beb9bd9aeb9d74087 Mon Sep 17 00:00:00 2001 From: Madhav Prabhu C M <59736487+madhavpcm@users.noreply.github.com> Date: Sun, 9 Oct 2022 22:12:36 +0530 Subject: [PATCH] feat(ipc). Per hook format (#2810) * Added format-x support for ipc_module, tested with demo config * Certain cases not working * Certain cases not working, build * Changed label to output tag, Mixing of default formats wont work * created changelog --- CHANGELOG.md | 1 + include/modules/ipc.hpp | 1 + src/modules/ipc.cpp | 20 ++++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13701c47..98aea186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `custom/text`: The `content` setting and all its properties are deprecated in favor of `format` with the same functionality. ([`#2676`](https://github.com/polybar/polybar/pull/2676)) ### Added +- Added support for format-i for each hook-i defined in ipc module ([`#2775`](https://github.com/polybar/polybar/issues/2775), [`#2810`](https://github.com/polybar/polybar/pull/2810)) by [@madhavpcm](https://github.com/madhavpcm). - `internal/temperature`: `%temperature-k%` token displays the temperature in kelvin ([`#2774`](https://github.com/polybar/polybar/discussions/2774), [`#2784`](https://github.com/polybar/polybar/pull/2784)) - `internal/pulseaudio`: `reverse-scroll` option ([`#2664`](https://github.com/polybar/polybar/pull/2664)) - `custom/script`: Repeat interval for script failure (`interval-fail`) and `exec-if` (`interval-if`) ([`#943`](https://github.com/polybar/polybar/issues/943), [`#2606`](https://github.com/polybar/polybar/issues/2606), [`#2630`](https://github.com/polybar/polybar/pull/2630)) diff --git a/include/modules/ipc.hpp b/include/modules/ipc.hpp index b59f17c4..11fdcff4 100644 --- a/include/modules/ipc.hpp +++ b/include/modules/ipc.hpp @@ -29,6 +29,7 @@ namespace modules { void start() override; void update(); string get_output(); + string get_format() const; bool build(builder* builder, const string& tag) const; void on_message(const string& message); diff --git a/src/modules/ipc.cpp b/src/modules/ipc.cpp index b8627036..41ee3fea 100644 --- a/src/modules/ipc.cpp +++ b/src/modules/ipc.cpp @@ -59,8 +59,12 @@ namespace modules { for (auto& hook : m_hooks) { hook->command = pid_token(hook->command); } - m_formatter->add(DEFAULT_FORMAT, TAG_OUTPUT, {TAG_OUTPUT}); + + for (size_t i = 0; i < m_hooks.size(); i++) { + string format_i = "format-" + to_string(i); + m_formatter->add_optional(format_i, {TAG_OUTPUT}); + } } /** @@ -103,6 +107,17 @@ namespace modules { return m_builder->flush(); } + string ipc_module::get_format() const { + if (m_current_hook != -1 && (size_t)m_current_hook < m_hooks.size()) { + string format_i = "format-" + to_string(m_current_hook); + if (m_formatter->has_format(format_i)) { + return format_i; + } else { + return DEFAULT_FORMAT; + } + } + return DEFAULT_FORMAT; + } /** * Output content retrieved from hook commands */ @@ -150,7 +165,8 @@ namespace modules { } } catch (const std::invalid_argument& err) { m_log.err( - "%s: Hook action received '%s' cannot be converted to a valid hook index. Defined hooks goes from 0 to %zu.", + "%s: Hook action received '%s' cannot be converted to a valid hook index. Defined hooks goes from 0 to " + "%zu.", name(), data, m_hooks.size() - 1); } }