Last active
August 29, 2015 14:07
-
-
Save pingf/aeb6d3a1068f6f8c1c0c to your computer and use it in GitHub Desktop.
interactive menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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