forked from dizir7772/AssistBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
63 lines (55 loc) · 2.17 KB
/
menu.py
File metadata and controls
63 lines (55 loc) · 2.17 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
54
55
56
57
58
59
60
61
62
63
from console import *
import readchar as rc
import colorama
import screen
from colorama import Fore, Back, Style
colorama.init(autoreset=True)
"""
|------------------------------------------|
| MENU NAME |
|------------------------------------------|
| |
| |
| MENU ITEM |
| MENU ITEM |
| MENU ITEM |
| MENU ITEM |
| MENU ITEM |
| |
| |
|------------------------------------------|
| STATUS |
|------------------------------------------|
"""
class Menu:
""" Клас що описує сутність - меню """
BREAK = -1
NONE = -2
def __init__(self, screen: screen.Screen, menu_items: list[str]) -> None:
self._items = menu_items
self._curr_active = 0
self._screen = screen
def draw(self):
""" Виводить на консоль стан меню """
self._screen.draw()
con_h, con_w = get_console_size()
start_row = gap(con_h, len(self._items))
for index, item in enumerate(self._items):
move_cursor(start_row+index, gap(con_w, len(item)))
if index != self._curr_active:
print(f'{Fore.YELLOW}{item}')
else:
print(f'{Fore.BLACK}{Back.YELLOW}{item}')
def start(self):
""" Взяємодіє з користувачем та повертаї індекс вибраного пункту меню """
while True:
self.draw()
key = rc.readkey()
if key == rc.key.ESC:
return Menu.BREAK
elif key == rc.key.UP:
self._curr_active = (self._curr_active - 1) % len(self._items)
elif key == rc.key.DOWN:
self._curr_active = (self._curr_active + 1) % len(self._items)
elif key == rc.key.ENTER:
return self._curr_active