Add basic selection and navigation systems
Create and connect locations Select options and render menus
This commit is contained in:
32
src/utils/input.py
Normal file
32
src/utils/input.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Utility to show a seletion menu
|
||||
"""
|
||||
import time
|
||||
class Option(object):
|
||||
def __init__(self, text: str, callback: callable) -> None:
|
||||
self.text = text
|
||||
self.callback = callback
|
||||
|
||||
class Menu(object):
|
||||
def __init__(self, title: str, options: list) -> None:
|
||||
self.title = title
|
||||
self.options = options
|
||||
|
||||
def show(self) -> int:
|
||||
print(f"\n{self.title}")
|
||||
|
||||
for i, option in enumerate(self.options):
|
||||
print(f"{i+1}. {option.text}")
|
||||
time.sleep(0.25)
|
||||
selected = int(input(f"Select an option [1-{len(self.options)}]: "))
|
||||
|
||||
if selected > len(self.options):
|
||||
print("Invalid option.")
|
||||
time.sleep(0.5)
|
||||
self.show()
|
||||
else:
|
||||
print("") #Newline for better readability
|
||||
if self.options[selected-1].callback is not None:
|
||||
self.options[selected-1].callback()
|
||||
|
||||
return selected
|
||||
Reference in New Issue
Block a user