a
    lc                      @   s   d Z ddl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 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.
    N)CallableDict	GeneratorTupleUnion   )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   ]/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/input/vt100_parser.pyr   !   s   r   c                   @   s    e Zd ZdZee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returnc                    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>6   s   z:_IsPrefixOfLongerMatchCache.__missing__.<locals>.<genexpr>)_cpr_response_prefix_rematch_mouse_event_prefix_reanyr   items)selfr   resultr   r   r   __missing__-   s    z'_IsPrefixOfLongerMatchCache.__missing__N)r   r   r   r   strboolr'   r   r   r   r   r   '   s   r   c                   @   s   e Zd ZdZeegdf ddddZdedddd	Zdd
ddZ	e
edeeedf f dddZedee
ef df d
ddZee
eeedf f e
ddddZe
ddddZdd
ddZe
d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.
    N)feed_key_callbackr   c                 C   s   || _ |   d S r   )r*   reset)r%   r*   r   r   r   __init__V   s    zVt100Parser.__init__F)requestr   c                 C   s   d| _ |   d S )NF)_in_bracketed_paste_start_parser)r%   r-   r   r   r   r+   Z   s    zVt100Parser.reset)r   c                 C   s   |   | _| jd dS )z-
        Start the parser coroutine.
        N)_input_parser_generator_input_parsersendr%   r   r   r   r/   ^   s    
zVt100Parser._start_parser.r   c                 C   sD   t |rtjS t|r tjS z
t| W S  ty>   Y dS 0 dS )zD
        Return the key (or keys) that maps to this prefix.
        N)_cpr_response_rer!   r	   ZCPRResponse_mouse_event_reZVt100MouseEventr   KeyError)r%   r   r   r   r   
_get_matche   s    


zVt100Parser._get_matchc           	      c   s   d}d}d}d}|rd}ndV }t |tr0d}n||7 }|rt| }| |}|sV|sl|rl| || d}q|st|s|sd}d}tt|ddD ]>}| |d| }|r| ||d|  ||d }d}q|s| |d |d  |dd }qdS )zA
        Coroutine (state machine) for the input parser.
         FNTr   r
   )
isinstancer    _IS_PREFIX_OF_LONGER_MATCH_CACHEr7   _call_handlerrangelen)	r%   r   retryflushcZis_prefix_of_longer_matchr!   foundir   r   r   r0   x   s8    

z#Vt100Parser._input_parser_generator)keyinsert_textr   c                 C   sb   t |tr6t|D ] \}}| ||dkr,|nd qn(|tjkrNd| _d| _n| t	|| dS )z&
        Callback to handler.
        r   r8   TN)
r:   tuple	enumerater<   r	   BracketedPaster.   _paste_bufferr*   r   )r%   rD   rE   rC   r   r   r   r   r<      s    

zVt100Parser._call_handler)datar   c                 C   s   | j r||  j|7  _d}|| jv r| j|}| jd| }| ttj| d| _ | j|t| d }d| _| | n<t	|D ]2\}}| j r| ||d   qq| j
| qdS )zV
        Feed the input stream.

        :param data: Input string (unicode).
        z[201~NFr8   )r.   rI   indexr*   r   r	   rH   r>   feedrG   r1   r2   )r%   rJ   Zend_markZ	end_indexZpaste_content	remainingrC   rA   r   r   r   rL      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   r@      s    zVt100Parser.flushc                 C   s   |  | |   dS )z8
        Wrapper around ``feed`` and ``flush``.
        N)rL   r@   )r%   rJ   r   r   r   feed_and_flush   s    
zVt100Parser.feed_and_flush)F)r   r   r   r   r   r   r,   r)   r+   r/   r(   r   r	   r   r7   r   r   r0   r<   rL   r@   rN   r   r   r   r   r   C   s   " 3&)r   retypingr   r   r   r   r   Zkey_binding.key_processorr   keysr	   Zansi_escape_sequencesr   __all__compileescaper4   r5   r    r"   r   r(   r)   r   r;   r   r   r   r   r   <module>   s   