feat: optional default icon for 0-match classes

Co-authored-by: Gabriel Fox <Inbox@GabrielFox.Dev>
This commit is contained in:
Brenno Lemos 2023-09-22 19:05:34 -03:00
parent fbe544984c
commit adbc9d95de
4 changed files with 42 additions and 2 deletions

View File

@ -136,6 +136,7 @@ class Workspaces : public AModule, public EventHandler {
Json::Value window_rewrite_rules_;
std::map<std::string, std::string> regex_cache_;
std::string format_window_separator_;
std::string window_rewrite_default_;
bool with_icon_;
uint64_t monitor_id_;
std::string active_workspace_name_;

View File

@ -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

View File

@ -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);

View File

@ -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