B
    '˜t\•#  ã               @   sÊ   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	 d dl
Z
d dlZd dlZd dlZd dlZd dlZd	d
dgZG dd	„ d	eƒZi Zejdd„ ƒZejdd„ ƒZG dd
„ d
eƒZG dd„ deƒZdS )é    )Úunicode_literalsé   )Úget_event_loopé   )ÚInput)ÚPosixStdinReader)ÚVt100ParserNÚ
Vt100InputÚraw_modeÚcooked_modec               @   sv   e Zd ZdZeƒ Zdd„ Zedd„ ƒZdd„ Z	dd	„ Z
d
d„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Z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.)
    c                sº   y|  ¡  W n6 tjk
rB   dtjkr4t d¡‚n
t d¡‚Y nX | ¡ }|  ¡ }|s‚|tjkr‚d}tj 	|| ¡ tj 
|¡ |ˆ _|  ¡ ˆ _g ˆ _tˆ jƒˆ _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 to a terminal (fd=%r).
c                s   ˆ j  | ¡S )N)Ú_bufferÚappend)Úkey)Úself© ú9lib/python3.7/site-packages/prompt_toolkit/input/vt100.pyÚ<lambda>A   s    z%Vt100Input.__init__.<locals>.<lambda>)ÚfilenoÚioÚUnsupportedOperationÚsysÚmodulesÚisattyr	   Ú_fds_not_a_terminalÚstderrÚwriteÚaddÚstdinÚ_filenor   r   Ústdin_readerr   Úvt100_parser)r   r   r   ÚfdÚmsgr   )r   r   Ú__init__   s&    

zVt100Input.__init__c             C   s
   | j  ¡ S )N)r   r   )r   r   r   r   Úresponds_to_cprC   s    zVt100Input.responds_to_cprc             C   s   t |ƒst‚t| |ƒS )zj
        Return a context manager that makes this input active in the current
        event loop.
        )ÚcallableÚAssertionErrorÚ_attached_input)r   Zinput_ready_callbackr   r   r   ÚattachI   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_input)r   r   r   r   ÚdetachQ   s    zVt100Input.detachc             C   s&   | j  ¡ }| j |¡ | j}g | _|S )z Read list of KeyPress. )r   Úreadr    Zfeedr   )r   ÚdataÚresultr   r   r   Ú	read_keysX   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    Úflushr   )r   r-   r   r   r   Ú
flush_keyse   s    
zVt100Input.flush_keysc             C   s   | j jS )N)r   Úclosed)r   r   r   r   r1   s   s    zVt100Input.closedc             C   s   t | j ¡ ƒS )N)r
   r   r   )r   r   r   r   r
   w   s    zVt100Input.raw_modec             C   s   t | j ¡ ƒS )N)r   r   r   )r   r   r   r   r   z   s    zVt100Input.cooked_modec             C   s
   | j  ¡ S )N)r   r   )r   r   r   r   r   }   s    zVt100Input.filenoc             C   s   d| j f S )Nzfd-%s)r   )r   r   r   r   Útypeahead_hash€   s    zVt100Input.typeahead_hashN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úsetr   r#   Úpropertyr$   r(   r*   r.   r0   r1   r
   r   r   r2   r   r   r   r   r	      s   &c          
   c   sx   t ƒ }|  ¡ }t ||f¡}| ||¡ |t||f< z
dV  W d| |¡ |rh| ||¡ |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.
    N)r   r   Ú_current_callbacksÚgetÚ
add_readerÚremove_reader)ÚinputÚcallbackÚloopr!   Úpreviousr   r   r   r'   ‡   s    

r'   c          
   c   sd   t ƒ }|  ¡ }t ||f¡}|r6| |¡ d t||f< z
d V  W d |r^| ||¡ |t||f< X d S )N)r   r   r9   r:   r<   r;   )r=   r?   r!   r@   r   r   r   r)   ¢   s    

r)   c               @   s@   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZ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.
    c             C   s8   || _ yt |¡| _W n tjk
r2   d | _Y nX d S )N)r   ÚtermiosÚ	tcgetattrÚattrs_beforeÚerror)r   r   r   r   r   r#   Ë   s
    zraw_mode.__init__c             C   sˆ   yt  | j¡}W n t jk
r&   Y n^X |  |tj ¡|tj< |  |tj ¡|tj< d|tj	 t j
< t  | jt j|¡ t | jd¡ d S )Nr   s   [?1l)rA   rB   r   rD   Ú_patch_lflagÚttyZLFLAGÚ_patch_iflagZIFLAGZCCZVMINÚ	tcsetattrÚTCSANOWÚosr   )r   Znewattrr   r   r   Ú	__enter__Ó   s    zraw_mode.__enter__c             C   s   |t jt jB t jB t jB  @ S )N)rA   ÚECHOÚICANONÚIEXTENÚISIG)ÚclsÚattrsr   r   r   rE   é   s    zraw_mode._patch_lflagc             C   s$   |t jt jB t jB t jB t jB  @ S )N)rA   ZIXONZIXOFFÚICRNLZINLCRZIGNCR)rP   rQ   r   r   r   rG   í   s    zraw_mode._patch_iflagc             O   s>   | j d k	r:yt | jtj| j ¡ W n tjk
r8   Y nX d S )N)rC   rA   rH   r   rI   rD   )r   ÚaÚkwr   r   r   Ú__exit__ù   s
    
zraw_mode.__exit__N)
r3   r4   r5   r6   r#   rK   ÚclassmethodrE   rG   rU   r   r   r   r   r
   ´   s   c               @   s(   e Zd ZdZedd„ ƒZe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. '''
    c             C   s   |t jt jB t jB t jB B S )N)rA   rL   rM   rN   rO   )rP   rQ   r   r   r   rE     s    zcooked_mode._patch_lflagc             C   s
   |t jB S )N)rA   rR   )rP   rQ   r   r   r   rG     s    zcooked_mode._patch_iflagN)r3   r4   r5   r6   rV   rE   rG   r   r   r   r   r     s   )Z
__future__r   Z	eventloopr   Úbaser   Zposix_utilsr   r    r   r   rJ   r   rA   rF   Ú
contextlibÚ__all__r	   r9   Úcontextmanagerr'   r)   Úobjectr
   r   r   r   r   r   Ú<module>   s&   oP