Refactor rewriteTitle

This commit is contained in:
herlev
2022-10-19 13:25:08 +02:00
parent f72c1a54d3
commit 54e04b5a30
7 changed files with 47 additions and 59 deletions

View File

@ -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<uint>() == workspaceID;
});
if (workspace != std::end(json)) {
if (workspace == std::end(json)) {
return "";
}
return (*workspace)["lastwindowtitle"].as<std::string>();
@ -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

View File

@ -1,4 +1,5 @@
#include "modules/sway/window.hpp"
#include "util/rewrite_title.hpp"
#include <gdkmm/pixbuf.h>
#include <glibmm/fileutils.h>
@ -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

View File

@ -0,0 +1,32 @@
#include "util/rewrite_title.hpp"
#include <spdlog/spdlog.h>
#include <regex>
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