-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.py
More file actions
53 lines (37 loc) · 1.14 KB
/
tui.py
File metadata and controls
53 lines (37 loc) · 1.14 KB
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
44
45
46
47
48
49
50
51
52
53
from typing import Callable
from dataclasses import dataclass
import getpass
@dataclass
class Option:
name: str
func: Callable
args: any = None
def prompt_secret(prompt: str):
return getpass.getpass(f"{prompt} > ")
def prompt_input(prompt: str):
return input(f"{prompt} > ")
def prompt_number(prompt: str, min: int, max: int):
try:
number = int(prompt_input(prompt))
if number < min or number > max:
raise ValueError
return number
except ValueError:
print("Invalid input.")
return prompt_number(prompt, max, min)
def prompt_selection(size: int):
return prompt_number("Select", 0, size - 1)
def show_menu(title, options: list[Option]):
print(f"\n{title}")
for i, option in enumerate(options):
print(f"{i}. {option.name}")
print(f"{len(options)}. Exit")
selection = prompt_selection(len(options) + 1)
print()
if selection == len(options):
return True
option = options[selection]
return option.func(option.args)
def show_menu_loop(title, options: list[Option]):
while not show_menu(title, options):
continue