mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Refactor rewriteTitle
This commit is contained in:
32
src/util/rewrite_title.cpp
Normal file
32
src/util/rewrite_title.cpp
Normal 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
|
Reference in New Issue
Block a user