Skip to content

Instantly share code, notes, and snippets.

@EDDxample
Created April 1, 2022 19:12
Show Gist options
  • Save EDDxample/b7e7cbc648b14847be2b103ba48d6ee6 to your computer and use it in GitHub Desktop.
Save EDDxample/b7e7cbc648b14847be2b103ba48d6ee6 to your computer and use it in GitHub Desktop.
python script to draw a cli based menu
import keyboard # pip install keyboard
def coolmenu(options: list):
index = 0
def update_index(n):
nonlocal index
index = min(max(index + n, 0), len(options) - 1)
draw()
def draw():
screen = '\x1B[2J\x1B[1;1H'
for i, op in enumerate(options):
screen += f'\x1B[30;107m> {op}\x1B[0m\n' if index == i else f' {op}\n'
print(screen)
draw()
keyboard.add_hotkey('up', update_index, (-1,))
keyboard.add_hotkey('down', update_index, (1,))
keyboard.wait('enter')
return options[index]
option = coolmenu(['start', 'options', 'exit'])
print('selected:', option)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment