From b16c8972c70d536df0e579beee8c9054f0990d6b Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Sun, 21 Mar 2021 11:42:25 +0100 Subject: [PATCH 1/3] Add option to rewrite sway/window title MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrites window title according to config option "rewrite". "rewrite" is an object where keys are regular expressions and values are rewrite rules if the expression matches. Rules may contain references to captures of the expression. Regex and replacement follow ECMA-script rules. If no regex matches, the title is left unchanged. example: "sway/window": { "rewrite": { "(.*) - Mozilla Firefox": " $1", "(.*) - zsh": " $1", } } --- include/modules/sway/window.hpp | 1 + src/modules/sway/window.cpp | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index 40aaa1a0..0f7ae317 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 d8f113f7..3b424c2f 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 From af3c868a5b63b1e77b56801ca558f76afcc21ed6 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 21 Apr 2021 12:15:25 +0200 Subject: [PATCH 2/3] Catch exception on erroneous rules std::regex and std::regex_replace may throw an std::regex_error if the expression or replacement contain errors. Log this error and carry on with the next rule, so that the title is shown even if the config contains errors. --- src/modules/sway/window.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 3b424c2f..04910394 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -141,9 +141,15 @@ std::string Window::rewriteTitle(const std::string& 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()); + 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)) { + return std::regex_replace(title, rule, it->asString()); + } + } catch (const std::regex_error& e) { + spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what()); } } } From 7cdf178f8d7c3caec57b57bd2f539e4982d83a9a Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 21 Apr 2021 12:18:33 +0200 Subject: [PATCH 3/3] Document changes in manpage Add section on rewrite rules and extend example --- man/waybar-sway-window.5.scd | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/man/waybar-sway-window.5.scd b/man/waybar-sway-window.5.scd index 40250e6f..ea060696 100644 --- a/man/waybar-sway-window.5.scd +++ b/man/waybar-sway-window.5.scd @@ -66,12 +66,32 @@ Addressed by *sway/window* default: true ++ Option to disable tooltip on hover. +*rewrite*: ++ + typeof: object ++ + Rules to rewrite window title. See *rewrite rules*. + +# REWRITE RULES + +*rewrite* is an object where keys are regular expressions and values are +rewrite rules if the expression matches. Rules may contain references to +captures of the expression. + +Regular expression and replacement follow ECMA-script rules. + +If no expression matches, the title is left unchanged. + +Invalid expressions (e.g., mismatched parentheses) are skipped. + # EXAMPLES ``` "sway/window": { "format": "{}", - "max-length": 50 + "max-length": 50, + "rewrite": { + "(.*) - Mozilla Firefox": "🌎 $1", + "(.*) - zsh": "> [$1]" + } } ```