-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_select.py
75 lines (63 loc) · 2.4 KB
/
multi_select.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from get_key_press import SpecialKeys, get_key_press
from utils import clear_screen, press_enter_to_continue, print_special
class MultiSelect:
def __init__(self, options, header=None, selected_by_default=False):
self.__options = [
{
"label": option["label"],
"value": option["value"],
"selected": selected_by_default,
}
for option in options
]
self.__header = header
self.__current_option = 0
def get(self) -> list:
self.__current_option = 0
selected = []
try:
selected = self.__get()
except KeyboardInterrupt as e:
raise e
except Exception as e:
press_enter_to_continue(f"{str(e)}\n\nWystąpił krytyczny błąd")
return selected
def __get(self) -> list:
while True:
clear_screen()
if self.__header:
print(f"{self.__header}")
else:
print("Wybierz opcje")
print("===========================================")
for i, option in enumerate(self.__options):
label = (
f"[x] {option['label']}"
if option["selected"]
else f"[ ] {option['label']}"
)
if i == self.__current_option:
print_special(f"> {label}")
else:
print(f" {label}")
print("===========================================")
print("↑ ↓ - poruszanie się")
print("Spacja - zaznacz/odznacz")
print("Enter - zatwierdź")
key = get_key_press()
if key == SpecialKeys.UP:
self.__current_option = (self.__current_option - 1) % len(
self.__options
)
elif key == SpecialKeys.DOWN:
self.__current_option = (self.__current_option + 1) % len(
self.__options
)
elif key == SpecialKeys.ENTER:
return [
option["value"] for option in self.__options if option["selected"]
]
elif key == SpecialKeys.SPACE:
self.__options[self.__current_option]["selected"] = not self.__options[
self.__current_option
]["selected"]