mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-08 07:18:47 +00:00
Flush stdin and exit stats screen on Enter
This commit is contained in:
26
src/main.py
26
src/main.py
@@ -312,7 +312,33 @@ def _display_live_stats(
|
||||
stats_mgr.reset()
|
||||
return
|
||||
|
||||
# Flush any pending input so an accidental newline doesn't exit immediately
|
||||
try: # pragma: no cover - depends on platform
|
||||
import termios
|
||||
|
||||
termios.tcflush(sys.stdin, termios.TCIFLUSH)
|
||||
except Exception:
|
||||
try: # pragma: no cover - Windows fallback
|
||||
import msvcrt
|
||||
|
||||
while msvcrt.kbhit():
|
||||
msvcrt.getwch()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
while True:
|
||||
# Break out immediately if the user has already pressed Enter
|
||||
try: # pragma: no cover - non-interactive environments
|
||||
import select
|
||||
|
||||
ready, _, _ = select.select([sys.stdin], [], [], 0)
|
||||
if ready:
|
||||
line = sys.stdin.readline().strip()
|
||||
if line == "" or line.lower() == "b":
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if callable(sync_fn):
|
||||
try:
|
||||
sync_fn()
|
||||
|
@@ -88,3 +88,21 @@ def test_stats_display_resets_after_exit(monkeypatch, capsys):
|
||||
main._display_live_stats(pm)
|
||||
out = capsys.readouterr().out
|
||||
assert out.count("stats") == 2
|
||||
|
||||
|
||||
def test_stats_screen_breaks_on_enter(monkeypatch):
|
||||
calls = {"display": 0}
|
||||
|
||||
def display():
|
||||
calls["display"] += 1
|
||||
print("stats")
|
||||
|
||||
pm = _make_pm()
|
||||
pm.display_stats = display
|
||||
|
||||
monkeypatch.setattr(main, "get_notification_text", lambda *_: "")
|
||||
monkeypatch.setattr(main, "timed_input", lambda *_args, **_kwargs: "")
|
||||
|
||||
main._display_live_stats(pm, interval=0.01)
|
||||
|
||||
assert calls["display"] == 1
|
||||
|
Reference in New Issue
Block a user