Skip to content

Instantly share code, notes, and snippets.

@zambony
Last active March 3, 2020 23:56
Show Gist options
  • Save zambony/79938119411b6043b3f637865b79a863 to your computer and use it in GitHub Desktop.
Save zambony/79938119411b6043b3f637865b79a863 to your computer and use it in GitHub Desktop.
from collections import deque
import os
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
clear()
quitCommands = ["quit", "exit", "done", "bye", "leave", "stop"]
menuStack = deque()
def setMenu(obj):
menuStack.append(obj)
clear()
return True
mainMenu = [
{
"text": "Print 'Hello!'",
"command": lambda : print("Hello!")
},
{
"text": "Hack the NSA",
"command": lambda : print("ffffffffffffffffffffffffffffffffffffffff")
},
{
"text": "Secret menu",
"command": lambda : setMenu(sub)
},
]
sub = [
{
"text": "Nothing. Don't push me.",
"command": lambda : print("Told you.")
},
{
"text": "ping",
"command": lambda : print("pong")
}
]
menuStack.append(mainMenu)
while (True):
currentMenu = menuStack[-1]
canBack = len(menuStack) > 1
if (canBack):
print("0.) Previous menu")
for k, v in enumerate(currentMenu):
print(f"{k + 1}.)", v["text"])
print()
cin = input(">>> ").strip().lower()
if (cin in quitCommands):
exit()
choice = int(cin)
if ((canBack and choice < 0 or (not canBack and choice < 1)) or choice > len(currentMenu)):
print("Invalid choice!\n")
print()
continue
if (canBack and choice == 0):
menuStack.pop()
clear()
continue
print()
noSpacer = currentMenu[choice - 1]["command"]()
if (not noSpacer):
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment