Skip to content

Commit

Permalink
label: add $FAIL and $ATTEMPTS (#186)
Browse files Browse the repository at this point in the history
* label: add `$ATTEMPTS` variable

* labels: also add `$FAIL`

* SHOUT
  • Loading branch information
bvr-yr authored Mar 13, 2024
1 parent 9466f59 commit 988d5b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/core/hyprlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
Expand Down
35 changes: 35 additions & 0 deletions src/renderer/widgets/IWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "IWidget.hpp"
#include "../../helpers/Log.hpp"
#include "../../helpers/VarList.hpp"
#include "../../core/hyprlock.hpp"
#include <chrono>
#include <unistd.h>

Expand Down Expand Up @@ -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()) -
Expand All @@ -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));
Expand Down

0 comments on commit 988d5b3

Please sign in to comment.