Merge pull request #1730 from herlev/hyprland-window-rewrite
This commit is contained in:
commit
a24f2d72a7
|
@ -24,7 +24,6 @@ class Window : public AIconLabel, public sigc::trackable {
|
||||||
std::tuple<std::size_t, int, std::string, std::string, std::string, std::string> getFocusedNode(
|
std::tuple<std::size_t, int, std::string, std::string, std::string, std::string> getFocusedNode(
|
||||||
const Json::Value& nodes, std::string& output);
|
const Json::Value& nodes, std::string& output);
|
||||||
void getTree();
|
void getTree();
|
||||||
std::string rewriteTitle(const std::string& title);
|
|
||||||
void updateAppIconName();
|
void updateAppIconName();
|
||||||
void updateAppIcon();
|
void updateAppIcon();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
#include <json/json.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace waybar::util {
|
||||||
|
std::string rewriteTitle(const std::string&, const Json::Value&);
|
||||||
|
}
|
|
@ -17,12 +17,31 @@ Addressed by *hyprland/window*
|
||||||
default: {} ++
|
default: {} ++
|
||||||
The format, how information should be displayed. On {} the current window title is displayed.
|
The format, how information should be displayed. On {} the current window title is displayed.
|
||||||
|
|
||||||
|
*rewrite*: ++
|
||||||
|
typeof: object ++
|
||||||
|
Rules to rewrite window title. See *rewrite rules*.
|
||||||
|
|
||||||
|
# REWRITE RULES
|
||||||
|
|
||||||
|
*rewrite* is an object where keys are regular expressions and values are
|
||||||
|
rewrite rules if the expression matches. Rules may contain references to
|
||||||
|
captures of the expression.
|
||||||
|
|
||||||
|
Regular expression and replacement follow ECMA-script rules.
|
||||||
|
|
||||||
|
If no expression matches, the title is left unchanged.
|
||||||
|
|
||||||
|
Invalid expressions (e.g., mismatched parentheses) are skipped.
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
|
|
||||||
```
|
```
|
||||||
"hyprland/window": {
|
"hyprland/window": {
|
||||||
"format": "{}"
|
"format": "{}",
|
||||||
|
"rewrite": {
|
||||||
|
"(.*) - Mozilla Firefox": "🌎 $1",
|
||||||
|
"(.*) - zsh": "> [$1]"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -157,7 +157,8 @@ src_files = files(
|
||||||
'src/config.cpp',
|
'src/config.cpp',
|
||||||
'src/group.cpp',
|
'src/group.cpp',
|
||||||
'src/util/ustring_clen.cpp',
|
'src/util/ustring_clen.cpp',
|
||||||
'src/util/sanitize_str.cpp'
|
'src/util/sanitize_str.cpp',
|
||||||
|
'src/util/rewrite_title.cpp'
|
||||||
)
|
)
|
||||||
|
|
||||||
if is_linux
|
if is_linux
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
#include <util/sanitize_str.hpp>
|
#include <util/sanitize_str.hpp>
|
||||||
|
|
||||||
#include "modules/hyprland/backend.hpp"
|
#include "modules/hyprland/backend.hpp"
|
||||||
#include "util/command.hpp"
|
#include "util/command.hpp"
|
||||||
#include "util/json.hpp"
|
#include "util/json.hpp"
|
||||||
|
#include "util/rewrite_title.hpp"
|
||||||
|
|
||||||
namespace waybar::modules::hyprland {
|
namespace waybar::modules::hyprland {
|
||||||
|
|
||||||
|
@ -32,7 +34,8 @@ auto Window::update() -> void {
|
||||||
|
|
||||||
if (!format_.empty()) {
|
if (!format_.empty()) {
|
||||||
label_.show();
|
label_.show();
|
||||||
label_.set_markup(fmt::format(format_, lastView));
|
label_.set_markup(
|
||||||
|
fmt::format(format_, waybar::util::rewriteTitle(lastView, config_["rewrite"])));
|
||||||
} else {
|
} else {
|
||||||
label_.hide();
|
label_.hide();
|
||||||
}
|
}
|
||||||
|
@ -60,7 +63,7 @@ std::string Window::getLastWindowTitle(uint workspaceID) {
|
||||||
return workspace["id"].as<uint>() == workspaceID;
|
return workspace["id"].as<uint>() == workspaceID;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (workspace != std::end(json)) {
|
if (workspace == std::end(json)) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return (*workspace)["lastwindowtitle"].as<std::string>();
|
return (*workspace)["lastwindowtitle"].as<std::string>();
|
||||||
|
@ -86,5 +89,4 @@ void Window::onEvent(const std::string& ev) {
|
||||||
|
|
||||||
dp.emit();
|
dp.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace waybar::modules::hyprland
|
} // namespace waybar::modules::hyprland
|
|
@ -12,6 +12,8 @@
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "util/rewrite_title.hpp"
|
||||||
|
|
||||||
namespace waybar::modules::sway {
|
namespace waybar::modules::sway {
|
||||||
|
|
||||||
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||||
|
@ -175,8 +177,9 @@ auto Window::update() -> void {
|
||||||
bar_.window.get_style_context()->remove_class("solo");
|
bar_.window.get_style_context()->remove_class("solo");
|
||||||
bar_.window.get_style_context()->remove_class("empty");
|
bar_.window.get_style_context()->remove_class("empty");
|
||||||
}
|
}
|
||||||
label_.set_markup(fmt::format(format_, fmt::arg("title", rewriteTitle(window_)),
|
label_.set_markup(fmt::format(
|
||||||
fmt::arg("app_id", app_id_), fmt::arg("shell", shell_)));
|
format_, fmt::arg("title", waybar::util::rewriteTitle(window_, config_["rewrite"])),
|
||||||
|
fmt::arg("app_id", app_id_), fmt::arg("shell", shell_)));
|
||||||
if (tooltipEnabled()) {
|
if (tooltipEnabled()) {
|
||||||
label_.set_tooltip_text(window_);
|
label_.set_tooltip_text(window_);
|
||||||
}
|
}
|
||||||
|
@ -262,30 +265,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
|
} // namespace waybar::modules::sway
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue