initial build
This commit is contained in:
0
ui/__init__.py
Normal file
0
ui/__init__.py
Normal file
49
ui/input_menu.py
Normal file
49
ui/input_menu.py
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
class InputMenu:
|
||||
|
||||
def __init__(self, title):
|
||||
self._title = title
|
||||
self._fields = []
|
||||
|
||||
def add_option(self, key, type, title, min, max):
|
||||
self._fields.append({"key": key, "title": title, "type": type, "value": None, "min": min, "max": max})
|
||||
return self
|
||||
|
||||
def do_input(self):
|
||||
print("")
|
||||
print(f"/--[ {self._title} ]----------------------------")
|
||||
|
||||
for i in range(len(self._fields)):
|
||||
data = None
|
||||
|
||||
while True:
|
||||
data = input(f"| ({self._fields[i]['title']}) => ")
|
||||
if self._fields[i]['type'] == "STR":
|
||||
if len(data) > self._fields[i]['min'] and len(data) <= self._fields[i]['max']:
|
||||
self._fields[i]['value'] = data
|
||||
break
|
||||
else:
|
||||
print("| Invalid input! Try again. \n|")
|
||||
|
||||
elif self._fields[i]['type'] == "INT":
|
||||
try:
|
||||
num = int(data)
|
||||
if num > self._fields[i]['min'] and num <= self._fields[i]['max']:
|
||||
self._fields[i]['value'] = data
|
||||
break
|
||||
else:
|
||||
print("| Invalid input! Try again. \n|")
|
||||
except:
|
||||
print("| Invalid input! Try again. \n|")
|
||||
else:
|
||||
exit("Invalid input type!")
|
||||
|
||||
print(f"\-------------------------------")
|
||||
print("")
|
||||
return self
|
||||
|
||||
def get_value(self, key):
|
||||
for i in range(len(self._fields)):
|
||||
if self._fields[i]["key"] == key:
|
||||
return self._fields[i]["value"]
|
||||
return None
|
34
ui/selection_menu.py
Normal file
34
ui/selection_menu.py
Normal file
@@ -0,0 +1,34 @@
|
||||
class SelectionMenu:
|
||||
|
||||
def __init__(self, title):
|
||||
self.title = title
|
||||
self.options = []
|
||||
|
||||
def add_option(self, title, callback):
|
||||
self.options.append({"title": title, "callback": callback})
|
||||
return self
|
||||
|
||||
def display(self):
|
||||
print("")
|
||||
print(f"/--[ {self.title} ]----------------------------")
|
||||
for i in range(len(self.options)):
|
||||
print(f"| {i}). {self.options[i]['title']}")
|
||||
print(f"\-------------------------------")
|
||||
print("")
|
||||
return self
|
||||
|
||||
def input_option(self):
|
||||
try:
|
||||
data = input("Selection: ")
|
||||
index = int(data)
|
||||
if index < 0 or index > len(self.options) - 1:
|
||||
print("Please enter in a valid selection.")
|
||||
return self.input_option()
|
||||
return self.options[index]['callback']
|
||||
except:
|
||||
print("Please enter in a valid selection.")
|
||||
return self.input_option()
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user