a
    õ£Be)  ã                   @  s  U d dl mZ 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	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 g d¢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 nJ tjyV } z0dtjv r6t d¡|‚nt d¡|‚W Y d }~n
d }~0 0 | ¡ }|  ¡ }|s |tjvr d}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© ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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   
)r   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yZ   t‚Y n0 ˆ tˆˆf< z<dV  W ˆ ˆ¡ |ršˆ ˆ|¡ |tˆˆf< qÚtˆˆf= n4ˆ ˆ¡ |rÎˆ ˆ|¡ |tˆˆf< n
tˆˆf= 0 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  s€   t ƒ }|  ¡ }t ||f¡}|r6| |¡ d t||f< z&d V  W |r|| ||¡ |t||f< n|rz| ||¡ |t||f< 0 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y4   d | _Y n0 d S r   )r!   ÚtermiosÚ	tcgetattrÚattrs_beforeÚerror)r   r!   r   r   r   r1   è   s    zraw_mode.__init__r6   c                 C  sx   zt  | j¡}W n t jy$   Y nP0 |  |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 ur8zt | jtj| j ¡ W n tjy6   Y n0 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   	r   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
   r   )!Ú
__future__r   r$   ÚplatformÚ
contextlibr"   rW   r\   Ú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>   s,   oþ
1R