initial build
This commit is contained in:
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
|
Reference in New Issue
Block a user