-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
72 lines (58 loc) · 1.51 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys, os, curses, traceback
class Display(object):
def __init__(self):
# Initialize curses
self.screen = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
curses.curs_set(0)
# 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
self.screen.keypad(1)
# No buffering
self.screen.nodelay(1)
# Pretty border
self.border = 0
# Get the size of the screen
# @TODO make use of this
self.height, self.width = self.screen.getmaxyx()
self.refresh()
# Refresh the screen
def refresh(self):
self.screen.border(self.border)
self.screen.refresh()
# Function to add a string to the screen
def addStr(self, y, x, message):
try:
self.screen.addstr(y, x, str(message))
except:
self.close()
traceback.print_exc(file=sys.stdout)
print y
print x
print message
exit()
def putstring(self, y, x, str):
self.addStr(y, x, str);
# Function to move the cursor
def move(self, y, x):
self.screen.move(y,x)
# Function to get a keypress
def getch(self):
try:
return self.screen.getch()
except:
raise Exception("screen.getch raised !")
def erase(self):
self.screen.erase()
# Close the screen and reset to sanity
def close(self):
self.screen.erase()
self.screen.refresh()
self.screen.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()