feat(niri/worspaces): add ignore-workspaces configuration option
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <gtkmm/button.h>
|
#include <gtkmm/button.h>
|
||||||
#include <json/value.h>
|
#include <json/value.h>
|
||||||
|
#include <regex>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "AModule.hpp"
|
#include "AModule.hpp"
|
||||||
#include "bar.hpp"
|
#include "bar.hpp"
|
||||||
@@ -20,11 +22,14 @@ class Workspaces : public AModule, public EventHandler {
|
|||||||
void doUpdate();
|
void doUpdate();
|
||||||
Gtk::Button &addButton(const Json::Value &ws);
|
Gtk::Button &addButton(const Json::Value &ws);
|
||||||
std::string getIcon(const std::string &value, const Json::Value &ws);
|
std::string getIcon(const std::string &value, const Json::Value &ws);
|
||||||
|
bool isWorkspaceIgnored(const std::string &name);
|
||||||
|
|
||||||
const Bar &bar_;
|
const Bar &bar_;
|
||||||
Gtk::Box box_;
|
Gtk::Box box_;
|
||||||
// Map from niri workspace id to button.
|
// Map from niri workspace id to button.
|
||||||
std::unordered_map<uint64_t, Gtk::Button> buttons_;
|
std::unordered_map<uint64_t, Gtk::Button> buttons_;
|
||||||
|
// Vec of regex rules to ignore workspaces.
|
||||||
|
std::vector<std::regex> ignoreWorkspaces_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules::niri
|
} // namespace waybar::modules::niri
|
||||||
|
|||||||
@@ -50,6 +50,11 @@ Addressed by *niri/workspaces*
|
|||||||
default: false ++
|
default: false ++
|
||||||
Enables this module to consume all left over space dynamically.
|
Enables this module to consume all left over space dynamically.
|
||||||
|
|
||||||
|
*ignore-workspaces*: ++
|
||||||
|
typeof: array ++
|
||||||
|
default: [] ++
|
||||||
|
Regexes to match against workspaces names and index's. If there's a match, the workspace will not be shown.
|
||||||
|
|
||||||
# FORMAT REPLACEMENTS
|
# FORMAT REPLACEMENTS
|
||||||
|
|
||||||
*{value}*: Name of the workspace, or index for unnamed workspaces,
|
*{value}*: Name of the workspace, or index for unnamed workspaces,
|
||||||
|
|||||||
@@ -15,6 +15,24 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
|
|||||||
box_.get_style_context()->add_class(MODULE_CLASS);
|
box_.get_style_context()->add_class(MODULE_CLASS);
|
||||||
event_box_.add(box_);
|
event_box_.add(box_);
|
||||||
|
|
||||||
|
// Parse ignore-workspaces rules.
|
||||||
|
auto ignoreWorkspaces = config["ignore-workspaces"];
|
||||||
|
if (ignoreWorkspaces.isArray()) {
|
||||||
|
for (const auto &workspaceRegex : ignoreWorkspaces) {
|
||||||
|
if (workspaceRegex.isString()) {
|
||||||
|
std::string ruleString = workspaceRegex.asString();
|
||||||
|
try {
|
||||||
|
const std::regex rule{ruleString, std::regex_constants::icase};
|
||||||
|
ignoreWorkspaces_.emplace_back(rule);
|
||||||
|
} catch (const std::regex_error &e) {
|
||||||
|
spdlog::error("Invalid ignore-workspaces rule {}: {}", ruleString, e.what());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
spdlog::error("Not a string in ignore-workspaces: '{}'", workspaceRegex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!gIPC) gIPC = std::make_unique<IPC>();
|
if (!gIPC) gIPC = std::make_unique<IPC>();
|
||||||
|
|
||||||
gIPC->registerForIPC("WorkspacesChanged", this);
|
gIPC->registerForIPC("WorkspacesChanged", this);
|
||||||
@@ -37,6 +55,16 @@ void Workspaces::doUpdate() {
|
|||||||
const auto &workspaces = gIPC->workspaces();
|
const auto &workspaces = gIPC->workspaces();
|
||||||
std::copy_if(workspaces.cbegin(), workspaces.cend(), std::back_inserter(my_workspaces),
|
std::copy_if(workspaces.cbegin(), workspaces.cend(), std::back_inserter(my_workspaces),
|
||||||
[&](const auto &ws) {
|
[&](const auto &ws) {
|
||||||
|
std::string name;
|
||||||
|
if (ws["name"]) {
|
||||||
|
name = ws["name"].asString();
|
||||||
|
} else {
|
||||||
|
name = std::to_string(ws["idx"].asUInt());
|
||||||
|
}
|
||||||
|
if (isWorkspaceIgnored(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (alloutputs) return true;
|
if (alloutputs) return true;
|
||||||
return ws["output"].asString() == bar_.output->name;
|
return ws["output"].asString() == bar_.output->name;
|
||||||
});
|
});
|
||||||
@@ -193,4 +221,13 @@ std::string Workspaces::getIcon(const std::string &value, const Json::Value &ws)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Workspaces::isWorkspaceIgnored(const std::string &name) {
|
||||||
|
for (auto &rule : ignoreWorkspaces_) {
|
||||||
|
if (std::regex_match(name, rule)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace waybar::modules::niri
|
} // namespace waybar::modules::niri
|
||||||
|
|||||||
Reference in New Issue
Block a user