
    e5                         d Z ddlZddlZddlZddlZ ej
                  d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ej0                  d   dk\  ZereZd Z G d d      Zy)a  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
:class:`~pexpect.ANSI.ANSI` class extends this class to add parsing of ANSI
escape codes.

PEXPECT LICENSE

    This license is approved by the OSI and FSF as GPL-compatible.
        http://opensource.org/licenses/isc-license.txt

    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

    Nzpexpect.screen and pexpect.ANSI are deprecated. We recommend using pyte to emulate a terminal screen: https://pypi.python.org/pypi/pyte   )
stacklevel         	   
                                        c                 "    | |k  r|S | |kD  r|S | S )z@This returns a number, n constrained to the min and max bounds.  )nminmaxs      0/usr/lib/python3/dist-packages/pexpect/screen.py	constrainr   <   s!     	3w
3w
H    c                   @   e Zd ZdZd0dZd Zd ZereZneZ	d Zd Z
d ZefdZef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d1dZd2dZd2dZd2dZd2d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+ Z0d, Z1d- Z2d. Z3y/)3screena5  This object maintains the state of a virtual text screen as a
    rectangular 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).

    Characters are represented internally using unicode. Methods that accept
    input characters, when passed 'bytes' (which in Python 2 is equivalent to
    'str'), convert them from the encoding specified in the 'encoding'
    parameter to the constructor. Methods that return screen contents return
    unicode strings, with the exception of __str__() under Python 2. Passing
    ``encoding=None`` limits the API to only accept unicode input, so passing
    bytes in will raise :exc:`TypeError`.
    c                 v   || _         || _        || _        || _        |! t	        j
                  |      |      | _        nd| _        d| _        d| _        d| _	        d| _
        d| _        | j                   | _        t        | j                         D cg c]  }t        g| j                  z   c}| _        yc c}w )z8This initializes a blank screen of the given dimensions.N   )rowscolsencodingencoding_errorscodecsgetincrementaldecoderdecodercur_rcur_ccur_saved_rcur_saved_cscroll_row_startscroll_row_endrangeSPACEw)selfrcr%   r&   _s         r   __init__zscreen.__init__U   s     		 .A677A/RDLDL

 !"ii05dii0@A1E7TYY&AAs   B6c                 f    | j                   | j                   j                  |      S t        d      )ztThis converts from the external coding system (as passed to
        the constructor) to the internal one (unicode). zLThis screen was constructed with encoding=None, so it does not handle bytes.)r)   decode	TypeError)r3   ss     r   _decodezscreen._decodeh   s5     <<#<<&&q)) ; < <r   c                 |    dj                  | j                  D cg c]  }dj                  |       c}      S c c}w )zThis returns a printable representation of the screen as a unicode
        string (which, under Python 3.x, is the same as 'str'). The end of each
        screen line is terminated by a newline.
 joinr2   r3   r5   s     r   _unicodezscreen._unicodeq   s-    
 zz$&&:QSXXa[:;;:   9c                 b    | j                   xs d}| j                         j                  |d      S )z{This returns a printable representation of the screen. The end of
            each screen line is terminated by a newline. asciireplace)r%   rC   encode)r3   r%   s     r   __str__zscreen.__str__}   s+     }}/H==?))(I>>r   c                 |    dj                  | j                  D cg c]  }dj                  |       c}      S c c}w )zThis returns a copy of the screen as a unicode string. This is similar to
        __str__/__unicode__ except that lines are not terminated with line
        feeds.r?   r@   rB   s     r   dumpzscreen.dump   s-    
 xx81388A;8998rD   c                     dd| j                   z  z   dz   }|dj                  t        |       j                  d      D cg c]
  }d|z   dz    c}      z   dz   |z   S c c}w )zThis returns a copy of the screen as a unicode string with an ASCII
        text box around the screen border. This is similar to
        __str__/__unicode__ except that it adds a box.+-z+
r>   |)r$   rA   unicodesplit)r3   top_botlines      r   prettyzscreen.pretty   sa    
 dii'&0@S@STY@Z$[T$Yt^$[\\_ddgnnn$[s   A
c                     t        |t              r| j                  |      }| j                  dd| j                  | j
                  |       y Nr"   )
isinstancebytesr<   fill_regionr#   r$   r3   chs     r   fillzscreen.fill   s8    b% b!B!Adii		26r   c                    t        |t              r| j                  |      }t        |d| j                        }t        |d| j                        }t        |d| j
                        }t        |d| j
                        }||kD  r||}}||kD  r||}}t        ||dz         D ])  }t        ||dz         D ]  }| j                  |||        + y rV   )rW   rX   r<   r   r#   r$   r0   put_abs)r3   rscsrecer[   r4   r5   s           r   rY   zscreen.fill_region   s    b% b!BAtyy)Atyy)Atyy)Atyy)7B7BBqD! 	&ABQ' &a"%&	&r   c                 <    | j                  | j                  d       y)zKThis moves the cursor to the beginning (col 1) of the current row.
        r"   N)cursor_homer*   r3   s    r   crz	screen.cr   s     	$**a(r   c                     | j                   }| j                          || j                   k(  r!| j                          | j                          yy)z3This moves the cursor down with scrolling.
        N)r*   cursor_down	scroll_up
erase_liner3   old_rs     r   lfz	screen.lf   s>     

DJJNNOO r   c                 D    | j                          | j                          y)ztThis advances the cursor with CRLF properties.
        The cursor will line wrap and the screen may scroll.
        N)rf   rm   re   s    r   crlfzscreen.crlf   s    
 	

r   c                 $    | j                          y)z%This is an alias for crlf().
        N)ro   re   s    r   newlinezscreen.newline   s     			r   c                     t        |d| j                        }t        |d| j                        }t        |t              r| j                  |      d   }n|d   }|| j                  |dz
     |dz
  <   y)zScreen array starts at 1 index.r"   r   N)r   r#   r$   rW   rX   r<   r2   )r3   r4   r5   r[   s       r   r^   zscreen.put_abs   sh     q!TYY'q!TYY'b% b!!$BABqsAaCr   c                     t        |t              r| j                  |      }| j                  | j                  | j
                  |       y)z?This puts a characters at the current cursor position.
        N)rW   rX   r<   r^   r*   r+   rZ   s     r   putz
screen.put   s4     b% b!Bdjj$**b1r   c           
      L   t        |t              r| j                  |      }t        |d| j                        }t        |d| j
                        }t        | j
                  |d      D ](  }| j                  ||| j                  ||dz
               * | j                  |||       y)zThis 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.
        r"   N)	rW   rX   r<   r   r#   r$   r0   r^   get_abs)r3   r4   r5   r[   cis        r   
insert_abszscreen.insert_abs   s     b% b!Bq!TYY'q!TYY'Ar* 	6BLL!BQr!t 45	6a"r   c                     t        |t              r| j                  |      }| j                  | j                  | j
                  |       y N)rW   rX   r<   ry   r*   r+   rZ   s     r   insertzscreen.insert   s2    b% b!BTZZ4r   c                     t        |d| j                        }t        |d| j                        }| j                  |dz
     |dz
     S rV   )r   r#   r$   r2   r3   r4   r5   s      r   rw   zscreen.get_abs   sA    q!TYY'q!TYY'vvac{1Q3r   c                 P    | j                  | j                  | j                         y r{   )rw   r*   r+   re   s    r   getz
screen.get   s    djj$**-r   c                    t        |d| j                        }t        |d| j                        }t        |d| j                        }t        |d| j                        }||kD  r||}}||kD  r||}}g }t        ||dz         D ]@  }d}t        ||dz         D ]  }| j	                  ||      }	||	z   } |j                  |       B |S )z>This returns a list of lines representing the region.
        r"   r?   )r   r#   r$   r0   rw   append)
r3   r_   r`   ra   rb   scr4   rS   r5   r[   s
             r   
get_regionzscreen.get_region   s     Atyy)Atyy)Atyy)Atyy)7B7BBqD! 	ADBQ' !\\1Q'by! IIt	 	r   c                     t        | j                  d| j                        | _        t        | j                  d| j                        | _        y)z6This keeps the cursor within the screen area.
        r"   N)r   r*   r#   r+   r$   re   s    r   cursor_constrainzscreen.cursor_constrain  s4     

Atyy9


Atyy9
r   c                 @    || _         || _        | j                          y r{   )r*   r+   r   r~   s      r   rd   zscreen.cursor_home  s    

 r   c                 L    | j                   |z
  | _         | j                          y r{   r+   r   r3   counts     r   cursor_backzscreen.cursor_back      ZZ%'
 r   c                 L    | j                   |z   | _         | j                          y r{   r*   r   r   s     r   rh   zscreen.cursor_down#  r   r   c                 L    | j                   |z   | _         | j                          y r{   r   r   s     r   cursor_forwardzscreen.cursor_forward(  r   r   c                 L    | j                   |z
  | _         | j                          y r{   r   r   s     r   	cursor_upzscreen.cursor_up-  r   r   c                 |    | j                   }| j                          || j                   k(  r| j                          y y r{   )r*   r   ri   rk   s     r   cursor_up_reversezscreen.cursor_up_reverse2  s0    

DJJNN r   c                 (    | j                  ||       y)zIdentical to Cursor Home.N)rd   r~   s      r   cursor_force_positionzscreen.cursor_force_position9  s     	!Qr   c                 $    | j                          yzSave current cursor position.N)cursor_save_attrsre   s    r   cursor_savezscreen.cursor_save>  s     	 r   c                 $    | j                          yz-Restores cursor position after a Save Cursor.N)cursor_restore_attrsre   s    r   cursor_unsavezscreen.cursor_unsaveC  s     	!!#r   c                 H    | j                   | _        | j                  | _        yr   )r*   r,   r+   r-   re   s    r   r   zscreen.cursor_save_attrsH  s      ::::r   c                 P    | j                  | j                  | j                         yr   )rd   r,   r-   re   s    r   r   zscreen.cursor_restore_attrsN  s      	$**D,<,<=r   c                     | j                   dk  rd| _         | j                  | j                  kD  r| j                  | _        yy)z6This keeps the scroll region within the screen region.r   r"   N)r.   r/   r#   re   s    r   scroll_constrainzscreen.scroll_constrainS  s=       A%$%D!*"&))D +r   c                 4    d| _         | j                  | _        y)z$Enable scrolling for entire display.r"   N)r.   r#   r/   re   s    r   scroll_screenzscreen.scroll_screen[  s     !""iir   c                 @    || _         || _        | j                          y)z/Enable scrolling from row {start} to row {end}.N)r.   r/   r   )r3   r_   ra   s      r   scroll_screen_rowszscreen.scroll_screen_rowsa  s      !# r   c                     | j                   dz
  }| j                  dz
  }t        j                  | j                  ||       | j                  |dz   |dz    y)zScroll display down one line.r"   Nr.   r/   copydeepcopyr2   r3   r;   es      r   scroll_downzscreen.scroll_downh  sN     !!A%!#--q4qs1Q3r   c                     | j                   dz
  }| j                  dz
  }t        j                  | j                  |dz   |dz          | j                  || y)zScroll display up one line.r"   Nr   r   s      r   ri   zscreen.scroll_upp  sN     !!A%!#mmDFF1Q3qsO4qr   c                 |    | j                  | j                  | j                  | j                  | j                         y)zOErases from the current cursor position to the end of the current
        line.N)rY   r*   r+   r$   re   s    r   erase_end_of_linezscreen.erase_end_of_linex  s(     	$**djj$**diiHr   c                 h    | j                  | j                  d| j                  | j                         y)zQErases from the current cursor position to the start of the current
        line.r"   N)rY   r*   r+   re   s    r   erase_start_of_linezscreen.erase_start_of_line~  s$     	$**aTZZ@r   c                 h    | j                  | j                  d| j                  | j                         y)zErases the entire current line.r"   N)rY   r*   r$   re   s    r   rj   zscreen.erase_line  s$     	$**aTYY?r   c                     | j                          | j                  | j                  dz   d| j                  | j                         y)zQErases the screen from the current line down to the bottom of the
        screen.r"   N)r   rY   r*   r#   r$   re   s    r   
erase_downzscreen.erase_down  s4     	!$**q.!TYY		Br   c                 z    | j                          | j                  | j                  dz
  dd| j                         y)zLErases the screen from the current line up to the top of the
        screen.r"   N)r   rY   r*   r$   re   s    r   erase_upzscreen.erase_up  s0     	  #$**Q,1dii8r   c                 $    | j                          y)z,Erases the screen with the background color.N)r\   re   s    r   erase_screenzscreen.erase_screen  s     			r   c                      y)z#Sets a tab at the current position.Nr   re   s    r   set_tabzscreen.set_tab       	r   c                      y)z#Clears tab at the current position.Nr   re   s    r   	clear_tabzscreen.clear_tab  r   r   c                      y)zClears all tabs.Nr   re   s    r   clear_all_tabszscreen.clear_all_tabs  r   r   N)r   P   zlatin-1rG   )r"   r"   )r"   )4__name__
__module____qualname____doc__r7   r<   rC   PY3rI   __unicode__rK   rT   r1   r\   rY   rf   rm   ro   rq   r^   rt   ry   r|   rw   r   r   r   rd   r   rh   r   r   r   r   r   r   r   r   r   r   r   r   ri   r   r   rj   r   r   r   r   r   r   r   r   r   r    r    F   s   B&<< 	?:o  7 -2 &")	25 .*:!!
!
!
!
 
!
$
&>
,( 55IA@
C9


r   r    )r   r'   r   syswarningswarnNULENQBELBSHTLFVTFFCRSOSIXONXOFFCANSUBESCDELr1   version_infor   strrP   r   r    r   r   r   <module>r      s   2   
   3 
 		
aGc cr   