Skip to content

Instantly share code, notes, and snippets.

@pingf
Last active August 29, 2015 14:07
Show Gist options
  • Save pingf/aeb6d3a1068f6f8c1c0c to your computer and use it in GitHub Desktop.
Save pingf/aeb6d3a1068f6f8c1c0c to your computer and use it in GitHub Desktop.
interactive menu
#coding=utf-8
from blessings import Terminal
import readchar
from readchar import key
import os
terminal = Terminal()
current = 0
choices = ['a','b','c']
#os.system('clear')
while True:
with terminal.location():
print('[%d]select commands~'%current)
for choice in choices:
if choice == choices[current]:
symbol = '{t.red}>{t.normal}'.format(t=terminal)
else:
symbol = ' '
print(symbol+choice)
pressed = readchar.readkey()
if pressed == key.UP:
current = max(0, current - 1)
continue
if pressed == key.DOWN:
current = min(len(choices) - 1, current + 1)
continue
if pressed == key.ENTER:
print('[%d]'%current+terminal.clear_eol())
os.system('ls')
break
if pressed == key.CTRL_C:
raise KeyboardInterr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment