Skip to content

Commit f9786a4

Browse files
committed
module to simplify user input
1 parent a223cda commit f9786a4

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/utils/misc/uinput.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Any
2+
3+
from rich.console import Console
4+
5+
6+
def uinput(console: Console, msg: str, qtype: int) -> Any:
7+
"""Takes user input and return the evaluated output.
8+
9+
Args:
10+
console -- Console instance
11+
msg -- question to ask the user
12+
qtype -- question type, whether y/N, string input or number input
13+
1 is yes or no input
14+
2 is number/list input
15+
16+
Returns:
17+
The evaluated input based on the user response, e.g.:
18+
y -> True
19+
N -> False
20+
1, 2, 3 -> list[int]
21+
"""
22+
23+
match qtype:
24+
case 1:
25+
console.print(
26+
f"{msg} [bold][[green]y[/green]/[red]N[/red]][/bold]", end=" "
27+
)
28+
if input().lower().strip() == "y":
29+
return True
30+
case 2:
31+
console.print(
32+
(
33+
f"{msg} [bold][NUMBER/LIST INPUT"
34+
", separate by comma ','][/bold]"
35+
), end=" "
36+
)
37+
return input()
38+
39+
return False

0 commit comments

Comments
 (0)