Merge pull request #1055 from vrld/feature-rewrite-window-title
Add option to rewrite sway/window title
This commit is contained in:
commit
66d8035ed1
|
@ -22,6 +22,7 @@ class Window : public ALabel, public sigc::trackable {
|
|||
std::tuple<std::size_t, int, std::string, std::string> getFocusedNode(const Json::Value& nodes,
|
||||
std::string& output);
|
||||
void getTree();
|
||||
std::string rewriteTitle(const std::string& title);
|
||||
|
||||
const Bar& bar_;
|
||||
std::string window_;
|
||||
|
|
|
@ -66,12 +66,32 @@ Addressed by *sway/window*
|
|||
default: true ++
|
||||
Option to disable tooltip on hover.
|
||||
|
||||
*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
|
||||
|
||||
```
|
||||
"sway/window": {
|
||||
"format": "{}",
|
||||
"max-length": 50
|
||||
"max-length": 50,
|
||||
"rewrite": {
|
||||
"(.*) - Mozilla Firefox": "🌎 $1",
|
||||
"(.*) - zsh": "> [$1]"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "modules/sway/window.hpp"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <regex>
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
|
||||
|
@ -56,7 +57,7 @@ auto Window::update() -> void {
|
|||
bar_.window.get_style_context()->remove_class("solo");
|
||||
bar_.window.get_style_context()->remove_class("empty");
|
||||
}
|
||||
label_.set_markup(fmt::format(format_, fmt::arg("title", window_),
|
||||
label_.set_markup(fmt::format(format_, fmt::arg("title", rewriteTitle(window_)),
|
||||
fmt::arg("app_id", app_id_)));
|
||||
if (tooltipEnabled()) {
|
||||
label_.set_tooltip_text(window_);
|
||||
|
@ -131,4 +132,29 @@ void Window::getTree() {
|
|||
}
|
||||
}
|
||||
|
||||
std::string Window::rewriteTitle(const std::string& title)
|
||||
{
|
||||
const auto& rules = config_["rewrite"];
|
||||
if (!rules.isObject()) {
|
||||
return 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)) {
|
||||
return std::regex_replace(title, rule, it->asString());
|
||||
}
|
||||
} catch (const std::regex_error& e) {
|
||||
spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return title;
|
||||
}
|
||||
|
||||
} // namespace waybar::modules::sway
|
||||
|
|
Loading…
Reference in New Issue