From 97ae2ff343e44a2fb9898312deed81c3499e3f05 Mon Sep 17 00:00:00 2001 From: herlev <> Date: Tue, 18 Oct 2022 13:18:43 +0200 Subject: [PATCH 1/5] 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 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 From 59e7f1974c0b3eec4d1f1ba371daae040cac362d Mon Sep 17 00:00:00 2001 From: herlev <> Date: Tue, 18 Oct 2022 13:21:20 +0200 Subject: [PATCH 2/5] Document hyprland/window rewrite option --- man/waybar-hyprland-window.5.scd | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/man/waybar-hyprland-window.5.scd b/man/waybar-hyprland-window.5.scd index 4be137d..0135d7c 100644 --- a/man/waybar-hyprland-window.5.scd +++ b/man/waybar-hyprland-window.5.scd @@ -17,12 +17,31 @@ Addressed by *hyprland/window* default: {} ++ The format, how information should be displayed. On {} the current window title is displayed. +*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 ``` "hyprland/window": { - "format": "{}" + "format": "{}", + "rewrite": { + "(.*) - Mozilla Firefox": "🌎 $1", + "(.*) - zsh": "> [$1]" + } } ``` From 54e04b5a30228e1c7c95c714bcf0fe178684b96d Mon Sep 17 00:00:00 2001 From: herlev <> Date: Wed, 19 Oct 2022 13:25:08 +0200 Subject: [PATCH 3/5] Refactor rewriteTitle --- include/modules/hyprland/window.hpp | 1 - include/modules/sway/window.hpp | 1 - include/util/rewrite_title.hpp | 8 ++++++++ meson.build | 3 ++- src/modules/hyprland/window.cpp | 32 +++-------------------------- src/modules/sway/window.cpp | 29 ++------------------------ src/util/rewrite_title.cpp | 32 +++++++++++++++++++++++++++++ 7 files changed, 47 insertions(+), 59 deletions(-) create mode 100644 include/util/rewrite_title.hpp create mode 100644 src/util/rewrite_title.cpp diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index 2c2ed97..1423aec 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -19,7 +19,6 @@ 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/include/modules/sway/window.hpp b/include/modules/sway/window.hpp index c99ba7f..c13d5ce 100644 --- a/include/modules/sway/window.hpp +++ b/include/modules/sway/window.hpp @@ -24,7 +24,6 @@ class Window : public AIconLabel, public sigc::trackable { std::tuple getFocusedNode( const Json::Value& nodes, std::string& output); void getTree(); - std::string rewriteTitle(const std::string& title); void updateAppIconName(); void updateAppIcon(); diff --git a/include/util/rewrite_title.hpp b/include/util/rewrite_title.hpp new file mode 100644 index 0000000..c477339 --- /dev/null +++ b/include/util/rewrite_title.hpp @@ -0,0 +1,8 @@ +#pragma once +#include + +#include + +namespace waybar::util { +std::string rewriteTitle(const std::string&, const Json::Value&); +} diff --git a/meson.build b/meson.build index e537bcc..e72d99f 100644 --- a/meson.build +++ b/meson.build @@ -157,7 +157,8 @@ src_files = files( 'src/config.cpp', 'src/group.cpp', 'src/util/ustring_clen.cpp', - 'src/util/sanitize_str.cpp' + 'src/util/sanitize_str.cpp', + 'src/util/rewrite_title.cpp' ) if is_linux diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index 0d22fe0..e49561d 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -8,6 +8,7 @@ #include "modules/hyprland/backend.hpp" #include "util/command.hpp" #include "util/json.hpp" +#include "util/rewrite_title.hpp" namespace waybar::modules::hyprland { @@ -33,7 +34,7 @@ auto Window::update() -> void { if (!format_.empty()) { label_.show(); - label_.set_markup(fmt::format(format_, rewriteTitle(lastView))); + label_.set_markup(fmt::format(format_, waybar::util::rewriteTitle(lastView, config_["rewrite"]))); } else { label_.hide(); } @@ -61,7 +62,7 @@ std::string Window::getLastWindowTitle(uint workspaceID) { return workspace["id"].as() == workspaceID; }); - if (workspace != std::end(json)) { + if (workspace == std::end(json)) { return ""; } return (*workspace)["lastwindowtitle"].as(); @@ -87,31 +88,4 @@ 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 diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 3d63743..499c08c 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -1,4 +1,5 @@ #include "modules/sway/window.hpp" +#include "util/rewrite_title.hpp" #include #include @@ -175,7 +176,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", rewriteTitle(window_)), + label_.set_markup(fmt::format(format_, fmt::arg("title", waybar::util::rewriteTitle(window_, config_["rewrite"])), fmt::arg("app_id", app_id_), fmt::arg("shell", shell_))); if (tooltipEnabled()) { label_.set_tooltip_text(window_); @@ -262,30 +263,4 @@ void Window::getTree() { } } -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::sway diff --git a/src/util/rewrite_title.cpp b/src/util/rewrite_title.cpp new file mode 100644 index 0000000..fae59bb --- /dev/null +++ b/src/util/rewrite_title.cpp @@ -0,0 +1,32 @@ +#include "util/rewrite_title.hpp" + +#include + +#include + +namespace waybar::util { +std::string rewriteTitle(const std::string& title, const Json::Value& rules) { + 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::util From e660a3634d3599a3703555bde47ecb9875eb4570 Mon Sep 17 00:00:00 2001 From: herlev <> Date: Wed, 19 Oct 2022 13:29:05 +0200 Subject: [PATCH 4/5] Fix linter --- src/modules/sway/window.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/modules/sway/window.cpp b/src/modules/sway/window.cpp index 499c08c..5da7d3d 100644 --- a/src/modules/sway/window.cpp +++ b/src/modules/sway/window.cpp @@ -1,5 +1,4 @@ #include "modules/sway/window.hpp" -#include "util/rewrite_title.hpp" #include #include @@ -13,6 +12,8 @@ #include #include +#include "util/rewrite_title.hpp" + namespace waybar::modules::sway { Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) @@ -176,8 +177,9 @@ 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", waybar::util::rewriteTitle(window_, config_["rewrite"])), - fmt::arg("app_id", app_id_), fmt::arg("shell", shell_))); + label_.set_markup(fmt::format( + format_, fmt::arg("title", waybar::util::rewriteTitle(window_, config_["rewrite"])), + fmt::arg("app_id", app_id_), fmt::arg("shell", shell_))); if (tooltipEnabled()) { label_.set_tooltip_text(window_); } From e1045381fed839105662b27f07cb8b43d2b0e836 Mon Sep 17 00:00:00 2001 From: herlev <> Date: Wed, 19 Oct 2022 13:30:28 +0200 Subject: [PATCH 5/5] Fix linter --- src/modules/hyprland/window.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index e49561d..d942cf6 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -34,7 +34,8 @@ auto Window::update() -> void { if (!format_.empty()) { label_.show(); - label_.set_markup(fmt::format(format_, waybar::util::rewriteTitle(lastView, config_["rewrite"]))); + label_.set_markup( + fmt::format(format_, waybar::util::rewriteTitle(lastView, config_["rewrite"]))); } else { label_.hide(); }