feat: add option to ignore workspaces based on regex

This commit is contained in:
Brenno Lemos 2023-10-11 10:59:33 -03:00
parent 30cc88a4c5
commit c995bafa7a
2 changed files with 46 additions and 9 deletions

View File

@ -9,6 +9,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <regex>
#include <string> #include <string>
#include <vector> #include <vector>
@ -94,6 +95,7 @@ class Workspaces : public AModule, public EventHandler {
std::string get_rewrite(std::string window_class); std::string get_rewrite(std::string window_class);
std::string& get_window_separator() { return format_window_separator_; } std::string& get_window_separator() { return format_window_separator_; }
bool is_workspace_ignored(std::string& workspace_name);
private: private:
void onEvent(const std::string&) override; void onEvent(const std::string&) override;
@ -140,6 +142,9 @@ class Workspaces : public AModule, public EventHandler {
std::vector<std::unique_ptr<Workspace>> workspaces_; std::vector<std::unique_ptr<Workspace>> workspaces_;
std::vector<Json::Value> workspaces_to_create_; std::vector<Json::Value> workspaces_to_create_;
std::vector<std::string> workspaces_to_remove_; std::vector<std::string> workspaces_to_remove_;
std::vector<std::regex> ignore_workspaces_;
std::mutex mutex_; std::mutex mutex_;
const Bar& bar_; const Bar& bar_;
Gtk::Box box_; Gtk::Box box_;

View File

@ -73,6 +73,23 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
} }
} }
Json::Value ignore_workspaces = config["ignore-workspaces"];
if (ignore_workspaces.isArray()) {
for (Json::Value &workspace_regex : ignore_workspaces) {
if (workspace_regex.isString()) {
std::string rule_string = workspace_regex.asString();
try {
const std::regex rule{rule_string, std::regex_constants::icase};
ignore_workspaces_.emplace_back(rule);
} catch (const std::regex_error &e) {
spdlog::error("Invalid rule {}: {}", rule_string, e.what());
}
} else {
spdlog::error("Not a string: '{}'", workspace_regex);
}
}
}
Json::Value format_window_separator = config["format-window-separator"]; Json::Value format_window_separator = config["format-window-separator"];
format_window_separator_ = format_window_separator_ =
format_window_separator.isString() ? format_window_separator.asString() : " "; format_window_separator.isString() ? format_window_separator.asString() : " ";
@ -156,6 +173,17 @@ bool isDoubleSpecial(std::string &workspace_name) {
return workspace_name.find("special:special:") != std::string::npos; return workspace_name.find("special:special:") != std::string::npos;
} }
bool Workspaces::is_workspace_ignored(std::string &name) {
for (auto &rule : ignore_workspaces_) {
if (std::regex_match(name, rule)) {
return true;
break;
}
}
return false;
}
void Workspaces::onEvent(const std::string &ev) { void Workspaces::onEvent(const std::string &ev) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>')); std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>'));
@ -170,16 +198,18 @@ void Workspaces::onEvent(const std::string &ev) {
} }
} else if (eventName == "createworkspace") { } else if (eventName == "createworkspace") {
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
for (Json::Value workspace_json : workspaces_json) {
std::string name = workspace_json["name"].asString(); if (!is_workspace_ignored(payload)) {
if (name == payload && for (Json::Value workspace_json : workspaces_json) {
(all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && std::string name = workspace_json["name"].asString();
(show_special() || !name.starts_with("special")) && !isDoubleSpecial(payload)) { if (name == payload &&
workspaces_to_create_.push_back(workspace_json); (all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) &&
break; (show_special() || !name.starts_with("special")) && !isDoubleSpecial(payload)) {
workspaces_to_create_.push_back(workspace_json);
break;
}
} }
} }
} else if (eventName == "focusedmon") { } else if (eventName == "focusedmon") {
active_workspace_name_ = payload.substr(payload.find(',') + 1); active_workspace_name_ = payload.substr(payload.find(',') + 1);
@ -511,8 +541,10 @@ void Workspaces::init() {
const Json::Value clients_json = gIPC->getSocket1JsonReply("clients"); const Json::Value clients_json = gIPC->getSocket1JsonReply("clients");
for (Json::Value workspace_json : workspaces_json) { for (Json::Value workspace_json : workspaces_json) {
std::string workspace_name = workspace_json["name"].asString();
if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) &&
(!workspace_json["name"].asString().starts_with("special") || show_special())) { (!workspace_name.starts_with("special") || show_special()) &&
!is_workspace_ignored(workspace_name)) {
create_workspace(workspace_json, clients_json); create_workspace(workspace_json, clients_json);
} }
} }