U
    oufV                     @  s   d dl mZ d dlZejdks"td dlZd dl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gZG dd dZG dd deeZdS )    )annotationsNwin32)contextmanager)ContextManagerIteratorTextIOcast   DummyContext   )	PipeInput)
Vt100InputPosixPipeInputc                   @  sH   e Zd ZdZddddZddddZdddd	Zddd
dZdS )_PipezCWrapper around os.pipe, that ensures we don't double close any end.Nonereturnc                 C  s    t  \| _| _d| _d| _d S )NF)ospiperead_fdwrite_fd_read_closed_write_closedself r   C/tmp/pip-unpacked-wheel-8milen2s/prompt_toolkit/input/posix_pipe.py__init__   s    z_Pipe.__init__c                 C  s    | j r
dS t| j d| _ dS )z!Close read-end if not yet closed.NT)r   r   closer   r   r   r   r   
close_read   s    z_Pipe.close_readc                 C  s    | j r
dS t| j d| _ dS )z"Close write-end if not yet closed.NT)r   r   r   r   r   r   r   r   close_write$   s    z_Pipe.close_writec                 C  s   |    |   dS )zClose both read and write ends.N)r    r!   r   r   r   r   r   ,   s    z_Pipe.closeN)__name__
__module____qualname____doc__r   r    r!   r   r   r   r   r   r      s
   r   c                      s   e Zd ZdZdZddddd fdd	Zeeddd
dddZ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ddddZ  ZS ) r   a  
    Input that is send through a pipe.
    This is useful if we want to send the input programmatically into the
    application. Mostly useful for unit testing.

    Usage::

        with PosixPipeInput.create() as input:
            input.send_text('inputdata')
    r    r   strr   )_pipe_textr   c                   sT   | _ G  fddd}t tt|   |  j jd7  _ jj _d S )Nc                      s0   e Zd ZdZddddZdd fddZd	S )
z&PosixPipeInput.__init__.<locals>.Stdinutf-8boolr   c                 S  s   dS )NTr   stdinr   r   r   isattyG   s    z-PosixPipeInput.__init__.<locals>.Stdin.isattyintc                   s    j jS N)r   r   r,   r   r   r   filenoJ   s    z-PosixPipeInput.__init__.<locals>.Stdin.filenoN)r"   r#   r$   encodingr.   r1   r   r   r   r   StdinD   s   r3   r   )r   superr   r   r   	send_text	__class___id)r   r(   r)   r3   r6   r   r   r   @   s    	
zPosixPipeInput.__init__zIterator[PosixPipeInput])textr   c                 c  s(   t  }zt||dV  W 5 |  X d S )N)r(   r)   )r   r   r   )clsr9   r   r   r   r   createT   s    zPosixPipeInput.createbytes)datar   c                 C  s   t | jj| d S r0   )r   writer   r   r   r=   r   r   r   
send_bytes]   s    zPosixPipeInput.send_bytesc                 C  s   t | jj|d dS )zSend text to the input.r*   N)r   r>   r   r   encoder?   r   r   r   r5   `   s    zPosixPipeInput.send_textzContextManager[None]r   c                 C  s   t  S r0   r
   r   r   r   r   raw_moded   s    zPosixPipeInput.raw_modec                 C  s   t  S r0   r
   r   r   r   r   cooked_modeg   s    zPosixPipeInput.cooked_modec                 C  s   | j   dS )zClose pipe fds.N)r   r!   r   r   r   r   r   j   s    zPosixPipeInput.closec                 C  s   d| j  S )z@
        This needs to be unique for every `PipeInput`.
        zpipe-input-)r7   r   r   r   r   typeahead_hashr   s    zPosixPipeInput.typeahead_hash)r&   )r&   )r"   r#   r$   r%   r7   r   classmethodr   r;   r@   r5   rB   rC   r   rD   __classcell__r   r   r8   r   r   2   s   )
__future__r   sysplatformAssertionErrorr   
contextlibr   typingr   r   r   r   utilsr   baser   Zvt100r   __all__r   r   r   r   r   r   <module>   s   