mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-10 00:09:04 +00:00
Add timed input for inactivity and tests
This commit is contained in:
@@ -21,6 +21,7 @@ try:
|
||||
canonical_json_dumps,
|
||||
)
|
||||
from .password_prompt import prompt_for_password
|
||||
from .input_utils import timed_input
|
||||
|
||||
if logger.isEnabledFor(logging.DEBUG):
|
||||
logger.info("Modules imported successfully.")
|
||||
@@ -41,4 +42,5 @@ __all__ = [
|
||||
"exclusive_lock",
|
||||
"shared_lock",
|
||||
"prompt_for_password",
|
||||
"timed_input",
|
||||
]
|
||||
|
19
src/utils/input_utils.py
Normal file
19
src/utils/input_utils.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import sys
|
||||
import select
|
||||
import io
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def timed_input(prompt: str, timeout: Optional[float]) -> str:
|
||||
"""Read input from the user with a timeout."""
|
||||
print(prompt, end="", flush=True)
|
||||
if timeout is None or timeout <= 0:
|
||||
return sys.stdin.readline().strip()
|
||||
try:
|
||||
sys.stdin.fileno()
|
||||
except (AttributeError, io.UnsupportedOperation):
|
||||
return input().strip()
|
||||
ready, _, _ = select.select([sys.stdin], [], [], timeout)
|
||||
if ready:
|
||||
return sys.stdin.readline().strip()
|
||||
raise TimeoutError("input timed out")
|
Reference in New Issue
Block a user