screen.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. """This implements a virtual screen. This is used to support ANSI terminal
  2. emulation. The screen representation and state is implemented in this class.
  3. Most of the methods are inspired by ANSI screen control codes. The ANSI class
  4. extends this class to add parsing of ANSI escape codes.
  5. PEXPECT LICENSE
  6. This license is approved by the OSI and FSF as GPL-compatible.
  7. http://opensource.org/licenses/isc-license.txt
  8. Copyright (c) 2012, Noah Spurrier <noah@noah.org>
  9. PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
  10. PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
  11. COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
  12. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. """
  20. import copy
  21. NUL = 0 # Fill character; ignored on input.
  22. ENQ = 5 # Transmit answerback message.
  23. BEL = 7 # Ring the bell.
  24. BS = 8 # Move cursor left.
  25. HT = 9 # Move cursor to next tab stop.
  26. LF = 10 # Line feed.
  27. VT = 11 # Same as LF.
  28. FF = 12 # Same as LF.
  29. CR = 13 # Move cursor to left margin or newline.
  30. SO = 14 # Invoke G1 character set.
  31. SI = 15 # Invoke G0 character set.
  32. XON = 17 # Resume transmission.
  33. XOFF = 19 # Halt transmission.
  34. CAN = 24 # Cancel escape sequence.
  35. SUB = 26 # Same as CAN.
  36. ESC = 27 # Introduce a control sequence.
  37. DEL = 127 # Fill character; ignored on input.
  38. SPACE = chr(32) # Space or blank character.
  39. def constrain (n, min, max):
  40. """This returns a number, n constrained to the min and max bounds. """
  41. if n < min:
  42. return min
  43. if n > max:
  44. return max
  45. return n
  46. class screen:
  47. """This object maintains the state of a virtual text screen as a
  48. rectangluar array. This maintains a virtual cursor position and handles
  49. scrolling as characters are added. This supports most of the methods needed
  50. by an ANSI text screen. Row and column indexes are 1-based (not zero-based,
  51. like arrays). """
  52. def __init__ (self, r=24,c=80):
  53. """This initializes a blank scree of the given dimentions."""
  54. self.rows = r
  55. self.cols = c
  56. self.cur_r = 1
  57. self.cur_c = 1
  58. self.cur_saved_r = 1
  59. self.cur_saved_c = 1
  60. self.scroll_row_start = 1
  61. self.scroll_row_end = self.rows
  62. self.w = [ [SPACE] * self.cols for c in range(self.rows)]
  63. def __str__ (self):
  64. """This returns a printable representation of the screen. The end of
  65. each screen line is terminated by a newline. """
  66. return '\n'.join ([ ''.join(c) for c in self.w ])
  67. def dump (self):
  68. """This returns a copy of the screen as a string. This is similar to
  69. __str__ except that lines are not terminated with line feeds. """
  70. return ''.join ([ ''.join(c) for c in self.w ])
  71. def pretty (self):
  72. """This returns a copy of the screen as a string with an ASCII text box
  73. around the screen border. This is similar to __str__ except that it
  74. adds a box. """
  75. top_bot = '+' + '-'*self.cols + '+\n'
  76. return top_bot + '\n'.join(['|'+line+'|' for line in str(self).split('\n')]) + '\n' + top_bot
  77. def fill (self, ch=SPACE):
  78. self.fill_region (1,1,self.rows,self.cols, ch)
  79. def fill_region (self, rs,cs, re,ce, ch=SPACE):
  80. rs = constrain (rs, 1, self.rows)
  81. re = constrain (re, 1, self.rows)
  82. cs = constrain (cs, 1, self.cols)
  83. ce = constrain (ce, 1, self.cols)
  84. if rs > re:
  85. rs, re = re, rs
  86. if cs > ce:
  87. cs, ce = ce, cs
  88. for r in range (rs, re+1):
  89. for c in range (cs, ce + 1):
  90. self.put_abs (r,c,ch)
  91. def cr (self):
  92. """This moves the cursor to the beginning (col 1) of the current row.
  93. """
  94. self.cursor_home (self.cur_r, 1)
  95. def lf (self):
  96. """This moves the cursor down with scrolling.
  97. """
  98. old_r = self.cur_r
  99. self.cursor_down()
  100. if old_r == self.cur_r:
  101. self.scroll_up ()
  102. self.erase_line()
  103. def crlf (self):
  104. """This advances the cursor with CRLF properties.
  105. The cursor will line wrap and the screen may scroll.
  106. """
  107. self.cr ()
  108. self.lf ()
  109. def newline (self):
  110. """This is an alias for crlf().
  111. """
  112. self.crlf()
  113. def put_abs (self, r, c, ch):
  114. """Screen array starts at 1 index."""
  115. r = constrain (r, 1, self.rows)
  116. c = constrain (c, 1, self.cols)
  117. ch = str(ch)[0]
  118. self.w[r-1][c-1] = ch
  119. def put (self, ch):
  120. """This puts a characters at the current cursor position.
  121. """
  122. self.put_abs (self.cur_r, self.cur_c, ch)
  123. def insert_abs (self, r, c, ch):
  124. """This inserts a character at (r,c). Everything under
  125. and to the right is shifted right one character.
  126. The last character of the line is lost.
  127. """
  128. r = constrain (r, 1, self.rows)
  129. c = constrain (c, 1, self.cols)
  130. for ci in range (self.cols, c, -1):
  131. self.put_abs (r,ci, self.get_abs(r,ci-1))
  132. self.put_abs (r,c,ch)
  133. def insert (self, ch):
  134. self.insert_abs (self.cur_r, self.cur_c, ch)
  135. def get_abs (self, r, c):
  136. r = constrain (r, 1, self.rows)
  137. c = constrain (c, 1, self.cols)
  138. return self.w[r-1][c-1]
  139. def get (self):
  140. self.get_abs (self.cur_r, self.cur_c)
  141. def get_region (self, rs,cs, re,ce):
  142. """This returns a list of lines representing the region.
  143. """
  144. rs = constrain (rs, 1, self.rows)
  145. re = constrain (re, 1, self.rows)
  146. cs = constrain (cs, 1, self.cols)
  147. ce = constrain (ce, 1, self.cols)
  148. if rs > re:
  149. rs, re = re, rs
  150. if cs > ce:
  151. cs, ce = ce, cs
  152. sc = []
  153. for r in range (rs, re+1):
  154. line = ''
  155. for c in range (cs, ce + 1):
  156. ch = self.get_abs (r,c)
  157. line = line + ch
  158. sc.append (line)
  159. return sc
  160. def cursor_constrain (self):
  161. """This keeps the cursor within the screen area.
  162. """
  163. self.cur_r = constrain (self.cur_r, 1, self.rows)
  164. self.cur_c = constrain (self.cur_c, 1, self.cols)
  165. def cursor_home (self, r=1, c=1): # <ESC>[{ROW};{COLUMN}H
  166. self.cur_r = r
  167. self.cur_c = c
  168. self.cursor_constrain ()
  169. def cursor_back (self,count=1): # <ESC>[{COUNT}D (not confused with down)
  170. self.cur_c = self.cur_c - count
  171. self.cursor_constrain ()
  172. def cursor_down (self,count=1): # <ESC>[{COUNT}B (not confused with back)
  173. self.cur_r = self.cur_r + count
  174. self.cursor_constrain ()
  175. def cursor_forward (self,count=1): # <ESC>[{COUNT}C
  176. self.cur_c = self.cur_c + count
  177. self.cursor_constrain ()
  178. def cursor_up (self,count=1): # <ESC>[{COUNT}A
  179. self.cur_r = self.cur_r - count
  180. self.cursor_constrain ()
  181. def cursor_up_reverse (self): # <ESC> M (called RI -- Reverse Index)
  182. old_r = self.cur_r
  183. self.cursor_up()
  184. if old_r == self.cur_r:
  185. self.scroll_up()
  186. def cursor_force_position (self, r, c): # <ESC>[{ROW};{COLUMN}f
  187. """Identical to Cursor Home."""
  188. self.cursor_home (r, c)
  189. def cursor_save (self): # <ESC>[s
  190. """Save current cursor position."""
  191. self.cursor_save_attrs()
  192. def cursor_unsave (self): # <ESC>[u
  193. """Restores cursor position after a Save Cursor."""
  194. self.cursor_restore_attrs()
  195. def cursor_save_attrs (self): # <ESC>7
  196. """Save current cursor position."""
  197. self.cur_saved_r = self.cur_r
  198. self.cur_saved_c = self.cur_c
  199. def cursor_restore_attrs (self): # <ESC>8
  200. """Restores cursor position after a Save Cursor."""
  201. self.cursor_home (self.cur_saved_r, self.cur_saved_c)
  202. def scroll_constrain (self):
  203. """This keeps the scroll region within the screen region."""
  204. if self.scroll_row_start <= 0:
  205. self.scroll_row_start = 1
  206. if self.scroll_row_end > self.rows:
  207. self.scroll_row_end = self.rows
  208. def scroll_screen (self): # <ESC>[r
  209. """Enable scrolling for entire display."""
  210. self.scroll_row_start = 1
  211. self.scroll_row_end = self.rows
  212. def scroll_screen_rows (self, rs, re): # <ESC>[{start};{end}r
  213. """Enable scrolling from row {start} to row {end}."""
  214. self.scroll_row_start = rs
  215. self.scroll_row_end = re
  216. self.scroll_constrain()
  217. def scroll_down (self): # <ESC>D
  218. """Scroll display down one line."""
  219. # Screen is indexed from 1, but arrays are indexed from 0.
  220. s = self.scroll_row_start - 1
  221. e = self.scroll_row_end - 1
  222. self.w[s+1:e+1] = copy.deepcopy(self.w[s:e])
  223. def scroll_up (self): # <ESC>M
  224. """Scroll display up one line."""
  225. # Screen is indexed from 1, but arrays are indexed from 0.
  226. s = self.scroll_row_start - 1
  227. e = self.scroll_row_end - 1
  228. self.w[s:e] = copy.deepcopy(self.w[s+1:e+1])
  229. def erase_end_of_line (self): # <ESC>[0K -or- <ESC>[K
  230. """Erases from the current cursor position to the end of the current
  231. line."""
  232. self.fill_region (self.cur_r, self.cur_c, self.cur_r, self.cols)
  233. def erase_start_of_line (self): # <ESC>[1K
  234. """Erases from the current cursor position to the start of the current
  235. line."""
  236. self.fill_region (self.cur_r, 1, self.cur_r, self.cur_c)
  237. def erase_line (self): # <ESC>[2K
  238. """Erases the entire current line."""
  239. self.fill_region (self.cur_r, 1, self.cur_r, self.cols)
  240. def erase_down (self): # <ESC>[0J -or- <ESC>[J
  241. """Erases the screen from the current line down to the bottom of the
  242. screen."""
  243. self.erase_end_of_line ()
  244. self.fill_region (self.cur_r + 1, 1, self.rows, self.cols)
  245. def erase_up (self): # <ESC>[1J
  246. """Erases the screen from the current line up to the top of the
  247. screen."""
  248. self.erase_start_of_line ()
  249. self.fill_region (self.cur_r-1, 1, 1, self.cols)
  250. def erase_screen (self): # <ESC>[2J
  251. """Erases the screen with the background color."""
  252. self.fill ()
  253. def set_tab (self): # <ESC>H
  254. """Sets a tab at the current position."""
  255. pass
  256. def clear_tab (self): # <ESC>[g
  257. """Clears tab at the current position."""
  258. pass
  259. def clear_all_tabs (self): # <ESC>[3g
  260. """Clears all tabs."""
  261. pass
  262. # Insert line Esc [ Pn L
  263. # Delete line Esc [ Pn M
  264. # Delete character Esc [ Pn P
  265. # Scrolling region Esc [ Pn(top);Pn(bot) r