U
    dh)                     @  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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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Dict	GeneratorListOptionalSetTextIOTupleUnion   )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-4x_7prb2/prompt_toolkit/input/vt100.py<lambda>U       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"   r9   r#   r#   r$   attachX   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$   detach_   s    zVt100Input.detachzlist[KeyPress]c                 C  s&   | j  }| j| | j}g | _|S )zRead list of KeyPress.)r2   readr3   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.)
        )r3   r/   r   )r"   rB   r#   r#   r$   
flush_keyss   s    
zVt100Input.flush_keysboolc                 C  s   | j jS r   )r2   closedr!   r#   r#   r$   rF      s    zVt100Input.closedc                 C  s   t | j S r   )r   r   r'   r!   r#   r#   r$   r      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-)r1   r!   r#   r#   r$   typeahead_hash   s    zVt100Input.typeahead_hashN)__name__
__module____qualname____doc__setr   __annotations__r7   r;   r>   rC   rD   propertyrF   r   r   r'   rI   r#   r#   r#   r$   r   %   s   
)z>dict[tuple[AbstractEventLoop, int], Callable[[], None] | None]_current_callbacksr8   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   r<   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)rF   remove_readerr#   rS   r5   rR   loopr#   r$   callback_wrapper   s    
z)_attached_input.<locals>.callback_wrapperN)r   r'   rQ   get
add_readerPermissionErrorEOFErrorrT   )rR   rS   previousrW   r#   rU   r$   r:      s     




r:   )rR   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'   rQ   rX   rT   rY   )rR   rV   r5   r\   r#   r#   r$   r=      s    

r=   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.
    rG   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$   r7      s    zraw_mode.__init__r<   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   )r]   r^   r'   r`   _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   r]   ECHOICANONIEXTENISIGclsrj   r#   r#   r$   ra     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IGNCRrp   r#   r#   r$   rc     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   )r_   r]   rf   r'   rg   r`   )r"   rx   r#   r#   r$   __exit__"  s
    
zraw_mode.__exit__N)
rJ   rK   rL   rM   r7   rh   classmethodra   rc   ry   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. '''
    rG   ri   c                 C  s   |t jt jB t jB t jB B S r   rk   rp   r#   r#   r$   ra   6  s    zcooked_mode._patch_lflagc                 C  s
   |t jB S r   )r]   rt   rp   r#   r#   r$   rc   :  s    zcooked_mode._patch_iflagN)rJ   rK   rL   rM   rz   ra   rc   r#   r#   r#   r$   r   -  s
   )(
__future__r   r*   platformAssertionError
contextlibr(   r]   rb   Zasyncior   r   typingr   r   r   r	   r
   r   r   r   r   r   Zkey_bindingr   baser   Zposix_utilsr   r3   r   __all__r   rQ   rO   contextmanagerr:   r=   r   r   r#   r#   r#   r$   <module>   s4    0o
1R