Add rewrite option to hyprland/window

pull/1730/head
herlev 2022-10-18 13:18:43 +02:00
parent a7e6330078
commit 97ae2ff343
2 changed files with 29 additions and 1 deletions

View File

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

View File

@ -2,6 +2,7 @@
#include <spdlog/spdlog.h>
#include <regex>
#include <util/sanitize_str.hpp>
#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