o
    �õ±jÚ   ã                   @  sð   d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 dd	lmZ d
gZe de d¡ d ¡Ze de d¡ d ¡Ze de d¡ d ¡Ze de d¡ d ¡ZG dd„ dƒZG dd„ deeef ƒZeƒ ZG dd
„ d
ƒZdS )z 
Parser for VT100 input stream.
é    )ÚannotationsN)ÚCallableÚ	Generatoré   )ÚKeyPress)ÚKeysé   )ÚANSI_SEQUENCESÚVt100Parserú^z[z
\d+;\d+R\Zz(<?[\d;]+[mM]|M...)\Zz[\d;]*\Zz(<?[\d;]*|M.{0,2})\Zc                   @  s   e Zd ZdZdS )Ú_Flushz8Helper object to indicate flush operation to the parser.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú”/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/input/vt100_parser.pyr   $   s    r   c                   @  s   e Zd ZdZd	dd„ZdS )
Ú_IsPrefixOfLongerMatchCachez„
    Dictionary that maps input sequences to a boolean indicating whether there is
    any key that start with this characters.
    ÚprefixÚstrÚreturnÚboolc                   s@   t  ˆ ¡s
t ˆ ¡rd}nt‡ fdd„t ¡ D ƒƒ}|| ˆ < |S )NTc                 3  s*   � | ]\}}|  ˆ ¡r|ˆ kr|V  qd S ©N)Ú
startswith)Ú.0ÚkÚv©r   r   r   Ú	<genexpr>9   s   € ýþýz:_IsPrefixOfLongerMatchCache.__missing__.<locals>.<genexpr>)Ú_cpr_response_prefix_reÚmatchÚ_mouse_event_prefix_reÚanyr	   Úitems)Úselfr   Úresultr   r   r   Ú__missing__0   s   ÿþz'_IsPrefixOfLongerMatchCache.__missing__N)r   r   r   r   )r   r   r   r   r&   r   r   r   r   r   *   s    r   c                   @  sl   e Zd ZdZd$dd„Zd%d&dd„Zd'dd„Zd(dd„Zd)dd„Zd*dd„Z	d+dd„Z
d'dd „Zd+d!d"„Zd#S ),r
   aZ  
    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.
    Úfeed_key_callbackúCallable[[KeyPress], None]r   ÚNonec                 C  s   || _ |  ¡  d S r   )r'   Úreset)r$   r'   r   r   r   Ú__init__Y   ó   zVt100Parser.__init__FÚrequestr   c                 C  s   d| _ |  ¡  d S )NF)Ú_in_bracketed_pasteÚ_start_parser)r$   r-   r   r   r   r*   ]   r,   zVt100Parser.resetc                 C  s   |   ¡ | _| j d¡ dS )z-
        Start the parser coroutine.
        N)Ú_input_parser_generatorÚ_input_parserÚsend©r$   r   r   r   r/   a   s   
zVt100Parser._start_parserr   r   úNone | Keys | tuple[Keys, ...]c                 C  s@   t  |¡rtjS t |¡rtjS zt| W S  ty   Y dS w )zD
        Return the key (or keys) that maps to this prefix.
        N)Ú_cpr_response_rer    r   ÚCPRResponseÚ_mouse_event_reÚVt100MouseEventr	   ÚKeyError)r$   r   r   r   r   Ú
_get_matchh   s   


ÿzVt100Parser._get_matchú#Generator[None, str | _Flush, None]c           	      c  sú   � d}d}d}	 d}|rd}ndV }t |tƒrd}n||7 }|r|t| }|  |¡}|s-|s8|r8|  ||¡ d}nD|s<|s||s|d}d}tt|ƒddƒD ]}|  |d|… ¡}|ri|  ||d|… ¡ ||d… }d}qJ|s||  |d |d ¡ |dd… }q)zA
        Coroutine (state machine) for the input parser.
        Ú FTNr   éÿÿÿÿr   )Ú
isinstancer   Ú _IS_PREFIX_OF_LONGER_MATCH_CACHEr:   Ú_call_handlerÚrangeÚlen)	r$   r   ÚretryÚflushÚcÚis_prefix_of_longer_matchr    ÚfoundÚir   r   r   r0   {   s@   €

€Øz#Vt100Parser._input_parser_generatorÚkeyústr | Keys | tuple[Keys, ...]Úinsert_textc                 C  sf   t |tƒrt|ƒD ]\}}|  ||dkr|nd¡ q	dS |tjkr)d| _d| _dS |  t	||ƒ¡ dS )z&
        Callback to handler.
        r   r<   TN)
r>   ÚtupleÚ	enumerater@   r   ÚBracketedPaster.   Ú_paste_bufferr'   r   )r$   rI   rK   rH   r   r   r   r   r@   ­   s   
ÿ

zVt100Parser._call_handlerÚdatac                 C  sÂ   | j rA|  j|7  _d}|| jv r?| j |¡}| jd|… }|  ttj|ƒ¡ d| _ | j|t|ƒ d… }d| _|  |¡ dS dS t	|ƒD ]\}}| j rX|  ||d… ¡  dS | j
 |¡ qEdS )zV
        Feed the input stream.

        :param data: Input string (unicode).
        z[201~NFr<   )r.   rO   Úindexr'   r   r   rN   rB   ÚfeedrM   r1   r2   )r$   rP   Úend_markÚ	end_indexÚpaste_contentÚ	remainingrH   rE   r   r   r   rR   Á   s$   	
ôùzVt100Parser.feedc                 C  s   | j  tƒ ¡ dS )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)r1   r2   r   r3   r   r   r   rD   ç   s   zVt100Parser.flushc                 C  s   |   |¡ |  ¡  dS )z8
        Wrapper around ``feed`` and ``flush``.
        N)rR   rD   )r$   rP   r   r   r   Úfeed_and_flushõ   s   
zVt100Parser.feed_and_flushN)r'   r(   r   r)   )F)r-   r   r   r)   )r   r)   )r   r   r   r4   )r   r;   )rI   rJ   rK   r   r   r)   )rP   r   r   r)   )r   r   r   r   r+   r*   r/   r:   r0   r@   rR   rD   rW   r   r   r   r   r
   F   s    




2

&)r   Ú
__future__r   ÚreÚcollections.abcr   r   Úkey_binding.key_processorr   Úkeysr   Úansi_escape_sequencesr	   Ú__all__ÚcompileÚescaper5   r7   r   r!   r   Údictr   r   r   r?   r
   r   r   r   r   Ú<module>   s"    ÿ