From 988d5b3957fceb23e59716eebaf5f6bd16d566f0 Mon Sep 17 00:00:00 2001 From: bvr-yr <130279855+bvr-yr@users.noreply.github.com> Date: Thu, 14 Mar 2024 00:25:06 +0300 Subject: [PATCH] label: add `$FAIL` and `$ATTEMPTS` (#186) * label: add `$ATTEMPTS` variable * labels: also add `$FAIL` * SHOUT --- src/core/hyprlock.cpp | 17 ++++++++++------ src/renderer/widgets/IWidget.cpp | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/core/hyprlock.cpp b/src/core/hyprlock.cpp index 00e2fb1..c793489 100644 --- a/src/core/hyprlock.cpp +++ b/src/core/hyprlock.cpp @@ -307,14 +307,18 @@ static void handleUnlockSignal(int sig) { } } +static void forceUpdateTimers() { + for (auto& t : g_pHyprlock->getTimers()) { + if (t->canForceUpdate()) { + t->call(t); + t->cancel(); + } + } +} + static void handleForceUpdateSignal(int sig) { if (sig == SIGUSR2) { - for (auto& t : g_pHyprlock->getTimers()) { - if (t->canForceUpdate()) { - t->call(t); - t->cancel(); - } - } + forceUpdateTimers(); } } @@ -707,6 +711,7 @@ void CHyprlock::onPasswordCheckTimer() { m_sPasswordState.passBuffer = ""; m_sPasswordState.failedAttempts += 1; Debug::log(LOG, "Failed attempts: {}", m_sPasswordState.failedAttempts); + forceUpdateTimers(); for (auto& o : m_vOutputs) { o->sessionLockSurface->render(); diff --git a/src/renderer/widgets/IWidget.cpp b/src/renderer/widgets/IWidget.cpp index aaa5e27..9483839 100644 --- a/src/renderer/widgets/IWidget.cpp +++ b/src/renderer/widgets/IWidget.cpp @@ -1,6 +1,7 @@ #include "IWidget.hpp" #include "../../helpers/Log.hpp" #include "../../helpers/VarList.hpp" +#include "../../core/hyprlock.hpp" #include #include @@ -47,6 +48,29 @@ static void replaceAll(std::string& str, const std::string& from, const std::str } } +static void replaceAllAttempts(std::string& str) { + + const size_t ATTEMPTS = g_pHyprlock->getPasswordFailedAttempts(); + const std::string STR = std::to_string(ATTEMPTS); + size_t pos = 0; + + while ((pos = str.find("$ATTEMPTS", pos)) != std::string::npos) { + if (str.substr(pos, 10).ends_with('[') && str.substr(pos).contains(']')) { + const std::string REPL = str.substr(pos + 10, str.find_first_of(']', pos) - 10 - pos); + if (ATTEMPTS == 0) { + str.replace(pos, 11 + REPL.length(), REPL); + pos += REPL.length(); + } else { + str.replace(pos, 11 + REPL.length(), STR); + pos += STR.length(); + } + } else { + str.replace(pos, 9, STR); + pos += STR.length(); + } + } +} + static std::string getTime() { const auto current_zone = std::chrono::current_zone(); const auto HHMMSS = std::chrono::hh_mm_ss{current_zone->to_local(std::chrono::system_clock::now()) - @@ -72,6 +96,17 @@ IWidget::SFormatResult IWidget::formatString(std::string in) { result.updateEveryMs = result.updateEveryMs != 0 && result.updateEveryMs < 1000 ? result.updateEveryMs : 1000; } + if (in.contains("$FAIL")) { + const auto FAIL = g_pHyprlock->passwordLastFailReason(); + replaceAll(in, "$FAIL", FAIL.has_value() ? FAIL.value() : ""); + result.allowForceUpdate = true; + } + + if (in.contains("$ATTEMPTS")) { + replaceAllAttempts(in); + result.allowForceUpdate = true; + } + if (in.starts_with("cmd[") && in.contains("]")) { // this is a command CVarList vars(in.substr(4, in.find_first_of(']') - 4));