diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index 40aaa1a..0f7ae31 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -22,6 +22,7 @@ class Window : public ALabel, public sigc::trackable { std::tuple getFocusedNode(const Json::Value& nodes, std::string& output); void getTree(); + std::string rewriteTitle(const std::string& title); const Bar& bar_; std::string window_; diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index d8f113f..3b424c2 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -1,5 +1,6 @@ #include "modules/sway/window.hpp" #include +#include namespace waybar::modules::sway { @@ -56,7 +57,7 @@ auto Window::update() -> void { bar_.window.get_style_context()->remove_class("solo"); bar_.window.get_style_context()->remove_class("empty"); } - label_.set_markup(fmt::format(format_, fmt::arg("title", window_), + label_.set_markup(fmt::format(format_, fmt::arg("title", rewriteTitle(window_)), fmt::arg("app_id", app_id_))); if (tooltipEnabled()) { label_.set_tooltip_text(window_); @@ -131,4 +132,23 @@ void Window::getTree() { } } +std::string Window::rewriteTitle(const std::string& title) +{ + const auto& rules = config_["rewrite"]; + if (!rules.isObject()) { + return title; + } + + for (auto it = rules.begin(); it != rules.end(); ++it) { + if (it.key().isString() && it->isString()) { + const std::regex rule{it.key().asString()}; + if (std::regex_match(title, rule)) { + return std::regex_replace(title, rule, it->asString()); + } + } + } + + return title; +} + } // namespace waybar::modules::sway