refactor: separate regex rule matching and caching in separate class

This commit is contained in:
Brenno Lemos
2023-10-09 13:53:00 -03:00
parent 30cc88a4c5
commit 8d057e6f96
6 changed files with 149 additions and 62 deletions

View File

@ -16,8 +16,10 @@
#include "bar.hpp"
#include "modules/hyprland/backend.hpp"
#include "util/enum.hpp"
#include "util/regex_collection.hpp"
using WindowAddress = std::string;
namespace waybar::modules::hyprland {
class Workspaces;
@ -47,7 +49,7 @@ class Workspace {
void set_windows(uint value) { windows_ = value; };
void set_name(std::string value) { name_ = value; };
bool contains_window(WindowAddress addr) { return window_map_.contains(addr); }
void insert_window(WindowAddress addr, std::string window_repr);
void insert_window(WindowAddress addr, std::string window_class, std::string window_title);
std::string remove_window(WindowAddress addr);
void initialize_window_map(const Json::Value& clients_data);
@ -92,7 +94,7 @@ class Workspaces : public AModule, public EventHandler {
auto get_bar_output() const -> std::string { return bar_.output->name; }
std::string get_rewrite(std::string window_class);
std::string get_rewrite(std::string window_class, std::string window_title);
std::string& get_window_separator() { return format_window_separator_; }
private:
@ -129,11 +131,12 @@ class Workspaces : public AModule, public EventHandler {
bool persistent_created_ = false;
std::string format_;
std::map<std::string, std::string> icons_map_;
Json::Value window_rewrite_rules_;
std::map<std::string, std::string> regex_cache_;
util::RegexCollection window_rewrite_rules_;
bool any_window_rewrite_rule_uses_title_ = false;
std::string format_window_separator_;
std::string window_rewrite_default_;
bool with_icon_;
uint64_t monitor_id_;
std::string active_workspace_name_;

View File

@ -0,0 +1,37 @@
#pragma once
#include <json/json.h>
#include <functional>
#include <regex>
#include <string>
namespace waybar::util {
struct Rule {
std::regex rule;
std::string repr;
int priority;
};
int default_priority_function(std::string& key);
class RegexCollection {
private:
std::vector<Rule> rules;
std::map<std::string, std::string> regex_cache;
std::string default_repr;
std::string& find_match(std::string& value, bool& matched_any);
public:
RegexCollection() = default;
RegexCollection(const Json::Value& map, std::string default_repr = "",
std::function<int(std::string&)> priority_function = default_priority_function);
~RegexCollection() = default;
std::string& get(std::string& value, bool& matched_any);
std::string& get(std::string& value);
};
} // namespace waybar::util