utils: add sanitize_str to encode '&' etc.

gtk requires some chars (<>&"') to be encoded for them to render
properly. `sanitize_str` sanitizes raw strings that have such chars and
returns a properly encoded string
This commit is contained in:
Mika Braunschweig
2022-10-09 19:13:54 +02:00
parent 5da45ece9d
commit f86dff60e6
6 changed files with 47 additions and 29 deletions

View File

@ -2,6 +2,8 @@
#include <spdlog/spdlog.h>
#include <util/sanitize_str.hpp>
#include "modules/hyprland/backend.hpp"
namespace waybar::modules::hyprland {
@ -39,17 +41,7 @@ void Window::onEvent(const std::string& ev) {
std::lock_guard<std::mutex> lg(mutex_);
auto windowName = ev.substr(ev.find_first_of(',') + 1).substr(0, 256);
auto replaceAll = [](std::string str, const std::string& from,
const std::string& to) -> std::string {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
return str;
};
windowName = replaceAll(windowName, "&", "&amp;");
windowName = waybar::util::sanitize_string(windowName);
if (windowName == lastView) return;