-
Notifications
You must be signed in to change notification settings - Fork 0
/
menumaker.py
69 lines (49 loc) · 1.48 KB
/
menumaker.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
from os import system, name
def cls():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
class Menu:
def __init__(self, title: str, clear: bool, items: list):
'''
Title → string\n
Clear → True for clearing screen before every menu print\n
Items → list [ ["Item", function_name], ... ]
'''
self.clear = clear
self.title = title
self.items = items
def add(self, item):
self.items.append(item)
def add(self, name, function):
item = [name,function]
self.items.append(item)
def print(self):
size = len(self.title)
star = "*" * size * 3
n_items = len(self.items)
print(star)
print("*".ljust(int(len(star) / 3)) + self.title + "".ljust(int(len(star) / 3) - 1) + "*")
print(star)
print()
for item in range(n_items):
print('[' + str(item) + '] - ' + self.items[item][0])
print("[" + str(n_items) + "] - Sair")
def get_op(self):
self.print()
op = input("> ")
op = int(op)
return op
def menu(self):
show = True
while show:
if self.clear:
cls()
op = self.get_op()
if op == len(self.items):
break
if self.clear:
cls()
self.items[op][1]()
input("ENTER for return")