From 97ae2ff343e44a2fb9898312deed81c3499e3f05 Mon Sep 17 00:00:00 2001 From: herlev <> Date: Tue, 18 Oct 2022 13:18:43 +0200 Subject: [PATCH] Add rewrite option to hyprland/window --- include/modules/hyprland/window.hpp | 1 + src/modules/hyprland/window.cpp | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 1423aece..2c2ed972 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -19,6 +19,7 @@ class Window : public waybar::ALabel { private: uint getActiveWorkspaceID(std::string); std::string getLastWindowTitle(uint); + std::string rewriteTitle(const std::string&); void onEvent(const std::string&); bool separate_outputs; diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index cd1584d3..20e438d1 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -2,6 +2,7 @@ #include +#include #include #include "modules/hyprland/backend.hpp" @@ -32,7 +33,7 @@ auto Window::update() -> void { if (!format_.empty()) { label_.show(); - label_.set_markup(fmt::format(format_, lastView)); + label_.set_markup(fmt::format(format_, rewriteTitle(lastView))); } else { label_.hide(); } @@ -84,4 +85,30 @@ void Window::onEvent(const std::string& ev) { dp.emit(); } +std::string Window::rewriteTitle(const std::string& title) { + const auto& rules = config_["rewrite"]; + if (!rules.isObject()) { + return title; + } + + std::string res = title; + + for (auto it = rules.begin(); it != rules.end(); ++it) { + if (it.key().isString() && it->isString()) { + try { + // malformated regexes will cause an exception. + // in this case, log error and try the next rule. + const std::regex rule{it.key().asString()}; + if (std::regex_match(title, rule)) { + res = std::regex_replace(res, rule, it->asString()); + } + } catch (const std::regex_error& e) { + spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what()); + } + } + } + + return res; +} + } // namespace waybar::modules::hyprland \ No newline at end of file