3 """Utilities related to user input."""
5 import readchar # type: ignore
8 from typing import List
11 def single_keystroke_response(
12 valid_responses: List[str],
15 default_response: str = None,
16 timeout_seconds: int = None,
18 class TimeoutError(Exception):
21 def _handle_timeout(signum, frame) -> None:
24 def _single_keystroke_response_internal(
25 valid_responses: List[str], timeout_seconds=None
27 os_special_keystrokes = [3, 26] # ^C, ^Z
28 if timeout_seconds is not None:
29 signal.signal(signal.SIGALRM, _handle_timeout)
30 signal.alarm(timeout_seconds)
34 response = readchar.readchar()
35 if response in valid_responses:
37 if ord(response) in os_special_keystrokes:
41 if timeout_seconds is not None:
44 if prompt is not None:
48 response = _single_keystroke_response_internal(
49 valid_responses, timeout_seconds
52 if default_response is not None:
53 response = default_response
54 if prompt is not None:
59 def yn_response(prompt: str = None, *, timeout_seconds=None) -> str:
60 return single_keystroke_response(
61 ["y", "n", "Y", "N"], prompt=prompt, timeout_seconds=timeout_seconds
65 def keystroke_helper() -> None:
66 print("Watching for keystrokes; ^C to quit.")
68 key = readchar.readkey()
70 print(f'That was "{key}" ({ord(key)}).')
74 print(f'That was sequence "{key}" (', end="")
76 print(f" {ord(_)} ", end="")