2011-03-12 04:13:06 +08:00
|
|
|
"""This implements a virtual screen. This is used to support ANSI terminal
|
|
|
|
emulation. The screen representation and state is implemented in this class.
|
|
|
|
Most of the methods are inspired by ANSI screen control codes. The ANSI class
|
|
|
|
extends this class to add parsing of ANSI escape codes.
|
|
|
|
|
|
|
|
$Id: screen.py 486 2007-07-13 01:04:16Z noah $
|
|
|
|
"""
|
|
|
|
|
|
|
|
import copy
|
|
|
|
|
|
|
|
NUL = 0 # Fill character; ignored on input.
|
|
|
|
ENQ = 5 # Transmit answerback message.
|
|
|
|
BEL = 7 # Ring the bell.
|
2016-09-07 04:57:50 +08:00
|
|
|
BS = 8 # Move cursor left.
|
|
|
|
HT = 9 # Move cursor to next tab stop.
|
2011-03-12 04:13:06 +08:00
|
|
|
LF = 10 # Line feed.
|
|
|
|
VT = 11 # Same as LF.
|
|
|
|
FF = 12 # Same as LF.
|
|
|
|
CR = 13 # Move cursor to left margin or newline.
|
|
|
|
SO = 14 # Invoke G1 character set.
|
|
|
|
SI = 15 # Invoke G0 character set.
|
|
|
|
XON = 17 # Resume transmission.
|
|
|
|
XOFF = 19 # Halt transmission.
|
|
|
|
CAN = 24 # Cancel escape sequence.
|
|
|
|
SUB = 26 # Same as CAN.
|
|
|
|
ESC = 27 # Introduce a control sequence.
|
|
|
|
DEL = 127 # Fill character; ignored on input.
|
2016-09-07 04:57:50 +08:00
|
|
|
SPACE = chr(32) # Space or blank character.
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def constrain(n, min, max):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This returns a number, n constrained to the min and max bounds. """
|
|
|
|
|
|
|
|
if n < min:
|
|
|
|
return min
|
|
|
|
if n > max:
|
|
|
|
return max
|
|
|
|
return n
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-03-12 04:13:06 +08:00
|
|
|
class screen:
|
|
|
|
|
|
|
|
"""This object maintains the state of a virtual text screen as a
|
|
|
|
rectangluar array. This maintains a virtual cursor position and handles
|
|
|
|
scrolling as characters are added. This supports most of the methods needed
|
|
|
|
by an ANSI text screen. Row and column indexes are 1-based (not zero-based,
|
|
|
|
like arrays). """
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def __init__(self, r=24, c=80):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This initializes a blank scree of the given dimentions."""
|
|
|
|
|
|
|
|
self.rows = r
|
|
|
|
self.cols = c
|
|
|
|
self.cur_r = 1
|
|
|
|
self.cur_c = 1
|
|
|
|
self.cur_saved_r = 1
|
|
|
|
self.cur_saved_c = 1
|
|
|
|
self.scroll_row_start = 1
|
|
|
|
self.scroll_row_end = self.rows
|
2016-09-07 04:57:50 +08:00
|
|
|
self.w = [[SPACE] * self.cols for c in range(self.rows)]
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def __str__(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This returns a printable representation of the screen. The end of
|
|
|
|
each screen line is terminated by a newline. """
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return '\n'.join([''.join(c) for c in self.w])
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def dump(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This returns a copy of the screen as a string. This is similar to
|
|
|
|
__str__ except that lines are not terminated with line feeds. """
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return ''.join([''.join(c) for c in self.w])
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def pretty(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This returns a copy of the screen as a string with an ASCII text box
|
|
|
|
around the screen border. This is similar to __str__ except that it
|
|
|
|
adds a box. """
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
top_bot = '+' + '-' * self.cols + '+\n'
|
|
|
|
return top_bot + \
|
|
|
|
'\n'.join(['|' + line + '|' for line in str(self).split('\n')]) + '\n' + top_bot
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def fill(self, ch=SPACE):
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.fill_region(1, 1, self.rows, self.cols, ch)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def fill_region(self, rs, cs, re, ce, ch=SPACE):
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
rs = constrain(rs, 1, self.rows)
|
|
|
|
re = constrain(re, 1, self.rows)
|
|
|
|
cs = constrain(cs, 1, self.cols)
|
|
|
|
ce = constrain(ce, 1, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
if rs > re:
|
|
|
|
rs, re = re, rs
|
|
|
|
if cs > ce:
|
|
|
|
cs, ce = ce, cs
|
2016-09-07 04:57:50 +08:00
|
|
|
for r in range(rs, re + 1):
|
|
|
|
for c in range(cs, ce + 1):
|
|
|
|
self.put_abs(r, c, ch)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cr(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This moves the cursor to the beginning (col 1) of the current row.
|
|
|
|
"""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_home(self.cur_r, 1)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def lf(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This moves the cursor down with scrolling.
|
|
|
|
"""
|
|
|
|
|
|
|
|
old_r = self.cur_r
|
|
|
|
self.cursor_down()
|
|
|
|
if old_r == self.cur_r:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.scroll_up()
|
2011-03-12 04:13:06 +08:00
|
|
|
self.erase_line()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def crlf(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This advances the cursor with CRLF properties.
|
|
|
|
The cursor will line wrap and the screen may scroll.
|
|
|
|
"""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cr()
|
|
|
|
self.lf()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def newline(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This is an alias for crlf().
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.crlf()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def put_abs(self, r, c, ch):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Screen array starts at 1 index."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
r = constrain(r, 1, self.rows)
|
|
|
|
c = constrain(c, 1, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
ch = str(ch)[0]
|
2016-09-07 04:57:50 +08:00
|
|
|
self.w[r - 1][c - 1] = ch
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def put(self, ch):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This puts a characters at the current cursor position.
|
|
|
|
"""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.put_abs(self.cur_r, self.cur_c, ch)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def insert_abs(self, r, c, ch):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This inserts a character at (r,c). Everything under
|
|
|
|
and to the right is shifted right one character.
|
|
|
|
The last character of the line is lost.
|
|
|
|
"""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
r = constrain(r, 1, self.rows)
|
|
|
|
c = constrain(c, 1, self.cols)
|
|
|
|
for ci in range(self.cols, c, -1):
|
|
|
|
self.put_abs(r, ci, self.get_abs(r, ci - 1))
|
|
|
|
self.put_abs(r, c, ch)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def insert(self, ch):
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.insert_abs(self.cur_r, self.cur_c, ch)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def get_abs(self, r, c):
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
r = constrain(r, 1, self.rows)
|
|
|
|
c = constrain(c, 1, self.cols)
|
|
|
|
return self.w[r - 1][c - 1]
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def get(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.get_abs(self.cur_r, self.cur_c)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def get_region(self, rs, cs, re, ce):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This returns a list of lines representing the region.
|
|
|
|
"""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
rs = constrain(rs, 1, self.rows)
|
|
|
|
re = constrain(re, 1, self.rows)
|
|
|
|
cs = constrain(cs, 1, self.cols)
|
|
|
|
ce = constrain(ce, 1, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
if rs > re:
|
|
|
|
rs, re = re, rs
|
|
|
|
if cs > ce:
|
|
|
|
cs, ce = ce, cs
|
|
|
|
sc = []
|
2016-09-07 04:57:50 +08:00
|
|
|
for r in range(rs, re + 1):
|
2011-03-12 04:13:06 +08:00
|
|
|
line = ''
|
2016-09-07 04:57:50 +08:00
|
|
|
for c in range(cs, ce + 1):
|
|
|
|
ch = self.get_abs(r, c)
|
2011-03-12 04:13:06 +08:00
|
|
|
line = line + ch
|
2016-09-07 04:57:50 +08:00
|
|
|
sc.append(line)
|
2011-03-12 04:13:06 +08:00
|
|
|
return sc
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_constrain(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This keeps the cursor within the screen area.
|
|
|
|
"""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cur_r = constrain(self.cur_r, 1, self.rows)
|
|
|
|
self.cur_c = constrain(self.cur_c, 1, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_home(self, r=1, c=1): # <ESC>[{ROW};{COLUMN}H
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
self.cur_r = r
|
|
|
|
self.cur_c = c
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_constrain()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_back(self, count=1): # <ESC>[{COUNT}D (not confused with down)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
self.cur_c = self.cur_c - count
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_constrain()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_down(self, count=1): # <ESC>[{COUNT}B (not confused with back)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
self.cur_r = self.cur_r + count
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_constrain()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_forward(self, count=1): # <ESC>[{COUNT}C
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
self.cur_c = self.cur_c + count
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_constrain()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_up(self, count=1): # <ESC>[{COUNT}A
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
self.cur_r = self.cur_r - count
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_constrain()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_up_reverse(self): # <ESC> M (called RI -- Reverse Index)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
|
|
|
old_r = self.cur_r
|
|
|
|
self.cursor_up()
|
|
|
|
if old_r == self.cur_r:
|
|
|
|
self.scroll_up()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_force_position(self, r, c): # <ESC>[{ROW};{COLUMN}f
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Identical to Cursor Home."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_home(r, c)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_save(self): # <ESC>[s
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Save current cursor position."""
|
|
|
|
|
|
|
|
self.cursor_save_attrs()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_unsave(self): # <ESC>[u
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Restores cursor position after a Save Cursor."""
|
|
|
|
|
|
|
|
self.cursor_restore_attrs()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_save_attrs(self): # <ESC>7
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Save current cursor position."""
|
|
|
|
|
|
|
|
self.cur_saved_r = self.cur_r
|
|
|
|
self.cur_saved_c = self.cur_c
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def cursor_restore_attrs(self): # <ESC>8
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Restores cursor position after a Save Cursor."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.cursor_home(self.cur_saved_r, self.cur_saved_c)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_constrain(self):
|
2011-03-12 04:13:06 +08:00
|
|
|
"""This keeps the scroll region within the screen region."""
|
|
|
|
|
|
|
|
if self.scroll_row_start <= 0:
|
|
|
|
self.scroll_row_start = 1
|
|
|
|
if self.scroll_row_end > self.rows:
|
|
|
|
self.scroll_row_end = self.rows
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_screen(self): # <ESC>[r
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Enable scrolling for entire display."""
|
|
|
|
|
|
|
|
self.scroll_row_start = 1
|
|
|
|
self.scroll_row_end = self.rows
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_screen_rows(self, rs, re): # <ESC>[{start};{end}r
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Enable scrolling from row {start} to row {end}."""
|
|
|
|
|
|
|
|
self.scroll_row_start = rs
|
|
|
|
self.scroll_row_end = re
|
|
|
|
self.scroll_constrain()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_down(self): # <ESC>D
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Scroll display down one line."""
|
|
|
|
|
|
|
|
# Screen is indexed from 1, but arrays are indexed from 0.
|
|
|
|
s = self.scroll_row_start - 1
|
|
|
|
e = self.scroll_row_end - 1
|
2016-09-07 04:57:50 +08:00
|
|
|
self.w[s + 1:e + 1] = copy.deepcopy(self.w[s:e])
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_up(self): # <ESC>M
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Scroll display up one line."""
|
|
|
|
|
|
|
|
# Screen is indexed from 1, but arrays are indexed from 0.
|
|
|
|
s = self.scroll_row_start - 1
|
|
|
|
e = self.scroll_row_end - 1
|
2016-09-07 04:57:50 +08:00
|
|
|
self.w[s:e] = copy.deepcopy(self.w[s + 1:e + 1])
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def erase_end_of_line(self): # <ESC>[0K -or- <ESC>[K
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Erases from the current cursor position to the end of the current
|
|
|
|
line."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.fill_region(self.cur_r, self.cur_c, self.cur_r, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def erase_start_of_line(self): # <ESC>[1K
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Erases from the current cursor position to the start of the current
|
|
|
|
line."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.fill_region(self.cur_r, 1, self.cur_r, self.cur_c)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def erase_line(self): # <ESC>[2K
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Erases the entire current line."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.fill_region(self.cur_r, 1, self.cur_r, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def erase_down(self): # <ESC>[0J -or- <ESC>[J
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Erases the screen from the current line down to the bottom of the
|
|
|
|
screen."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.erase_end_of_line()
|
|
|
|
self.fill_region(self.cur_r + 1, 1, self.rows, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def erase_up(self): # <ESC>[1J
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Erases the screen from the current line up to the top of the
|
|
|
|
screen."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.erase_start_of_line()
|
|
|
|
self.fill_region(self.cur_r - 1, 1, 1, self.cols)
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def erase_screen(self): # <ESC>[2J
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Erases the screen with the background color."""
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
self.fill()
|
2011-03-12 04:13:06 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def set_tab(self): # <ESC>H
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Sets a tab at the current position."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def clear_tab(self): # <ESC>[g
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Clears tab at the current position."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def clear_all_tabs(self): # <ESC>[3g
|
2011-03-12 04:13:06 +08:00
|
|
|
"""Clears all tabs."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Insert line Esc [ Pn L
|
|
|
|
# Delete line Esc [ Pn M
|
|
|
|
# Delete character Esc [ Pn P
|
|
|
|
# Scrolling region Esc [ Pn(top);Pn(bot) r
|