ó
Ï–Fc           @   s¤   d  Z  d d l Z d Z d Z d Z d Z d Z d Z d	 Z d
 Z	 d Z
 d Z d Z d Z d Z d Z d Z d Z d Z e d ƒ Z d „  Z d d d „  ƒ  YZ d S(   sK  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 $
iÿÿÿÿNi    i   i   i   i	   i
   i   i   i   i   i   i   i   i   i   i   i   i    c         C   s$   |  | k  r | S|  | k r  | S|  S(   s@   This returns a number, n constrained to the min and max bounds. (    (   t   nt   mint   max(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt	   constrain   s
    t   screenc           B   s¯  e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z e d „ Z e d „ Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d d „ Z d d „ Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d  „  Z  d! „  Z! d" „  Z" d# „  Z# d$ „  Z$ d% „  Z% d& „  Z& d' „  Z' d( „  Z( d) „  Z) d* „  Z* d+ „  Z+ d, „  Z, d- „  Z- d. „  Z. RS(/   s<  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). i   iP   c         C   s~   | |  _  | |  _ d |  _ d |  _ d |  _ d |  _ d |  _ |  j  |  _ g  t |  j  ƒ D] } t	 g |  j ^ q[ |  _
 d S(   s7   This initializes a blank scree of the given dimentions.i   N(   t   rowst   colst   cur_rt   cur_ct   cur_saved_rt   cur_saved_ct   scroll_row_startt   scroll_row_endt   ranget   SPACEt   w(   t   selft   rt   c(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   __init__0   s    							c         C   s,   d j  g  |  j D] } d j  | ƒ ^ q ƒ S(   sw   This returns a printable representation of the screen. The end of
        each screen line is terminated by a newline. s   
t    (   t   joinR   (   R   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   __str__>   s    c         C   s,   d j  g  |  j D] } d j  | ƒ ^ q ƒ S(   sˆ   This returns a copy of the screen as a string. This is similar to
        __str__ except that lines are not terminated with line feeds. R   (   R   R   (   R   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   dumpE   s    c         C   sX   d d |  j  d } | d j g  t |  ƒ j d ƒ D] } d | d ^ q4 ƒ d | S(   s¥   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. t   +t   -s   +
s   
t   |(   R   R   t   strt   split(   R   t   top_bott   line(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   prettyL   s    c         C   s#   |  j  d d |  j |  j | ƒ d  S(   Ni   (   t   fill_regionR   R   (   R   t   ch(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   fillU   s    c         C   sß   t  | d |  j ƒ } t  | d |  j ƒ } t  | d |  j ƒ } t  | d |  j ƒ } | | k rp | | } } n  | | k rŒ | | } } n  xL t | | d ƒ D]7 } x. t | | d ƒ D] } |  j | | | ƒ qº Wq  Wd  S(   Ni   (   R   R   R   R   t   put_abs(   R   t   rst   cst   ret   ceR!   R   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR    Y   s    c         C   s   |  j  |  j d ƒ d S(   sK   This moves the cursor to the beginning (col 1) of the current row.
        i   N(   t   cursor_homeR   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   crg   s    c         C   s=   |  j  } |  j ƒ  | |  j  k r9 |  j ƒ  |  j ƒ  n  d S(   s3   This moves the cursor down with scrolling.
        N(   R   t   cursor_downt	   scroll_upt
   erase_line(   R   t   old_r(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   lfn   s
    	

c         C   s   |  j  ƒ  |  j ƒ  d S(   st   This advances the cursor with CRLF properties.
        The cursor will line wrap and the screen may scroll.
        N(   R)   R.   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   crlfy   s    
c         C   s   |  j  ƒ  d S(   s%   This is an alias for crlf().
        N(   R/   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   newline‚   s    c         C   sW   t  | d |  j ƒ } t  | d |  j ƒ } t | ƒ d } | |  j | d | d <d S(   s   Screen array starts at 1 index.i   i    N(   R   R   R   R   R   (   R   R   R   R!   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR#   ‰   s    c         C   s   |  j  |  j |  j | ƒ d S(   s?   This puts a characters at the current cursor position.
        N(   R#   R   R   (   R   R!   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   put’   s    c         C   s„   t  | d |  j ƒ } t  | d |  j ƒ } x@ t |  j | d ƒ D]) } |  j | | |  j | | d ƒ ƒ q@ W|  j | | | ƒ d S(   s¥   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.
        i   iÿÿÿÿN(   R   R   R   R   R#   t   get_abs(   R   R   R   R!   t   ci(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt
   insert_abs™   s
    'c         C   s   |  j  |  j |  j | ƒ d  S(   N(   R4   R   R   (   R   R!   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   insert¦   s    c         C   sA   t  | d |  j ƒ } t  | d |  j ƒ } |  j | d | d S(   Ni   (   R   R   R   R   (   R   R   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR2   ª   s    c         C   s   |  j  |  j |  j ƒ d  S(   N(   R2   R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   get°   s    c   
      C   s  t  | d |  j ƒ } t  | d |  j ƒ } t  | d |  j ƒ } t  | d |  j ƒ } | | k rp | | } } n  | | k rŒ | | } } n  g  } xh t | | d ƒ D]S } d } x7 t | | d ƒ D]" } |  j | | ƒ }	 | |	 } qÆ W| j | ƒ q¦ W| S(   s>   This returns a list of lines representing the region.
        i   R   (   R   R   R   R   R2   t   append(
   R   R$   R%   R&   R'   t   scR   R   R   R!   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt
   get_region´   s     c         C   s:   t  |  j d |  j ƒ |  _ t  |  j d |  j ƒ |  _ d S(   s6   This keeps the cursor within the screen area.
        i   N(   R   R   R   R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_constrainÊ   s    i   c         C   s    | |  _  | |  _ |  j ƒ  d  S(   N(   R   R   R:   (   R   R   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR(   Ò   s    		c         C   s   |  j  | |  _  |  j ƒ  d  S(   N(   R   R:   (   R   t   count(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_backØ   s    c         C   s   |  j  | |  _  |  j ƒ  d  S(   N(   R   R:   (   R   R;   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR*   Ý   s    c         C   s   |  j  | |  _  |  j ƒ  d  S(   N(   R   R:   (   R   R;   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_forwardâ   s    c         C   s   |  j  | |  _  |  j ƒ  d  S(   N(   R   R:   (   R   R;   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt	   cursor_upç   s    c         C   s3   |  j  } |  j ƒ  | |  j  k r/ |  j ƒ  n  d  S(   N(   R   R>   R+   (   R   R-   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_up_reverseì   s    	
c         C   s   |  j  | | ƒ d S(   s   Identical to Cursor Home.N(   R(   (   R   R   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_force_positionó   s    c         C   s   |  j  ƒ  d S(   s   Save current cursor position.N(   t   cursor_save_attrs(   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_saveù   s    c         C   s   |  j  ƒ  d S(   s-   Restores cursor position after a Save Cursor.N(   t   cursor_restore_attrs(   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   cursor_unsaveÿ   s    c         C   s   |  j  |  _ |  j |  _ d S(   s   Save current cursor position.N(   R   R	   R   R
   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyRA     s    c         C   s   |  j  |  j |  j ƒ d S(   s-   Restores cursor position after a Save Cursor.N(   R(   R	   R
   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyRC     s    c         C   s@   |  j  d k r d |  _  n  |  j |  j k r< |  j |  _ n  d S(   s6   This keeps the scroll region within the screen region.i    i   N(   R   R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   scroll_constrain  s    c         C   s   d |  _  |  j |  _ d S(   s$   Enable scrolling for entire display.i   N(   R   R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   scroll_screen  s    	c         C   s    | |  _  | |  _ |  j ƒ  d S(   s/   Enable scrolling from row {start} to row {end}.N(   R   R   RE   (   R   R$   R&   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   scroll_screen_rows"  s    		c         C   sI   |  j  d } |  j d } t j |  j | | !ƒ |  j | d | d +d S(   s   Scroll display down one line.i   N(   R   R   t   copyt   deepcopyR   (   R   t   st   e(    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   scroll_down*  s    c         C   sI   |  j  d } |  j d } t j |  j | d | d !ƒ |  j | | +d S(   s   Scroll display up one line.i   N(   R   R   RH   RI   R   (   R   RJ   RK   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR+   3  s    c         C   s&   |  j  |  j |  j |  j |  j ƒ d S(   sO   Erases from the current cursor position to the end of the current
        line.N(   R    R   R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   erase_end_of_line<  s    c         C   s#   |  j  |  j d |  j |  j ƒ d S(   sQ   Erases from the current cursor position to the start of the current
        line.i   N(   R    R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   erase_start_of_lineC  s    c         C   s#   |  j  |  j d |  j |  j ƒ d S(   s   Erases the entire current line.i   N(   R    R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR,   J  s    c         C   s1   |  j  ƒ  |  j |  j d d |  j |  j ƒ d S(   sQ   Erases the screen from the current line down to the bottom of the
        screen.i   N(   RM   R    R   R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt
   erase_downP  s    
c         C   s.   |  j  ƒ  |  j |  j d d d |  j ƒ d S(   sL   Erases the screen from the current line up to the top of the
        screen.i   N(   RN   R    R   R   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   erase_upX  s    
c         C   s   |  j  ƒ  d S(   s,   Erases the screen with the background color.N(   R"   (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   erase_screen`  s    c         C   s   d S(   s#   Sets a tab at the current position.N(    (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   set_tabf  s    c         C   s   d S(   s#   Clears tab at the current position.N(    (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt	   clear_tabl  s    c         C   s   d S(   s   Clears all tabs.N(    (   R   (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   clear_all_tabsr  s    (/   t   __name__t
   __module__t   __doc__R   R   R   R   R   R"   R    R)   R.   R/   R0   R#   R1   R4   R5   R2   R6   R9   R:   R(   R<   R*   R=   R>   R?   R@   RB   RD   RA   RC   RE   RF   RG   RL   R+   RM   RN   R,   RO   RP   RQ   RR   RS   RT   (    (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyR   (   sX   																																								(    (   RW   RH   t   NULt   ENQt   BELt   BSt   HTt   LFt   VTt   FFt   CRt   SOt   SIt   XONt   XOFFt   CANt   SUBt   ESCt   DELt   chrR   R   R   (    (    (    s*   /usr/lib/python2.7/dist-packages/screen.pyt   <module>   s*   	
