misc: use hyprutils for string replaceAll (#512)

This commit is contained in:
Maximilian Seidler 2024-10-11 15:44:47 +00:00 committed by GitHub
parent 264fb8b70f
commit 7362ce3435
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 29 deletions

View file

@ -5,6 +5,7 @@
#include <chrono> #include <chrono>
#include <unistd.h> #include <unistd.h>
#include <pwd.h> #include <pwd.h>
#include <hyprutils/string/String.hpp>
#include <hyprutils/string/VarList.hpp> #include <hyprutils/string/VarList.hpp>
using namespace Hyprutils::String; using namespace Hyprutils::String;
@ -48,16 +49,6 @@ Vector2D IWidget::posFromHVAlign(const Vector2D& viewport, const Vector2D& size,
return pos; return pos;
} }
static void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if (from.empty())
return;
size_t pos = 0;
while ((pos = str.find(from, pos)) != std::string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
}
static void replaceAllAttempts(std::string& str) { static void replaceAllAttempts(std::string& str) {
const size_t ATTEMPTS = g_pHyprlock->getPasswordFailedAttempts(); const size_t ATTEMPTS = g_pHyprlock->getPasswordFailedAttempts();
@ -133,29 +124,29 @@ IWidget::SFormatResult IWidget::formatString(std::string in) {
Debug::log(WARN, "Error in formatString, user_gecos null. Errno: ", errno); Debug::log(WARN, "Error in formatString, user_gecos null. Errno: ", errno);
IWidget::SFormatResult result; IWidget::SFormatResult result;
replaceAll(in, "$DESC", std::string{user_gecos ? user_gecos : ""}); replaceInString(in, "$DESC", std::string{user_gecos ? user_gecos : ""});
replaceAll(in, "$USER", std::string{username ? username : ""}); replaceInString(in, "$USER", std::string{username ? username : ""});
replaceAll(in, "<br/>", std::string{"\n"}); replaceInString(in, "<br/>", std::string{"\n"});
if (in.contains("$TIME12")) { if (in.contains("$TIME12")) {
replaceAll(in, "$TIME12", getTime12h()); replaceInString(in, "$TIME12", getTime12h());
result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000; result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;
} }
if (in.contains("$TIME")) { if (in.contains("$TIME")) {
replaceAll(in, "$TIME", getTime()); replaceInString(in, "$TIME", getTime());
result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000; result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000;
} }
if (in.contains("$FAIL")) { if (in.contains("$FAIL")) {
const auto FAIL = g_pAuth->getLastFailText(); const auto FAIL = g_pAuth->getLastFailText();
replaceAll(in, "$FAIL", FAIL.has_value() ? FAIL.value() : ""); replaceInString(in, "$FAIL", FAIL.has_value() ? FAIL.value() : "");
result.allowForceUpdate = true; result.allowForceUpdate = true;
} }
if (in.contains("$PROMPT")) { if (in.contains("$PROMPT")) {
const auto PROMPT = g_pAuth->getLastPrompt(); const auto PROMPT = g_pAuth->getLastPrompt();
replaceAll(in, "$PROMPT", PROMPT.has_value() ? PROMPT.value() : ""); replaceInString(in, "$PROMPT", PROMPT.has_value() ? PROMPT.value() : "");
result.allowForceUpdate = true; result.allowForceUpdate = true;
} }

View file

@ -2,17 +2,10 @@
#include "../Renderer.hpp" #include "../Renderer.hpp"
#include "../../core/hyprlock.hpp" #include "../../core/hyprlock.hpp"
#include "../../core/Auth.hpp" #include "../../core/Auth.hpp"
#include <hyprutils/string/String.hpp>
#include <algorithm> #include <algorithm>
static void replaceAll(std::string& str, const std::string& from, const std::string& to) { using namespace Hyprutils::String;
if (from.empty())
return;
size_t pos = 0;
while ((pos = str.find(from, pos)) != std::string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
}
CPasswordInputField::CPasswordInputField(const Vector2D& viewport_, const std::unordered_map<std::string, std::any>& props, const std::string& output) : CPasswordInputField::CPasswordInputField(const Vector2D& viewport_, const std::unordered_map<std::string, std::any>& props, const std::string& output) :
outputStringPort(output), shadow(this, props, viewport_) { outputStringPort(output), shadow(this, props, viewport_) {
@ -325,11 +318,11 @@ void CPasswordInputField::updatePlaceholder() {
if (displayFail) { if (displayFail) {
g_pHyprlock->addTimer(std::chrono::milliseconds(configFailTimeoutMs), failTimeoutCallback, nullptr); g_pHyprlock->addTimer(std::chrono::milliseconds(configFailTimeoutMs), failTimeoutCallback, nullptr);
placeholder.currentText = configFailText; placeholder.currentText = configFailText;
replaceAll(placeholder.currentText, "$FAIL", AUTHFEEDBACK); replaceInString(placeholder.currentText, "$FAIL", AUTHFEEDBACK);
replaceAll(placeholder.currentText, "$ATTEMPTS", std::to_string(placeholder.failedAttempts)); replaceInString(placeholder.currentText, "$ATTEMPTS", std::to_string(placeholder.failedAttempts));
} else { } else {
placeholder.currentText = configPlaceholderText; placeholder.currentText = configPlaceholderText;
replaceAll(placeholder.currentText, "$PROMPT", AUTHFEEDBACK); replaceInString(placeholder.currentText, "$PROMPT", AUTHFEEDBACK);
} }
placeholder.resourceID = placeholder.resourceID =