diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 528bf933..75465994 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -136,6 +136,7 @@ class Workspaces : public AModule, public EventHandler { Json::Value window_rewrite_rules_; std::map regex_cache_; std::string format_window_separator_; + std::string window_rewrite_default_; bool with_icon_; uint64_t monitor_id_; std::string active_workspace_name_; diff --git a/include/util/rewrite_string.hpp b/include/util/rewrite_string.hpp index 2ab39ad8..3352a47a 100644 --- a/include/util/rewrite_string.hpp +++ b/include/util/rewrite_string.hpp @@ -5,4 +5,6 @@ namespace waybar::util { std::string rewriteString(const std::string&, const Json::Value&); -} +std::string rewriteStringOnce(const std::string& value, const Json::Value& rules, + bool& matched_any); +} // namespace waybar::util diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index e6edc4ac..526129e9 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -77,6 +77,10 @@ auto Workspaces::parse_config(const Json::Value &config) -> void { format_window_separator.isString() ? format_window_separator.asString() : " "; window_rewrite_rules_ = config["window-rewrite"]; + + Json::Value window_rewrite_default = config["window-rewrite-default"]; + window_rewrite_default_ = + window_rewrite_default.isString() ? window_rewrite_default.asString() : "?"; } auto Workspaces::register_ipc() -> void { @@ -757,8 +761,14 @@ std::string Workspaces::get_rewrite(std::string window_class) { return regex_cache_[window_class]; } + bool matched_any; + std::string window_class_rewrite = - waybar::util::rewriteString(window_class, window_rewrite_rules_); + waybar::util::rewriteStringOnce(window_class, window_rewrite_rules_, matched_any); + + if (!matched_any) { + window_class_rewrite = window_rewrite_default_; + } regex_cache_.emplace(window_class, window_class_rewrite); diff --git a/src/util/rewrite_string.cpp b/src/util/rewrite_string.cpp index ec3e1673..475f8a3d 100644 --- a/src/util/rewrite_string.cpp +++ b/src/util/rewrite_string.cpp @@ -30,4 +30,31 @@ std::string rewriteString(const std::string& value, const Json::Value& rules) { return res; } + +std::string rewriteStringOnce(const std::string& value, const Json::Value& rules, + bool& matched_any) { + if (!rules.isObject()) { + return value; + } + + matched_any = false; + + std::string res = value; + + for (auto it = rules.begin(); it != rules.end(); ++it) { + if (it.key().isString() && it->isString()) { + try { + const std::regex rule{it.key().asString(), std::regex_constants::icase}; + if (std::regex_match(value, rule)) { + matched_any = true; + return std::regex_replace(res, rule, it->asString()); + } + } catch (const std::regex_error& e) { + spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what()); + } + } + } + + return value; +} } // namespace waybar::util