o
    ih)                     @  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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 g dZG dd deZi Z de!d< ej"dddZ#ej"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	Zd%ddZd&ddZ	d'ddZ
d'ddZed(ddZd&ddZd&ddZd)ddZ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_terminalstdinr   returnNonec              
     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 U/var/www/edux/Edux_v2/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_callbackCallable[[], None]ContextManager[None]c                 C  s
   t | |S )zj
        Return a context manager that makes this input active in the current
        event loop.
        )_attached_input)r#   r8   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.detach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#   rC   r$   r$   r%   
flush_keyss   s   
zVt100Input.flush_keysboolc                 C  s   | j jS r   )r2   closedr"   r$   r$   r%   rG      s   zVt100Input.closedc                 C     t | j S r   )r   r   r'   r"   r$   r$   r%   r         zVt100Input.raw_modec                 C  rH   r   )r   r   r'   r"   r$   r$   r%   r      rI   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)r   r   r   r   )r8   r9   r   r:   )r   r:   )r   r?   )r   rF   )r   rJ   )r   rK   )__name__
__module____qualname____doc__setr   __annotations__r7   r<   r>   rD   rE   propertyrG   r   r   r'   rL   r$   r$   r$   r%   r   %   s   
 

)





r   z>dict[tuple[AbstractEventLoop, int], Callable[[], None] | None]_current_callbacksinputcallbackr9   r   Generator[None, None, None]c              
   #  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   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)rG   remove_readerr$   rV   r5   rU   loopr$   r%   callback_wrapper   s   

z)_attached_input.<locals>.callback_wrapperNr   r   )r   r'   rT   get
add_readerPermissionErrorEOFErrorrX   )rU   rV   previousr[   r$   rY   r%   r;      s.   




r;   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'   rT   r]   rX   r^   )rU   rZ   r5   ra   r$   r$   r%   r=      s   
r=   c                   @  sJ   e Zd ZdZdddZddd	ZedddZedddZ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.
    r'   rJ   r   r   c                 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%   r7      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   )rb   rc   r'   re   _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   rb   ECHOICANONIEXTENISIGclsrq   r$   r$   r%   rf     s   zraw_mode._patch_lflagc                 C  s$   |t jt jB t jB t jB t jB  @ S r   )rb   IXONIXOFFICRNLINLCRIGNCRrw   r$   r$   r%   ri     s   zraw_mode._patch_iflagaobjectc                 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   )rd   rb   rm   r'   rn   re   )r#   r~   r$   r$   r%   __exit__"  s   
zraw_mode.__exit__N)r'   rJ   r   r   r\   rq   rJ   r   rJ   )r~   r   r   r   )
rM   rN   rO   rP   r7   rp   classmethodrf   ri   r   r$   r$   r$   r%   r      s    

	r   c                   @  s,   e Zd ZdZed
ddZe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. '''
    rq   rJ   r   c                 C  s   |t jt jB t jB t jB B S r   rr   rw   r$   r$   r%   rf   6  s   zcooked_mode._patch_lflagc                 C  s
   |t jB S r   )rb   r{   rw   r$   r$   r%   ri   :  s   
zcooked_mode._patch_iflagNr   )rM   rN   rO   rP   r   rf   ri   r$   r$   r$   r%   r   -  s    r   )rU   r   rV   r9   r   rW   )rU   r   r   rW   )'
__future__r   r*   platform
contextlibr(   rb   rg   asyncior   r   typingr   r   r   r	   r
   r   r   r   r   r   key_bindingr   baser   posix_utilsr   r3   r   __all__r   rT   rR   contextmanagerr;   r=   r   r   r$   r$   r$   r%   <module>   s.    0o
1R