U
    ouf)                     @  s  U d dl mZ d dlZejdks$td dlZd dlZd dlZd dlZd dl	m
Z
mZ d dl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dgZG dd deZi Zded< ejddddddZejdddddZG dd dZ G dd de Z!dS )    )annotationsNwin32)AbstractEventLoopget_running_loop)CallableContextManager	GeneratorTextIO   )KeyPress   )Input)PosixStdinReader)Vt100Parser
Vt100Inputraw_modecooked_modec                   @  s   e Zd ZU dZe Zded< dddddZd	d
dddZd
dddZ	ddddZ
ddddZeddddZd
dddZd
dddZddddZd d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.)
    zset[int]_fds_not_a_terminalr	   None)stdinreturnc              
     s   z|   W nH tjk
rT } z(dtjkr8td|ntd|W 5 d }~X Y nX | }|  }|s|tjkrd}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)Z	key_pressself >/tmp/pip-unpacked-wheel-8milen2s/prompt_toolkit/input/vt100.py<lambda>J       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__$   s0    



zVt100Input.__init__Callable[[], None]zContextManager[None])input_ready_callbackr   c                 C  s
   t | |S )zj
        Return a context manager that makes this input active in the current
        event loop.
        )_attached_input)r   r3   r   r   r   attachM   s    zVt100Input.attachr   c                 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   detachT   s    zVt100Input.detachzlist[KeyPress]c                 C  s&   | j  }| j| | j}g | _|S )zRead list of KeyPress.)r,   readr-   feedr   )r   dataresultr   r   r   	read_keys[   s
    
zVt100Input.read_keysc                 C  s   | j   | j}g | _|S )zc
        Flush pending keys and return them.
        (Used for flushing the 'escape' key.)
        )r-   r)   r   )r   r<   r   r   r   
flush_keysh   s    
zVt100Input.flush_keysboolc                 C  s   | j jS r   )r,   closedr   r   r   r   r@   v   s    zVt100Input.closedc                 C  s   t | j S r   )r   r   r!   r   r   r   r   r   z   s    zVt100Input.raw_modec                 C  s   t | j S r   )r   r   r!   r   r   r   r   r   }   s    zVt100Input.cooked_modeintc                 C  s
   | j  S r   )r   r!   r   r   r   r   r!      s    zVt100Input.filenostrc                 C  s   d| j  S )Nzfd-)r+   r   r   r   r   typeahead_hash   s    zVt100Input.typeahead_hashN)__name__
__module____qualname____doc__setr   __annotations__r1   r5   r8   r=   r>   propertyr@   r   r   r!   rC   r   r   r   r   r      s   
)z>dict[tuple[AbstractEventLoop, int], Callable[[], None] | None]_current_callbacksr2   zGenerator[(None, None, None)])inputcallbackr   c              
   #  s   t   tf}dd fdd}z| W n tk
r\   tY nX  tf< z
dV  W 5  |r| |tf< n
tf= X dS )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   r6   c                     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)r@   remove_readerr   rM   r/   rL   loopr   r   callback_wrapper   s    
z)_attached_input.<locals>.callback_wrapperN)r   r!   rK   get
add_readerPermissionErrorEOFErrorrN   )rL   rM   previousrQ   r   rO   r   r4      s     




r4   )rL   r   c              
   c  sd   t  }|  }t||f}|r6|| d t||f< z
d V  W 5 |r^||| |t||f< X d S r   )r   r!   rK   rR   rN   rS   )rL   rP   r/   rV   r   r   r   r7      s    

r7   c                   @  sf   e Zd ZdZdddddZdddd	Zeddd
ddZeddd
ddZddd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.
    rA   r   )r!   r   c                 C  s<   || _ |  zt|| _W n tjk
r6   d | _Y nX d S r   )r!   termios	tcgetattrattrs_beforeerror)r   r!   r   r   r   r1      s    zraw_mode.__init__r6   c                 C  sz   zt | j}W n t jk
r&   Y nPX | |tj |tj< | |tj |tj< d|tj	 t j
< t | jt j| d S )Nr   )rW   rX   r!   rZ   _patch_lflagttyZLFLAG_patch_iflagZIFLAGCCVMIN	tcsetattrTCSANOW)r   Znewattrr   r   r   	__enter__   s    zraw_mode.__enter__attrsr   c                 C  s   |t jt jB t jB t jB  @ S r   rW   ECHOICANONIEXTENISIGclsrd   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   )rW   IXONIXOFFICRNLINLCRIGNCRrj   r   r   r   r]     s    zraw_mode._patch_iflagobject)ar   c                 G  s>   | j d k	r:zt| jtj| j  W n tjk
r8   Y nX d S r   )rY   rW   r`   r!   ra   rZ   )r   rr   r   r   r   __exit__  s
    
zraw_mode.__exit__N)
rD   rE   rF   rG   r1   rb   classmethodr[   r]   rs   r   r   r   r   r      s   	c                   @  s8   e Zd ZdZedddddZeddd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. '''
    rA   rc   c                 C  s   |t jt jB t jB t jB B S r   re   rj   r   r   r   r[   +  s    zcooked_mode._patch_lflagc                 C  s
   |t jB S r   )rW   rn   rj   r   r   r   r]   /  s    zcooked_mode._patch_iflagN)rD   rE   rF   rG   rt   r[   r]   r   r   r   r   r   "  s
   )"
__future__r   r$   platformAssertionError
contextlibr"   rW   r\   Zasyncior   r   typingr   r   r   r	   Zkey_bindingr   baser   Zposix_utilsr   r-   r   __all__r   rK   rI   contextmanagerr4   r7   r   r   r   r   r   r   <module>   s4    o
1R