
    K                         S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  Sr  SS jr " S S\5      r " S S\5      r " S S\5      rS rg)z_
Renders the command line on the console.
(Redraws parts of the input line that were changed.)
    )unicode_literals)to_cli_filter)MouseHandlers)PointScreenWritePosition)Output)Style)Token)
is_windows)range)Rendererprint_tokensNc	                   ^^^^^^^^^^^  UR                   UR                  smn	T/mU R                  m U R                  n
U R                  mU R
                  mU R                  mU R                  mU R                  mU R                  5         UU4S jmUUUUUUU 4S jnUUUU 4S jnU(       d  U R                  5         T" 5         U(       d  U(       a  UT:w  a3  U" [        SS5      5      mT" 5         U R                  5         [        5       n[        UR                  U	5      n[        [!        UR                  UR                  5      U	5      nSn[#        U5       GH  nUR$                  U   nUR$                  U   nUR&                  U   n[        TS-
  U(       a  [!        UR)                  5       5      OS5      n[        TS-
  U(       a  [!        UR)                  5       5      OS5      nSnUUS-   :  a  UU   nUU   nUR*                  =(       d    SnUR,                  UR,                  :w  d  UR.                  UR.                  :w  aE  U" [        UUS95      mUU;   a  U
" UU   5        U" U5        TR1                  TR2                  U-   S9mUU-  nUUS-   :  a  M  U(       d  GML  UU:  d  GMU  U" [        UUS-   S95      mT" 5         U R5                  5         GM     XR                  :  a  U" [        US-
  SS95      mU(       a!  U" [        USS95      mU R                  5         OU" UR6                  5      mU(       a  U R9                  5         T" 5         UR:                  (       d  U(       a  U R;                  5         TTS   4$ )al  
Render the diff between this screen and the previous screen.

This takes two `Screen` instances. The one that represents the output like
it was during the last rendering and one that represents the current
output raster. Looking at these two `Screen` instances, this function will
render the difference by calling the appropriate methods of the `Output`
object that only paint the changes to the terminal.

This is some performance-critical code which is heavily optimized.
Don't change things without profiling first.

:param current_pos: Current cursor position.
:param last_token: `Token` instance that represents the output attributes of
        the last drawn character. (Color/attributes.)
:param attrs_for_token: :class:`._TokenToAttrsCache` instance.
:param width: The width of the terminal.
:param prevous_width: The width of the terminal during the last rendering.
c                     > T " 5         STS'   g)z(Wrapper around Output.reset_attributes. Nr    )_output_reset_attributes
last_tokens   *lib/third_party/prompt_toolkit/renderer.pyreset_attributes-_output_screen_diff.<locals>.reset_attributes@   s     "
1    c                   > TR                   TR                  p!U R                  U:  a5  T" 5         T	" SU R                  U-
  -  5        SnT" U R                   5        U $ U R                  U:  a  T" X R                  -
  5        UTS-
  :  a  T	" S5        T" U R                   5        U $ U R                   U:  d	  UTS-
  :  a  T" XR                   -
  5        U $ U R                   U:  a  T" U R                   U-
  5        U $ )z:Move cursor to this `new` point. Returns the given Point. z
r      xy)
new	current_x	current_y_output_cursor_backward_output_cursor_forward_output_cursor_upcurrent_posr   widthwrites
      r   move_cursor(_output_screen_diff.<locals>.move_cursorE   s    *}}kmm9559
 &CEEI-./I"355)JUUYi%%/0	!$K"355) 
 UUY)uqy"8#I$56 
 UUY"3559#45
r   c                    > TS   nU(       a"  XR                   :X  a  T" U R                  5        gT" TU R                      5        T" U R                  5        U R                   TS'   g)z%
Write the output of this character.
r   N)tokenchar)r,   the_last_token_output_set_attributesattrs_for_tokenr   r'   s     r   output_char(_output_screen_diff.<locals>.output_char`   sP     $An

:$))"?4::#>?$)) JJJqMr   r   r   )r   r   )r   )columnsrowsr'   	write_rawset_attributesr   cursor_forward	cursor_upcursor_backwardhide_cursordisable_autowrapr   
erase_downr   minheightmaxr   data_bufferzero_width_escapeskeysr&   r,   r+   _replacer   erase_end_of_linecursor_positionenable_autowrapshow_cursor)!outputscreenr%   previous_screenr   is_doner/   sizeprevious_widthr=   r4   r(   r0   current_height	row_countcr   new_rowprevious_rowzero_width_escapes_rownew_max_line_lenprevious_max_line_lennew_charold_char
char_widthr"   r#   r$   r   r.   r   r&   r'   s!     ` ` `                  @@@@@@@@r   _output_screen_diffrX      s/   * LL$))ME6 J LLE  I $22%66#22(($44 
 6' '  ! o5)@!%1+. (
 /N C'='=>GI	A9$$Q'&2215!'!:!:1!=uqy#glln*=aP #EAI<s<3D3D3F/G]^ _ "Q&&qzH#AH"..-AJ
 }}-8>>1Q)%!q/: ..4Q78H%)22[]]Z5O2POA% "Q&&* ?/2GG%ea3CA3E&FGK$$&E \ ...!%.1*<"BC !%.A">?!&"8"89  W
1%%r   c                       \ rS rSrSrSrg)HeightIsUnknownError   z?Information unavailable. Did not yet receive the CPR response. r   N)__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   rZ   rZ      s    Fr   rZ   c                   $    \ rS rSrSrS rS rSrg)_TokenToAttrsCache   za
A cache structure that maps Pygments Tokens to :class:`.Attr`.
(This is an important speed up.)
c                     Xl         g Nget_style_for_token)selfrh   s     r   __init___TokenToAttrsCache.__init__   s    #6 r   c                 V     U R                  U5      nX U'   U$ ! [         a    S n Nf = frf   )rh   KeyError)ri   r+   results      r   __missing___TokenToAttrsCache.__missing__   s;    	--e4F U	  	F	s    ((rg   N)r\   r]   r^   r_   r`   rj   ro   ra   r   r   r   rc   rc      s    7r   rc   c                   r    \ rS rSrSrSS jrSS jr\S 5       r\S 5       r	S r
S rSS	 jrSS
 jrS rSrg)r      z
Typical usage:

::

    output = Vt100_Output.from_pty(sys.stdout)
    r = Renderer(style, output)
    r.render(cli, layout=...)
c                     [        U[        5      (       d   e[        U[        5      (       d   eXl        X l        X0l        [        U5      U l        SU l        SU l	        SU l
        SU l        U R                  SS9  g )NFT)_scroll)
isinstancer
   r	   stylerG   use_alternate_screenr   mouse_support_in_alternate_screen_mouse_support_enabled_bracketed_paste_enabledwaiting_for_cprreset)ri   rv   rG   rw   rx   s        r   rj   Renderer.__init__   su    %''''&&))))
$8!*=9$)!&+#(-%  %

4
 r   c                 p   [        SSS9U l        S U l        S U l        S U l        S U l        S U l        [        5       U l        S U l	        SU l
        [        5       (       a!  U(       a  U R                  R                  5         U R                  (       a(  U(       a!  U R                  R                  5         SU l        U R                   (       a!  U R                  R#                  5         SU l        U R$                  (       a!  U R                  R'                  5         SU l        U R                  R)                  5         g )Nr   r   F)r   _cursor_pos_last_screen
_last_size_last_token_last_style_hash_attrs_for_tokenr   mouse_handlers_last_title_min_available_heightr   rG   scroll_buffer_to_promptry   quit_alternate_screenrz   disable_mouse_supportr{   disable_bracketed_pasteflush)ri   rt   leave_alternate_screens      r   r}   Renderer.reset   s     1? ! !% $ ,o   &'" <<GKK//1 $$)?KK--/(-D% &&KK--/*/D' ((KK//1,1D) 	r   c                 h    U R                   =(       d     U R                  S:  =(       d
    [        5       $ )z
True when the height from the cursor until the bottom of the terminal
is known. (It's often nicer to draw bottom toolbars only if the height
is known, in order to avoid flickering when the CPR response arrives.)
r   )rw   r   r   ri   s    r   height_is_knownRenderer.height_is_known2  s.     (( D,F,F,J L	r   c                 (   U R                   (       a  gU R                  S:  af  U R                  R                  5       R                  nU R
                  (       a  U R
                  R                  OSnU[        U R                  U5      -
  $ [        S5      e)zE
Return the number of rows visible in the terminal above the layout.
r   zRows above layout is unknown.)	ry   r   rG   get_sizer3   r   r=   r>   rZ   )ri   
total_rowslast_screen_heights      r   rows_above_layoutRenderer.rows_above_layout<  sx    
 $$''!+--/44J=A=N=N!2!2!9!9TUD$>$>@R SSS&'FGGr   c                 R   U R                   R                  S:X  d   e[        5       (       a   U R                  R	                  5       U l        gU R                  (       a*  U R                  R                  5       R                  U l        gSU l	        U R                  R                  5         g)z
Get current cursor position.
For vt100: Do CPR request. (answer will arrive later.)
For win32: Do API call. (Answer comes immediately.)
r   TN)r   r   r   rG   get_rows_below_cursor_positionr   rw   r   r3   r|   ask_for_cprr   s    r    request_absolute_cursor_position)Renderer.request_absolute_cursor_positionJ  sz     !!Q&&& <<)-)S)S)UD&((-1[[-A-A-C-H-H* (,$'')r   c                 t    U R                   R                  5       R                  nX!-
  S-   nX0l        SU l        g)zq
To be called when we know the absolute cursor position.
(As an answer of a "Cursor Position Request" response.)
r   FN)rG   r   r3   r   r|   )ri   rowr   rows_below_cursors       r   report_absolute_cursor_row#Renderer.report_absolute_cursor_row`  s:     [[))+00
&,q0 &7"$r   c                    U R                   nU R                  (       a(  U R                  (       d  SU l        UR                  5         U R                  (       d!  U R                   R                  5         SU l        U R                  U5      nU(       a)  U R                  (       d  UR                  5         SU l        O/U(       d(  U R                  (       a  UR                  5         SU l        UR                  5       n[        5       nSUl        [        5       nU(       a  Sn	O?U R                  (       a  U R                  R                  OSn	[!        U R"                  U	5      n	U R$                  U:w  a  SU l        U R&                  R)                  5       U R*                  :w  a  SU l        SU l        U R,                  c$  [/        U R&                  R0                  5      U l        U R&                  R)                  5       U l        UR3                  XU[5        SSUR6                  U R                  (       a  UR8                  OU	UR8                  S95        UR:                  (       d  UR<                  (       a  UR?                  [@        RB                  5        [E        XGU RF                  U R                  U RH                  UU R,                  UU R$                  (       a  U R$                  R6                  OSS9	u  U l#        U l$        Xpl        X`l        Xl%        URL                  n
XRN                  :w  a?  U
c  U R                   RQ                  5         OU R                   RS                  U
5        Xl'        URU                  5         g)z
Render the current interface to the output.

:param is_done: When True, put the cursor at the end of the interface. We
        won't print any changes to this part.
TFr   N)xposyposr&   r=   extended_height)r/   rK   rL   )+rG   rw   ry   enter_alternate_screenr{   enable_bracketed_pasterx   rz   enable_mouse_supportr   r   r   rF   r   r   r=   r>   r   r   rv   invalidation_hashr   r   rc   get_attrs_for_tokenwrite_to_screenr   r2   r3   is_aborting
is_exitingreplace_all_tokensr   AbortedrX   r   r   r   terminal_titler   clear_title	set_titler   )ri   clilayoutrJ   rG   needs_mouse_supportrK   rH   r   r=   	new_titles              r   renderRenderer.rendero  s     $$T-F-F(,D%))+ ,,KK..0,0D) #005t'B'B'')*.D'$)D)D((**/D'  "&F151B1BT&&--F33V<F ??d" $D
 ::'')T-B-BB $D$(D!  ($6tzz7U7U$VD! $

 < < >sNM,,!%!:!:DII II=
 	 ??cnn%%emm4 .AD,,t// 117;DOO33A.P*$* #, &&	((( '')%%i0(r   c                 ~   U R                   nUR                  U R                  R                  5        UR	                  U R                  R
                  5        UR                  5         UR                  5         UR                  5         U R                  (       a  U(       a  UR                  5         U R                  US9  g)ae  
Hide all output and put the cursor back at the first line. This is for
instance used for running a system command (while hiding the CLI) and
later resuming the same CLI.)

:param leave_alternate_screen: When True, and when inside an alternate
    screen buffer, quit the alternate screen.
:param erase_title: When True, clear the title from the title bar.
)r   N)rG   r8   r   r   r7   r   r;   r   r   r   r   r}   )ri   r   erase_titlerG   s       r   eraseRenderer.erase  s     t//112))++,!  

*@
Ar   c                     U R                  5         U R                  nUR                  5         UR                  SS5        UR	                  5         U R                  5         g)z
Clear screen and go to 0,0
r   N)r   rG   erase_screencursor_gotor   r   )ri   rG   s     r   clearRenderer.clear  sI    
 	

 1a --/r   )r   r{   r   ry   r   r   r   r   r   r   rz   r   rx   rG   rv   rw   r|   N)FF)FT)F)TT)r\   r]   r^   r_   r`   rj   r}   propertyr   r   r   r   r   r   r   ra   r   r   r   r   r      sX    !&0d   H H*,%ZxB00r   r   c                    [        U [        5      (       d   e[        U[        5      (       d   eU R                  5         U R	                  5         [        UR                  5      nU HC  u  pEX4   nU(       a  U R                  U5        OU R                  5         U R                  U5        ME     U R                  5         U R                  5         g)zH
Print a list of (Token, text) tuples in the given style to the output.
N)
ru   r	   r
   r   rE   rc   r   r5   r'   r   )rG   tokensrv   r/   r+   textattrss          r   r   r     s     ff%%%%eU#### 
 ))B)BCO&!!%(##%T  
LLNr   )NNFNNr   )r`   
__future__r   prompt_toolkit.filtersr   $prompt_toolkit.layout.mouse_handlersr   prompt_toolkit.layout.screenr   r   r   prompt_toolkit.outputr	   prompt_toolkit.stylesr
   prompt_toolkit.tokenr   prompt_toolkit.utilsr   	six.movesr   __all__rX   	ExceptionrZ   dictrc   objectr   r   r   r   r   <module>r      sp    ( 0 > E E ( ' & +  W[WXs&lG9 G $N0v N0br   