
    Mhr                       d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZmZ ddl m!Z! ddl"m#Z#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- erddl.m/Z/ ddl0m1Z1 ddgZ2dAd/Z3 G d0 d1e4          Z5 G d2 d&ee6e*f                   Z7 G d3 d(ee6e8f                   Z9 G d4 d5e          Z: G d6 d          Z;	 	 dBdCd@Z<d7S )Dz_
Renders the command line on the console.
(Redraws parts of the input line that were changed.)
    )annotations)FIRST_COMPLETEDFutureensure_futuresleepwait)deque)Enum)TYPE_CHECKINGAnyCallableDictHashable)get_app)CursorShape)PointSize)FilterOrBool	to_filter)AnyFormattedTextto_formatted_text)MouseHandlers)CharScreenWritePosition)
ColorDepthOutput)Attrs	BaseStyleDummyStyleTransformationStyleTransformation)Application)LayoutRendererprint_formatted_textappApplication[Any]outputr   screenr   current_posr   color_depthr   previous_screenScreen | None
last_style
str | Noneis_doneboolfull_screenattrs_for_style_string_StyleStringToAttrsCachestyle_string_has_style_StyleStringHasStyleCachesizer   previous_widthintreturntuple[Point, str | None]c                  	
 !"#$% |j         |j        c$}|j        %|j        }|j        "|j        !|j        |j         |j        |	                                 d!fd#d #$%fd}d"	%fd	}d
fd}|s
 #             |r|s|
                                 |s|r|$k    rF |t          dd                     #             |                                 t                      }t          |j        |          }t          t!          |j        |j                  |          }t#          |          D ]I}|j        |         }|j        |         }|j        |         }t          $dz
   ||                    }t          $dz
   ||                    }d}||k    r||         }||         }|j        pd}|j        |j        k    s|j        |j        k    rX |t          ||                    ||v r |||                     ||           t          j        |z   j                  ||z  }||k    |rA||k     r; |t          |dz   |                     #             |                                 K||j        k    r |t          d|dz
                      |r/ |t          d|                    |                                 n( ||                    | j        j                            |s|s|                                  #             |j        r|                                 fS )a  
    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_style: The style string, used for drawing the last drawn
        character.  (Color/attributes.)
    :param attrs_for_style_string: :class:`._StyleStringToAttrsCache` instance.
    :param width: The width of the terminal.
    :param previous_width: The width of the terminal during the last rendering.
    r:   Nonec                                     ddS )z'Wrapper around Output.reset_attributes.N )_output_reset_attributesr.   s   W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/renderer.pyreset_attributesz-_output_screen_diff.<locals>.reset_attributesZ   s     	! """


    newr   c                   j         j        }}| j        |k    r4               	d| j        |z
  z             d} | j                    | S | j        |k     r || j        z
             |dz
  k    r 	d            | j                    nF| j         |k     s	|dz
  k    r || j         z
             n| j         |k    r | j         |z
             | S )z9Move cursor to this `new` point. Returns the given Point.
r      xy)
rD   	current_x	current_y_output_cursor_backward_output_cursor_forward_output_cursor_upr*   rB   widthwrites
      rA   move_cursorz(_output_screen_diff.<locals>.move_cursor`   s   *}km9	59
 E&CEI-.///I""35)))JUYi#%/000	!!E$KKK""35))))UY)uqy"8"8##I$56666UY""359#4555
rC   charr   c                    | j         k    r | j                   dS | j                  }r|         k    r |            | j                   | j         dS )z5
        Write the output of this character.
        N)stylerT   )rT   	new_attrs_output_set_attributesr3   r+   r.   rR   s     rA   output_charz(_output_screen_diff.<locals>.output_char{   s     ##E$) /tz:I ?.DZ.P!P!P&&y+>>>E$)JJJrC   rowdict[int, Char]r9   c                f    fd|                                  D             }t          |d          S )a6  
        Return max used column index, ignoring whitespace (without style) at
        the end of the line. This is important for people that copy/paste
        terminal output.

        There are two reasons we are sometimes seeing whitespace at the end:
        - `BufferControl` adds a trailing space to each line, because it's a
          possible cursor position, so that the line wrapping won't change if
          the cursor position moves around.
        - The `Window` adds a style class to the current line for highlighting
          (cursor-line).
        c              3  R   K   | ]!\  }}|j         d k    s|j                 |V  "dS ) N)rT   rV   ).0indexcellr5   s      rA   	<genexpr>zD_output_screen_diff.<locals>.get_max_column_index.<locals>.<genexpr>   sM       
 
tyC#9$*#E 
 
rC   r   )default)itemsmax)rZ   numbersr5   s     rA   get_max_column_indexz1_output_screen_diff.<locals>.get_max_column_index   sH    
 
 
 
"yy{{
 
 

 7A&&&&rC   r   rI   rG   r:   r=   )rD   r   r:   r   )rT   r   r:   r=   )rZ   r[   r:   r9   )columnsrowsrR   	write_rawset_attributesrB   cursor_forward	cursor_upcursor_backwardhide_cursordisable_autowrapr   
erase_downr   minheightre   rangedata_bufferzero_width_escapesrQ   rT   rV   rJ   rK   erase_end_of_lineget_cursor_positionlayoutcurrent_windowenable_autowrapshow_cursor)&r&   r(   r)   r*   r+   r,   r.   r0   r2   r3   r5   r7   r8   rt   rk   rS   rY   rg   current_height	row_countrK   new_rowprevious_rowzero_width_escapes_rownew_max_line_lenprevious_max_line_lencnew_charold_char
char_widthrN   rO   rP   r@   rX   rB   rQ   rR   s&      `` `  ``                   @@@@@@@@rA   _output_screen_diffr   '   s   D L$)ME6 LE I $2%6#2($4                  6$ $ $ $ $ $ $ $ $ $,' ' ' ' ' '*    "+ "!!!
 	#&#*8E*A*A!k%!q///22 ((
 //N C'=>>GGI9 "' "'$Q'&215!'!:1!=uqy*>*>w*G*GHH #EAI/C/CL/Q/Q R R ###qzH#AH!,1J
 }--8>1Q1Q)k%!q///:: ...I4Q7888H%%%#kmj&@KMRRROA% ###*  	'/2GGG%+e.>.Ba&H&H&HIIK$$&&& ...!k%!~/A"B"B"BCC  Y!k%!~">">">??!k&"<"<SZ=V"W"WXX !k !     
""rC   c                      e Zd ZdZdS )HeightIsUnknownErrorz>Information unavailable. Did not yet receive the CPR response.N)__name__
__module____qualname____doc__r?   rC   rA   r   r   
  s        DDDDrC   r   c                  "    e Zd ZdZddZddZdS )r4   zk
    A cache structure that maps style strings to :class:`.Attr`.
    (This is an important speed up.)
    get_attrs_for_style_strCallable[[str], Attrs]style_transformationr!   r:   r=   c                "    || _         || _        d S N)r   r   )selfr   r   s      rA   __init__z!_StyleStringToAttrsCache.__init__  s    
 (?$$8!!!rC   	style_strstrr   c                n    |                      |          }| j                            |          }|| |<   |S r   )r   r   transform_attrs)r   r   attrss      rA   __missing__z$_StyleStringToAttrsCache.__missing__  s9    ,,Y77)99%@@YrC   N)r   r   r   r!   r:   r=   )r   r   r:   r   r   r   r   r   r   r   r?   rC   rA   r4   r4     sF         
9 9 9 9     rC   c                  "    e Zd ZdZddZdd
ZdS )r6   a  
    Cache for remember which style strings don't render the default output
    style (default fg/bg, no underline and no reverse and no blink). That way
    we know that we should render these cells, even when they're empty (when
    they contain a space).

    Note: we don't consider bold/italic/hidden because they don't change the
    output if there's no text in the cell.
    style_string_to_attrsdict[str, Attrs]r:   r=   c                    || _         d S r   )r   )r   r   s     rA   r   z"_StyleStringHasStyleCache.__init__/  s    %:"""rC   r   r   r1   c                    | j         |         }t          |j        p"|j        p|j        p|j        p|j        p|j                  }|| |<   |S r   )r   r1   colorbgcolor	underlinestrikeblinkreverse)r   r   r   
is_defaults       rA   r   z%_StyleStringHasStyleCache.__missing__2  sl    *95K } | {	
 }
 

 %YrC   N)r   r   r:   r=   )r   r   r:   r1   r   r?   rC   rA   r6   r6   $  sF         ; ; ; ;     rC   c                      e Zd ZdZdZdZdZdS )CPR_Supportz&Enum: whether or not CPR is supported.	SUPPORTEDNOT_SUPPORTEDUNKNOWNN)r   r   r   r   r   r   r   r?   rC   rA   r   r   A  s#        ,,I#MGGGrC   r   c                      e Zd ZdZdZ	 	 	 d*d+dZd,d-dZed.d            Zed/d            Z	ed0d            Z
d1dZd2dZed/d            Zd3d4d!Z	 d5d6d'Zd7d8d(Zd1d)ZdS )9r$   z
    Typical usage:

    ::

        output = Vt100_Output.from_pty(sys.stdout)
        r = Renderer(style, output)
        r.render(app, layout=...)
       FNrV   r   r(   r   r2   r1   mouse_supportr   cpr_not_supported_callbackCallable[[], None] | Noner:   r=   c                   || _         || _        || _        t          |          | _        || _        d| _        d| _        d| _        d| _	        t                      | _        t          j        | _        |j        st          j        | _        d | _        d | _        d | _        d | _        d | _        |                     d           d S )NFT)_scroll)rV   r(   r2   r   r   r   _in_alternate_screen_mouse_support_enabled_bracketed_paste_enabled_cursor_key_mode_resetr	   _waiting_for_cpr_futuresr   r   cpr_supportresponds_to_cprr   _attrs_for_style_style_string_has_style_last_style_hash_last_transformation_hash_last_color_depthreset)r   rV   r(   r2   r   r   s         rA   r   zRenderer.__init__V  s     
&&}55*D' %*!&+#(-%&+# >CWW%&.% 	9*8D BFIM$15:>&48

4
     rC   Tr   leave_alternate_screenc                X   t          dd          | _        d | _        d | _        d | _        d | _        t                      | _        d| _        |r| j	        
                                 | j        r"|r | j	                                         d| _        | j        r | j	                                         d| _        | j        r | j	                                         d| _        | j	                                         | j	                                         | j	                                         d S )Nr   rI   F)r   _cursor_pos_last_screen
_last_size_last_style_last_cursor_shaper   mouse_handlers_min_available_heightr(   scroll_buffer_to_promptr   quit_alternate_screenr   disable_mouse_supportr   disable_bracketed_pastereset_cursor_shaper}   flush)r   r   r   s      rA   r   zRenderer.reset~  s2    1??? ,0'+'+6: ,oo &'"
  	2K//111 $ 	.)? 	.K--///(-D% & 	0K--///*/D' ( 	2K//111,1D)&&(((!!!
 	rC   r-   c                    | j         S )zn
        The `Screen` class that was generated during the last rendering.
        This can be `None`.
        )r   r   s    rA   last_rendered_screenzRenderer.last_rendered_screen  s       rC   c                    | j         s| j        dk    rdS 	 | j                                        | _        dS # t          $ r Y dS w xY w)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   TF)r2   r   r(   get_rows_below_cursor_positionNotImplementedErrorr   s    rA   height_is_knownzRenderer.height_is_known  sc      	t9A==4	)-)S)S)U)UD&4" 	 	 	55	s   6 
AAr9   c                    | j         rdS | j        dk    rK| j                                        j        }| j        r| j        j        nd}|t          | j        |          z
  S t          d          )zU
        Return the number of rows visible in the terminal above the layout.
        r   zRows above layout is unknown.)	r   r   r(   get_sizerj   r   rt   re   r   )r   
total_rowslast_screen_heights      rA   rows_above_layoutzRenderer.rows_above_layout  s{    
 $ 	H1'!++--//4J=A=N!U!2!9!9TUD$>@R S SSS&'FGGGrC   c                     j         j        dk    sJ  j        r% j                                        j         _        dS 	  j                                         _        dS # t          $ r Y nw xY w j	        t          j        k    rdS d fd} j	        t          j        k    r |             dS  j        rdS  |             d fd}t                                           |                       dS )aS  
        Get current cursor position.

        We do this to calculate the minimum available height that we can
        consume for rendering the prompt. This is the available space below te
        cursor.

        For vt100: Do CPR request. (answer will arrive later.)
        For win32: Do API call. (Answer comes immediately.)
        r   Nr:   r=   c                      j                             t                                  j                                         d S r   )r   appendr   r(   ask_for_cprr   s   rA   do_cprz9Renderer.request_absolute_cursor_position.<locals>.do_cpr  s8    )00:::K##%%%%%rC   c                    K   t           j                   d {V   j        t          j        k    r.t          j         _         j        r                                  d S d S d S r   )r   CPR_TIMEOUTr   r   r   r   r   r   s   rA   timerz8Renderer.request_absolute_cursor_position.<locals>.timer  s}      ())))))))) ;#666#.#< 2 63355555 766 6rC   rh   )r   rK   r2   r(   r   rj   r   r   r   r   r   r   r   waiting_for_cprr   create_background_task)r   r   r   s   `  rA    request_absolute_cursor_positionz)Renderer.request_absolute_cursor_position  sA    !Q&&&&  	)-)=)=)?)?)DD&F	)-)S)S)U)UD&F" 	 	 	D	 {888F	& 	& 	& 	& 	& 	&
 {444FHHHF  	F		6 		6 		6 		6 		6 		6 			((11111s   A! !
A.-A.rZ   c                   t           j        | _        | j                                        j        }||z
  dz   }|| _        	 | j                                        }|	                    d           dS # t          $ r Y dS w xY w)z
        To be called when we know the absolute cursor position.
        (As an answer of a "Cursor Position Request" response.)
        rG   N)r   r   r   r(   r   rj   r   r   popleft
set_result
IndexError)r   rZ   r   rows_below_cursorfs        rA   report_absolute_cursor_rowz#Renderer.report_absolute_cursor_row  s    
 '0 [))++0
&,q0 &7"	-5577A LL  	 	 	DD	s    A0 0
A>=A>c                *    t          | j                  S )zi
        Waiting for CPR flag. True when we send the request, but didn't got a
        response.
        )r1   r   r   s    rA   r   zRenderer.waiting_for_cpr(  s     D1222rC   rG   timeoutc                R   K   t           j                  r j        t          j        k    rdS dfd}d fd}t           |                      t           |                      h}t          |t                     d{V \  }}|D ]}|                                 dS )z*
        Wait for a CPR response.
        Nr:   r=   c                 &   K   D ]
} |  d {V  d S r   r?   )
response_fcpr_futuress    rA   wait_for_responsesz;Renderer.wait_for_cpr_responses.<locals>.wait_for_responses:  s;      ) ! !
         ! !rC   c                    K   t                     d {V  D ]} |                                  t                      _        d S r   )r   cancelr	   r   )r   r   r   r   s    rA   wait_for_timeoutz9Renderer.wait_for_cpr_responses.<locals>.wait_for_timeout>  s]      ..        * $ $
!!####,1GGD)))rC   )return_whenrh   )	listr   r   r   r   r   r   r   r   )	r   r   r   r   tasks_pendingtaskr   s	   ``      @rA   wait_for_cpr_responseszRenderer.wait_for_cpr_responses0  s      4899  	d.+2KKK4	! 	! 	! 	! 	! 	!	4 	4 	4 	4 	4 	4 	4 	4 ,,..//**,,--
  ?CCCCCCCCC
7 	 	DKKMMMM	 	rC   r&   r'   rz   r#   r0   c                V   | j         }| j        r"| j        sd| _        |                                 | j        s | j                                          d| _        | j        s | j                                          d| _        |                                 }|r#| j	        s|
                                 d| _	        n$|s"| j	        r|                                 d| _	        |                                }t                      }d|_        t                      }| j        r|j        }	n|r+|j                            |j        |j                  j        }	nS| j        r| j        j        nd}
t/          | j        |
|j                            |j        |j                  j                  }	t3          |	|j                  }	| j        |k    rd| _        | j                                        | j        k    s2|j                                        | j        k    s|j         | j!        k    rd| _        d| _"        d| _#        | j"        $tI          | j        j%        |j                  | _"        | j#        tM          | j"                  | _#        | j                                        | _        |j                                        | _        |j         | _!        |j        '                    ||tQ          dd|j        |	          ddd           |)                                 |j*        r|+                    |j*                   tY          |||| j-        |j         | j        | j.        || j        | j"        | j#        || j        r| j        j        nd          \  | _-        | _.        || _        || _        || _/        |j0        1                    |          }| j2        | j2        |k    r|3                    |           || _2        |4                                 |j5        |j6        _5        |r| 7                                 dS dS )	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yposrQ   rt    )parent_styleerase_bgz_index)r2   r3   r5   r7   r8   )8r(   r2   r   enter_alternate_screenr   enable_bracketed_paster   reset_cursor_key_moder   r   enable_mouse_supportr   r   r   r}   r   rj   	containerpreferred_heightri   	preferredr   rt   re   r   rs   r   rV   invalidation_hashr   r   r   r+   r   r   r   r4   r   r6   write_to_screenr   draw_all_floats
exit_styleappend_style_to_contentr   r   r   r   cursorget_cursor_shaper   set_cursor_shaper   visible_windowsrz   r   )r   r&   rz   r0   r(   needs_mouse_supportr7   r)   r   rt   last_heightnew_cursor_shapes               rA   renderzRenderer.renderN  sd      	,D$= 	,(,D%))+++ , 	1K..000,0D) * 	/K--///*.D' #0022 	0t'B 	0'')))*.D''$ 	0)D 	0((****/D'   "&  	YFF 	%66di  F 7;6GN$+22QK* 11$,	JJT F VTY'' ?d"" $D J((**d.CCC'99;;-. .$"888 $D$(D!+/D( ($<
2C4L% %D! '/+D%, ,D( !%
 < < > >),)A)S)S)U)U&!$((qqVLLL 	) 	
 	
 	
 	    > 	;**3>::: .AO(#'#8#'#?7;MDO33A.
 .
 .
*$* #, :66s;;#+&*:::##$4555&6D# 	 &,%;
" 	JJLLLLL	 	rC   c                \   | j         }|                    | j        j                   |                    | j        j                   |                                 |                                 |                                 |	                                 | 
                    |           dS )aR  
        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.
        )r   N)r(   ro   r   rJ   rn   rK   rr   rB   r|   r   r   )r   r   r(   s      rA   erasezRenderer.erase  s     t/1222)+,,,!!!   

*@
AAAAArC   c                    |                                   | j        }|                                 |                    dd           |                                 |                                  dS )z,
        Clear screen and go to 0,0
        r   N)r  r(   erase_screencursor_gotor   r   )r   r(   s     rA   clearzRenderer.clear  sg    
 	

 1a   --/////rC   )FFN)rV   r   r(   r   r2   r1   r   r   r   r   r:   r=   )FT)r   r1   r   r1   r:   r=   )r:   r-   )r:   r1   )r:   r9   rh   )rZ   r9   r:   r=   )rG   )r   r9   r:   r=   )F)r&   r'   rz   r#   r0   r1   r:   r=   )T)r   r1   r:   r=   )r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r  r  r  r"  r?   rC   rA   r$   r$   I  s         K "&+@D&! &! &! &! &!P/ / / / /b ! ! ! X!    X H H H XH;2 ;2 ;2 ;2z   . 3 3 3 X3    > FKN N N N N`B B B B B*0 0 0 0 0 0rC   Nformatted_textr   rV   r   r   StyleTransformation | NoneColorDepth | Noner=   c                ~   t          |          }|pt                      }|p|                                 }|                                  |                                  d}t          |j        |          }|D ]^}}	}
||         }||k    r-|r|                     ||           n|                                  |}d|v r|                     |	           \|		                    dd          }	|		                    dd          }	| 
                    |	           |                                  |                                  dS )zT
    Print a list of (style_str, text) tuples in the given style to the output.
    Nz[ZeroWidthEscape]rH   r  
rF   )r   r    get_default_color_depthrB   r|   r4   r   rl   rk   replacerR   r   )r(   r$  rV   r   r+   	fragments
last_attrsr3   r   textr   r   s               rA   r%   r%     sn    ".11I/M3K3M3MA!?!?!A!AK 
#J 6%';   )  	4!&y1 J *%%e[9999'')))
 )++T"""" <<b))D <<f--DLL 
LLNNNNNrC   )r&   r'   r(   r   r)   r   r*   r   r+   r   r,   r-   r.   r/   r0   r1   r2   r1   r3   r4   r5   r6   r7   r   r8   r9   r:   r;   )NN)r(   r   r$  r   rV   r   r   r%  r+   r&  r:   r=   )=r   
__future__r   asyncior   r   r   r   r   collectionsr	   enumr
   typingr   r   r   r   r   "prompt_toolkit.application.currentr   prompt_toolkit.cursor_shapesr   prompt_toolkit.data_structuresr   r   prompt_toolkit.filtersr   r   prompt_toolkit.formatted_textr   r   $prompt_toolkit.layout.mouse_handlersr   prompt_toolkit.layout.screenr   r   r   prompt_toolkit.outputr   r   prompt_toolkit.stylesr   r   r    r!   prompt_toolkit.applicationr"   prompt_toolkit.layout.layoutr#   __all__r   	Exceptionr   r   r4   r1   r6   r   r$   r%   r?   rC   rA   <module>r@     s   
 # " " " " " G G G G G G G G G G G G G G             ? ? ? ? ? ? ? ? ? ? ? ? ? ? 6 6 6 6 6 6 4 4 4 4 4 4 6 6 6 6 6 6 6 6 : : : : : : : : M M M M M M M M > > > > > > D D D D D D D D D D 4 4 4 4 4 4 4 4             4666666333333 `# `# `# `#FE E E E E9 E E E    tCJ/   ,    S$Y   :    $   x0 x0 x0 x0 x0 x0 x0 x0~ 8<%)0 0 0 0 0 0 0rC   