def clear_to_end_of_window(self):
oldtop = self.WindowTop
lastline = self.WindowTop+System.Console.WindowHeight
pos = self.pos()
w, h = self.size()
length = w - pos[0] + min((lastline - pos[1] - 1), 5) * w - 1
self.write_color(length * " ")
self.pos(*pos)
self.WindowTop = oldtop
python类Console()的实例源码
def scroll_window(self, lines):
'''Scroll the window by the indicated number of lines.'''
top = self.WindowTop + lines
if top < 0:
top = 0
if top + System.Console.WindowHeight > System.Console.BufferHeight:
top = System.Console.BufferHeight
self.WindowTop = top
def getkeypress(self):
'''Return next key press event from the queue, ignoring others.'''
ck = System.ConsoleKey
while 1:
e = System.Console.ReadKey(True)
if e.Key == System.ConsoleKey.PageDown: #PageDown
self.scroll_window(12)
elif e.Key == System.ConsoleKey.PageUp:#PageUp
self.scroll_window(-12)
elif str(e.KeyChar) == "\000":#Drop deadkeys
log("Deadkey: %s"%e)
return event(self, e)
else:
return event(self, e)
def size(self, width=None, height=None):
'''Set/get window size.'''
sc = System.Console
if width is not None and height is not None:
sc.BufferWidth, sc.BufferHeight = width,height
else:
return sc.BufferWidth, sc.BufferHeight
if width is not None and height is not None:
sc.WindowWidth, sc.WindowHeight = width,height
else:
return sc.WindowWidth - 1, sc.WindowHeight - 1
def cursor(self, visible=True, size=None):
'''Set cursor on or off.'''
System.Console.CursorVisible = visible
def bell(self):
System.Console.Beep()
def __init__(self, newbuffer=0):
'''Initialize the Console object.
newbuffer=1 will allocate a new buffer so the old content will be restored
on exit.
'''
self.serial = 0
self.attr = System.Console.ForegroundColor
self.saveattr = winattr[str(System.Console.ForegroundColor).lower()]
self.savebg = System.Console.BackgroundColor
log('initial attr=%s' % self.attr)
def _get(self):
top = System.Console.WindowTop
log("WindowTop:%s" % top)
return top
def _set(self, value):
top = System.Console.WindowTop
log("Set WindowTop:old:%s,new:%s" % (top, value))
def pos(self, x=None, y=None):
'''Move or query the window cursor.'''
if x is not None:
System.Console.CursorLeft = x
else:
x = System.Console.CursorLeft
if y is not None:
System.Console.CursorTop = y
else:
y = System.Console.CursorTop
return x, y
def home(self):
'''Move to home.'''
self.pos(0, 0)
# Map ANSI color escape sequences into Windows Console Attributes
def page(self, attr=None, fill=' '):
'''Fill the entire screen.'''
System.Console.Clear()
def clear_to_end_of_window(self):
oldtop = self.WindowTop
lastline = self.WindowTop + System.Console.WindowHeight
pos = self.pos()
w, h = self.size()
length = w - pos[0] + min((lastline - pos[1] - 1), 5) * w - 1
self.write_color(length * " ")
self.pos(*pos)
self.WindowTop = oldtop
def scroll_window(self, lines):
'''Scroll the window by the indicated number of lines.'''
top = self.WindowTop + lines
if top < 0:
top = 0
if top + System.Console.WindowHeight > System.Console.BufferHeight:
top = System.Console.BufferHeight
self.WindowTop = top
def getkeypress(self):
'''Return next key press event from the queue, ignoring others.'''
ck = System.ConsoleKey
while 1:
e = System.Console.ReadKey(True)
if e.Key == System.ConsoleKey.PageDown: #PageDown
self.scroll_window(12)
elif e.Key == System.ConsoleKey.PageUp: #PageUp
self.scroll_window(-12)
elif str(e.KeyChar) == "\000": #Drop deadkeys
log("Deadkey: %s" % e)
return event(self, e)
else:
return event(self, e)
def size(self, width=None, height=None):
'''Set/get window size.'''
sc = System.Console
if width is not None and height is not None:
sc.BufferWidth, sc.BufferHeight = width, height
else:
return sc.BufferWidth, sc.BufferHeight
if width is not None and height is not None:
sc.WindowWidth, sc.WindowHeight = width, height
else:
return sc.WindowWidth - 1, sc.WindowHeight - 1
def cursor(self, visible=True, size=None):
'''Set cursor on or off.'''
System.Console.CursorVisible = visible
def bell(self):
System.Console.Beep()
def __init__(self, newbuffer=0):
u'''Initialize the Console object.
newbuffer=1 will allocate a new buffer so the old content will be restored
on exit.
'''
self.serial = 0
self.attr = System.Console.ForegroundColor
self.saveattr = winattr[str(System.Console.ForegroundColor).lower()]
self.savebg = System.Console.BackgroundColor
log(u'initial attr=%s' % self.attr)
def _get(self):
top = System.Console.WindowTop
log(u"WindowTop:%s"%top)
return top