54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
|
|
||
|
class MenuState:
|
||
|
def __init__(self, title, desc):
|
||
|
self.title = title
|
||
|
self.description = desc
|
||
|
self.parent = None
|
||
|
self.subs = []
|
||
|
|
||
|
def add_submenu(self, menu):
|
||
|
self.subs.append(menu)
|
||
|
menu.parent = self
|
||
|
|
||
|
def display_CLI(self):
|
||
|
display_text = f'{self.title}\n'
|
||
|
if self.parent != None:
|
||
|
display_text += f' b. Back'
|
||
|
for i in range(0, len(self.subs)):
|
||
|
sub = self.subs[i]
|
||
|
display_text += f' {i}. {sub.title}\n'
|
||
|
display_text += f' - {sub.description}\n'
|
||
|
return display_text
|
||
|
|
||
|
def nav_to_sub(self, sub_index):
|
||
|
if sub_index <= len(self.subs):
|
||
|
return self.subs[sub_index]
|
||
|
else:
|
||
|
return self
|
||
|
|
||
|
def nav_to_parent(self):
|
||
|
if self.parent != None:
|
||
|
return self.parent
|
||
|
else:
|
||
|
return self
|
||
|
|
||
|
|
||
|
class LeafMenu(MenuState):
|
||
|
def __init__(self, title, desc):
|
||
|
super.__init__(title, desc)
|
||
|
self.subs = None
|
||
|
self.actions = []
|
||
|
|
||
|
def add_submenu(self, menu):
|
||
|
return
|
||
|
|
||
|
def add_action(self, action):
|
||
|
self.actions.append(action)
|
||
|
|
||
|
def execute_action(self, act_index):
|
||
|
if act_index <= len(self.actions):
|
||
|
# what
|
||
|
self.actions[act_index]()
|
||
|
else:
|
||
|
return
|