label: add $FAIL and $ATTEMPTS (#186)

* label: add `$ATTEMPTS` variable

* labels: also add `$FAIL`

* SHOUT
This commit is contained in:
bvr-yr 2024-03-14 00:25:06 +03:00 committed by GitHub
parent 9466f59327
commit 988d5b3957
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 6 deletions

View file

@ -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) { static void handleForceUpdateSignal(int sig) {
if (sig == SIGUSR2) { if (sig == SIGUSR2) {
for (auto& t : g_pHyprlock->getTimers()) { forceUpdateTimers();
if (t->canForceUpdate()) {
t->call(t);
t->cancel();
}
}
} }
} }
@ -707,6 +711,7 @@ void CHyprlock::onPasswordCheckTimer() {
m_sPasswordState.passBuffer = ""; m_sPasswordState.passBuffer = "";
m_sPasswordState.failedAttempts += 1; m_sPasswordState.failedAttempts += 1;
Debug::log(LOG, "Failed attempts: {}", m_sPasswordState.failedAttempts); Debug::log(LOG, "Failed attempts: {}", m_sPasswordState.failedAttempts);
forceUpdateTimers();
for (auto& o : m_vOutputs) { for (auto& o : m_vOutputs) {
o->sessionLockSurface->render(); o->sessionLockSurface->render();

View file

@ -1,6 +1,7 @@
#include "IWidget.hpp" #include "IWidget.hpp"
#include "../../helpers/Log.hpp" #include "../../helpers/Log.hpp"
#include "../../helpers/VarList.hpp" #include "../../helpers/VarList.hpp"
#include "../../core/hyprlock.hpp"
#include <chrono> #include <chrono>
#include <unistd.h> #include <unistd.h>
@ -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() { static std::string getTime() {
const auto current_zone = std::chrono::current_zone(); 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()) - 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; 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("]")) { if (in.starts_with("cmd[") && in.contains("]")) {
// this is a command // this is a command
CVarList vars(in.substr(4, in.find_first_of(']') - 4)); CVarList vars(in.substr(4, in.find_first_of(']') - 4));