diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 1423aec..2c2ed97 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 cd1584d..20e438d 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