X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=input_utils.py;h=a989b2d6fe3db428d25a2de5b8baf6baef3c8734;hb=29ee1f98654a689e9cab76b0c7c68428faa43a8c;hp=648ee301639ec876750ebdd2b5d98e0d01603eaf;hpb=37dea8cb5d6adc9a4251949ea78a9b14620921ff;p=python_utils.git diff --git a/input_utils.py b/input_utils.py index 648ee30..a989b2d 100644 --- a/input_utils.py +++ b/input_utils.py @@ -2,11 +2,12 @@ """Utilities related to user input.""" -import readchar # type: ignore import signal import sys from typing import List +import readchar # type: ignore + import exceptions @@ -17,6 +18,8 @@ def single_keystroke_response( default_response: str = None, timeout_seconds: int = None, ) -> str: + """Get a single keystroke response to a prompt.""" + def _handle_timeout(signum, frame) -> None: raise exceptions.TimeoutError() @@ -56,12 +59,16 @@ def single_keystroke_response( def yn_response(prompt: str = None, *, timeout_seconds=None) -> str: + """Get a Y/N response to a prompt.""" + return single_keystroke_response( ["y", "n", "Y", "N"], prompt=prompt, timeout_seconds=timeout_seconds ).lower() def keystroke_helper() -> None: + """Misc util to watch keystrokes and report what they were.""" + print("Watching for keystrokes; ^C to quit.") while True: key = readchar.readkey()