Last active
December 16, 2015 00:59
Experimenting with curses
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
import blessings | |
term = blessings.Terminal() | |
def keypress(): | |
with term.cbreak(): | |
inp = term.inkey() | |
if inp.code == term.KEY_ENTER: | |
print 'hit enter' | |
if inp.code == term.KEY_DOWN: | |
print 'downward!' | |
if inp.code == term.KEY_UP: | |
print 'upward!' | |
if inp.code == term.KEY_RIGHT: | |
print 'to the right!' | |
if inp.code == term.KEY_LEFT: | |
print 'to the left' | |
if __name__ == '__main__': | |
while True: | |
keypress() |
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
import time | |
import random | |
import string | |
from blessings import Terminal | |
term = Terminal() | |
print dir(term) | |
quit() | |
with term.fullscreen(): | |
for i in range(10): | |
print '{t.normal}FAHSJDF'.format(t=term) | |
print term.move(0, 0) + 'hi' | |
time.sleep(3) | |
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
import time | |
import curses, traceback | |
# Example code from here: | |
# http://gnosis.cx/publish/programming/charming_python_6.html | |
def main(stdscr): | |
# Frame the interface area at fixed VT100 size | |
screen = stdscr.subwin(23, 79, 0, 0) | |
screen.box() | |
screen.hline(2, 1, curses.ACS_HLINE, 77) | |
screen.refresh() | |
time.sleep(5) | |
if __name__=='__main__': | |
try: | |
# Initialize curses | |
stdscr = curses.initscr() | |
# Turn off echoing of keys, and enter cbreak mode, | |
# where no buffering is performed on keyboard input | |
curses.noecho() | |
curses.cbreak() | |
# In keypad mode, escape sequences for special keys | |
# (like the cursor keys) will be interpreted and | |
# a special value like curses.KEY_LEFT will be returned | |
stdscr.keypad(1) | |
# Enter the main loop | |
main(stdscr) | |
# Set everything back to normal | |
stdscr.keypad(0) | |
curses.echo() | |
curses.nocbreak() | |
# Terminate curses | |
curses.endwin() | |
except: | |
# In event of error, restore terminal to sane state. | |
stdscr.keypad(0) | |
curses.echo() | |
curses.nocbreak() | |
curses.endwin() | |
# Print the exception | |
traceback.print_exc() |
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
import sys, os | |
import termios | |
import fcntl | |
def getch(): | |
fd = sys.stdin.fileno() | |
oldterm = termios.tcgetattr(fd) | |
newattr = termios.tcgetattr(fd) | |
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO | |
termios.tcsetattr(fd, termios.TCSANOW, newattr) | |
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) | |
try: | |
while 1: | |
try: | |
c = sys.stdin.read(1) | |
print type(c) | |
print 'keypress: %s' % c | |
break | |
except IOError: pass | |
finally: | |
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) | |
if __name__ == '__main__': | |
getch() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment