-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
39 lines (33 loc) · 1.13 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# handlers.py
import msvcrt
import sys
import time
class InputHandler:
@staticmethod
def user_input(message, choices=None):
while True:
StoryOutputHandler.story_output(message, 15, False)
user_input = input()
if choices is None or user_input.lower() in [choice.lower() for choice in choices]:
return user_input
else:
StoryOutputHandler.story_output(
f"Invalid input, your choices are: ({', '.join(choices)})", 15
)
@staticmethod
def user_keypress(message, key=None, delay=50, newline=True):
StoryOutputHandler.story_output(message, delay, newline)
while True:
pressed = msvcrt.getch().decode().lower()
if key is None or pressed == key.lower():
return
sys.stdout.write("\b")
return
class StoryOutputHandler:
@staticmethod
def story_output(message, delay=50, newline=True):
for letter in message:
print(letter, end="", flush=True)
time.sleep(delay / 1000)
if newline:
print()