o
    ˷eu)                     @   sV  U d dl Z e jdksJ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZmZmZ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d
lmZ ddlmZ g dZG dd deZi Zeeee f ee	g df  f e!d< ej"dede	g df ded fddZ#ej"deded fddZ$G dd dZ%G dd de%Z&dS )    Nwin32)AbstractEventLoop)
CallableContextManagerDict	GeneratorListOptionalSetTextIOTupleUnion)get_event_loop   )KeyPress   )Input)PosixStdinReader)Vt100Parser)
Vt100Inputraw_modecooked_modec                   @   s   e Zd ZU dZe Zee ed< de	ddfddZ
deg df ded fd	d
Zded fddZdee fddZdee fddZedefddZded fddZded fddZdefddZdefddZdS )r   zz
    Vt100 input for Posix systems.
    (This uses a posix file descriptor that can be registered in the event loop.)
    _fds_not_a_terminalstdinreturnNc              
      s   z|   W n tjy$ } zdtjv rtd|td|d }~ww | }|  }|sI|tjvrId}tj	||  tj
  tj| | _|   _g  _t j|jd _t fdd _d S )Nzidlelib.runz<Stdin is not a terminal. Running from Idle is not supported.zStdin is not a terminal.z*Warning: Input is not a terminal (fd=%r).
)encodingc                    s    j | S N)_bufferappend)	key_pressself Q/var/www/ideatree/venv/lib/python3.10/site-packages/prompt_toolkit/input/vt100.py<lambda>U   s    z%Vt100Input.__init__.<locals>.<lambda>)filenoioUnsupportedOperationsysmodulesisattyr   r   stderrwriteflushaddr   _filenor   r   r   stdin_readerr   vt100_parser)r!   r   er*   fdmsgr"   r    r#   __init__/   s4   




zVt100Input.__init__input_ready_callbackc                 C   s
   t | |S )zj
        Return a context manager that makes this input active in the current
        event loop.
        )_attached_input)r!   r6   r"   r"   r#   attachX   s   
zVt100Input.attachc                 C   s   t | S )z{
        Return a context manager that makes sure that this input is not active
        in the current event loop.
        )_detached_inputr    r"   r"   r#   detach_   s   zVt100Input.detachc                 C   s&   | j  }| j| | j}g | _|S )zRead list of KeyPress.)r0   readr1   feedr   )r!   dataresultr"   r"   r#   	read_keysf   s
   
zVt100Input.read_keysc                 C   s   | j   | j}g | _|S )zc
        Flush pending keys and return them.
        (Used for flushing the 'escape' key.)
        )r1   r-   r   )r!   r>   r"   r"   r#   
flush_keyss   s   
zVt100Input.flush_keysc                 C   s   | j jS r   )r0   closedr    r"   r"   r#   rA      s   zVt100Input.closedc                 C      t | j S r   )r   r   r%   r    r"   r"   r#   r         zVt100Input.raw_modec                 C   rB   r   )r   r   r%   r    r"   r"   r#   r      rC   zVt100Input.cooked_modec                 C   s
   | j  S r   )r   r%   r    r"   r"   r#   r%      s   
zVt100Input.filenoc                 C   s   d| j  S )Nzfd-)r/   r    r"   r"   r#   typeahead_hash   s   zVt100Input.typeahead_hash)__name__
__module____qualname____doc__setr   r
   int__annotations__r   r5   r   r   r8   r:   r   r   r?   r@   propertyboolrA   r   r   r%   strrD   r"   r"   r"   r#   r   %   s   
 )r   _current_callbacksinputcallbackr   )NNNc              
   #   s    t   tf}d fdd}z| W n	 ty*   tw  tf< z dV  W  |rK| |tf< dS tf= dS  |rf| |tf< w tf= w )z
    Context manager that makes this input active in the current event loop.

    :param input: :class:`~prompt_toolkit.input.Input` object.
    :param callback: Called when the input is ready to read.
    r   Nc                      s   j r    dS )a:  Wrapper around the callback that already removes the reader when
        the input is closed. Otherwise, we keep continuously calling this
        callback, until we leave the context manager (which can happen a bit
        later). This fixes issues when piping /dev/null into a prompt_toolkit
        application.N)rA   remove_readerr"   rQ   r3   rP   loopr"   r#   callback_wrapper   s   

z)_attached_input.<locals>.callback_wrapperr   N)r   r%   rO   get
add_readerPermissionErrorEOFErrorrR   )rP   rQ   previousrU   r"   rS   r#   r7      s.   




r7   c              
   c   s    t  }|  }t||f}|r|| d t||f< zd V  W |r1||| |t||f< d S d S |rA||| |t||f< w r   )r   r%   rO   rW   rR   rX   )rP   rT   r3   r[   r"   r"   r#   r9      s   
r9   c                   @   sj   e Zd ZdZdeddfddZdddZed	edefd
dZed	edefddZ	de
ddfddZdS )r   z
    ::

        with raw_mode(stdin):
            ''' the pseudo-terminal stdin is now used in raw mode '''

    We ignore errors when executing `tcgetattr` fails.
    r%   r   Nc                 C   s:   || _ |  z	t|| _W d S  tjy   d | _Y d S w r   )r%   termios	tcgetattrattrs_beforeerror)r!   r%   r"   r"   r#   r5      s   zraw_mode.__init__c                 C   sz   zt | j}W n t jy   Y d S w | |tj |tj< | |tj |tj< d|tj	 t j
< t | jt j| d S )Nr   )r\   r]   r%   r_   _patch_lflagttyLFLAG_patch_iflagIFLAGCCVMIN	tcsetattrTCSANOW)r!   newattrr"   r"   r#   	__enter__   s   zraw_mode.__enter__attrsc                 C   s   |t jt jB t jB t jB  @ S r   r\   ECHOICANONIEXTENISIGclsrk   r"   r"   r#   r`     s   zraw_mode._patch_lflagc                 C   s$   |t jt jB t jB t jB t jB  @ S r   )r\   IXONIXOFFICRNLINLCRIGNCRrq   r"   r"   r#   rc     s   zraw_mode._patch_iflagac                 G   s@   | j d urzt| jtj| j  W d S  tjy   Y d S w d S r   )r^   r\   rg   r%   rh   r_   )r!   rx   r"   r"   r#   __exit__"  s   
zraw_mode.__exit__rV   )rE   rF   rG   rH   rJ   r5   rj   classmethodr`   rc   objectry   r"   r"   r"   r#   r      s    
	r   c                   @   s<   e Zd ZdZededefddZededefddZdS )	r   z
    The opposite of ``raw_mode``, used when we need cooked mode inside a
    `raw_mode` block.  Used in `Application.run_in_terminal`.::

        with cooked_mode(stdin):
            ''' the pseudo-terminal stdin is now used in cooked mode. '''
    rk   r   c                 C   s   |t jt jB t jB t jB B S r   rl   rq   r"   r"   r#   r`   6  s   zcooked_mode._patch_lflagc                 C   s
   |t jB S r   )r\   ru   rq   r"   r"   r#   rc   :  s   
zcooked_mode._patch_iflagN)rE   rF   rG   rH   rz   rJ   r`   rc   r"   r"   r"   r#   r   -  s    r   )'r(   platform
contextlibr&   r\   ra   asyncior   typingr   r   r   r   r   r	   r
   r   r   r   prompt_toolkit.eventloopr   key_bindingr   baser   posix_utilsr   r1   r   __all__r   rO   rJ   rK   contextmanagerr7   r9   r   r   r"   r"   r"   r#   <module>   s>   
 0o
1R