
    ^h                        S r SSKJr  SSK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
/r\R                  " S\R                   " S5      -   S-   5      r\R                  " S\R                   " S5      -   S-   5      r\R                  " S\R                   " S5      -   S-   5      r\R                  " S\R                   " S5      -   S-   5      r " S S5      r " S S\\\4   5      r\" 5       r " S S
5      rg)z 
Parser for VT100 input stream.
    )annotationsN)CallableDict	Generator   )KeyPress)Keys   )ANSI_SEQUENCESVt100Parser^z[z
\d+;\d+R\Zz(<?[\d;]+[mM]|M...)\Zz[\d;]*\Zz(<?[\d;]*|M.{0,2})\Zc                      \ rS rSrSrSrg)_Flush$   z8Helper object to indicate flush operation to the parser. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       [/home/james-whalen/.local/lib/python3.13/site-packages/prompt_toolkit/input/vt100_parser.pyr   r   $   s    Br   r   c                  "    \ rS rSrSrSS jrSrg)_IsPrefixOfLongerMatchCache*   zx
Dictionary that maps input sequences to a boolean indicating whether there is
any key that start with this characters.
c                   ^ [         R                  T5      (       d  [        R                  T5      (       a  SnO([        U4S j[        R
                  " 5        5       5      nX T'   U$ )NTc              3  l   >#    U  H)  u  pUR                  T5      (       d  M  UT:w  d  M%  Uv   M+     g 7fN)
startswith).0kvprefixs      r   	<genexpr>:_IsPrefixOfLongerMatchCache.__missing__.<locals>.<genexpr>9   s4      2DA<<' ,-K 2s   44	4)_cpr_response_prefix_rematch_mouse_event_prefix_reanyr   items)selfr$   results    ` r   __missing__'_IsPrefixOfLongerMatchCache.__missing__0   sc     #((004J4P4P5
 5
 F  *002 F Vr   r   N)r$   strreturnbool)r   r   r   r   r   r.   r   r   r   r   r   r   *   s    
r   r   c                      \ rS rSrSrSS jrSSS jjrSS jrSS jrSS jr	      SS jr
SS	 jrSS
 jrSS jrSrg)r   F   a2  
Parser for VT100 input stream.
Data can be fed through the `feed` method and the given callback will be
called with KeyPress objects.

::

    def callback(key):
        pass
    i = Vt100Parser(callback)
    i.feed('data...')

:attr feed_key_callback: Function that will be called when a key is parsed.
c                0    Xl         U R                  5         g r   )feed_key_callbackreset)r,   r6   s     r   __init__Vt100Parser.__init__Y   s    !2

r   c                2    SU l         U R                  5         g )NF)_in_bracketed_paste_start_parser)r,   requests     r   r7   Vt100Parser.reset]   s    #( r   c                d    U R                  5       U l        U R                  R                  S5        g)z
Start the parser coroutine.
N)_input_parser_generator_input_parsersendr,   s    r   r<   Vt100Parser._start_parsera   s)     "99;%r   c                    [         R                  U5      (       a  [        R                  $ [        R                  U5      (       a  [        R
                  $  [        U   $ ! [         a     gf = f)z4
Return the key (or keys) that maps to this prefix.
N)_cpr_response_rer(   r	   CPRResponse_mouse_event_reVt100MouseEventr   KeyError)r,   r$   s     r   
_get_matchVt100Parser._get_matchh   s`     !!&))###""6**'''	!&)) 		s   A 
A,+A,c              #  ,  #    SnSnSn SnU(       a  SnO Sv n[        U[        5      (       a  SnOX-  nU(       a  [        U   nU R                  U5      nU(       d  U(       d  U(       a  U R	                  Xa5        SnOU(       d  U(       d  U(       d|  SnSn[        [        U5      SS5       H:  nU R                  USU 5      nU(       d  M   U R	                  XaSU 5        XS nSnM<     U(       d  U R	                  US   US   5        USS nGM
  7f)z1
Coroutine (state machine) for the input parser.
 FTNr   r
   )
isinstancer    _IS_PREFIX_OF_LONGER_MATCH_CACHErK   _call_handlerrangelen)	r,   r$   retryflushcis_prefix_of_longer_matchr(   foundis	            r   r@   #Vt100Parser._input_parser_generator{   s     E a(( EKF ,LV,T)/ !:&&u5F #<e!E E #3v;26 $r
 ; 5 ..uRajA%+BZF$(E 7 !**6!9fQi@!'Q s   C	DADc                   [        U[        5      (       a.  [        U5       H  u  p4U R                  XCS:X  a  UOS5        M      gU[        R
                  :X  a  SU l        SU l        gU R                  [        X5      5        g)z
Callback to handler.
r   rN   TN)
rP   tuple	enumeraterR   r	   BracketedPaster;   _paste_bufferr6   r   )r,   keyinsert_textrZ   r"   s        r   rR   Vt100Parser._call_handler   sl     c5!!
 "#""1!VkD ' d)))+/(%'"&&x'ABr   c                :   U R                   (       a  U =R                  U-  sl        SnX R                  ;   a  U R                  R                  U5      nU R                  SU nU R                  [	        [
        R                  U5      5        SU l         U R                  U[        U5      -   S nSU l        U R                  U5        gg[        U5       HF  u  pgU R                   (       a  U R                  XS 5          gU R                  R                  U5        MH     g)z>
Feed the input stream.

:param data: Input string (unicode).
z[201~NFrN   )r;   r`   indexr6   r   r	   r_   rT   feedr^   rA   rB   )r,   dataend_mark	end_indexpaste_content	remainingrZ   rW   s           r   rf   Vt100Parser.feed   s     ##$&"H--- ..44X>	 !% 2 2:I >&&x0C0C]'ST ,1( ..y3x=/H/JK	%'"		)$ .  "$++ IId2h'&&++A. (r   c                J    U R                   R                  [        5       5        g)a  
Flush the buffer of the input stream.

This will allow us to handle the escape key (or maybe meta) sooner.
The input received by the escape key is actually the same as the first
characters of e.g. Arrow-Up, so without knowing what follows the escape
sequence, we don't know whether escape has been pressed, or whether
it's something else. This flush function should be called after a
timeout, and processes everything that's still in the buffer as-is, so
without assuming any characters will follow.
N)rA   rB   r   rC   s    r   rV   Vt100Parser.flush   s     	)r   c                F    U R                  U5        U R                  5         g)z(
Wrapper around ``feed`` and ``flush``.
N)rf   rV   )r,   rg   s     r   feed_and_flushVt100Parser.feed_and_flush   s     			$

r   )r;   rA   r`   r6   N)r6   zCallable[[KeyPress], None]r1   None)F)r=   r2   r1   rr   )r1   rr   )r$   r0   r1   zNone | Keys | tuple[Keys, ...])r1   z#Generator[None, str | _Flush, None])ra   zstr | Keys | tuple[Keys, ...]rb   r0   r1   rr   )rg   r0   r1   rr   )r   r   r   r   r   r8   r7   r<   rK   r@   rR   rf   rV   rp   r   r   r   r   r   r   F   sR    $&&0,dC0C?BC	C($/L*r   )r   
__future__r   retypingr   r   r   key_binding.key_processorr   keysr	   ansi_escape_sequencesr   __all__compileescaperF   rH   r'   r)   r   r0   r2   r   rQ   r   r   r   r   <module>r|      s    # 	 , , 0  1  ::cBIIg$66FG  **S299W#558PPQ
 **S299W+=%=%KL C"))G*<$<?V$VW 	 	$sDy/ 2 $?#@  t tr   