-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.py
43 lines (33 loc) · 1.05 KB
/
input.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
40
41
42
43
import os
from typing import Callable
__all__ = ["menu", "ask", "yn_question", "clear_screen"]
def menu(
prompt: str, options: list, validator: Callable[[str], bool], error: str = None
):
if prompt:
print(prompt)
for opt in options:
print(opt)
response = input("> ")
while not validator(response):
if error:
print(error)
response = input("> ")
return response
def ask(prompt: str, validator: Callable[[str], bool], error: str = None):
if prompt:
print(prompt + " ('#' if not a character)")
response = input("> ")
while not (validator(response) or response == "#"):
if error:
print(error)
response = input("> ")
return response if response != "#" else None
def yn_question(prompt: str):
print(prompt + " (y/n)")
response = input("> ")
while not any(response.lower().startswith(s) for s in "yn"):
response = input("> ")
return response.startswith("y")
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")