B
    't\                 @   sn   d 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	gZedddZG dd	 d	eZdS )a  
patch_stdout
============

This implements a context manager that ensures that print statements within
it won't destroy the user interface. The context manager will replace
`sys.stdout` by something that draws the output above the current prompt,
rather than overwriting the UI.

Usage::

    with patch_stdout():
        ...
        application.run()
        ...

Multiple applications can run in the body of the context manager, one after the
other.
    )unicode_literals   )run_in_terminal)get_event_loop)contextmanagerNpatch_stdoutStdoutProxyFc             c   sH   t | d}tj}tj}|t_|t_z
dV  W d|  |t_|t_X dS )a  
    Replace `sys.stdout` by an :class:`_StdoutProxy` instance.

    Writing to this proxy will make sure that the text appears above the
    prompt, and that it doesn't destroy the output from the renderer.  If no
    application is curring, the behaviour should be identical to writing to
    `sys.stdout` directly.

    :param raw: (`bool`) When True, vt100 terminal escape sequences are not
                removed/escaped.
    )rawN)r   sysstdoutstderrflush)r	   proxyoriginal_stdoutZoriginal_stderr r   :lib/python3.7/site-packages/prompt_toolkit/patch_stdout.pyr   "   s    

c               @   sB   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dS )r   zp
    Proxy object for stdout which captures everything and prints output above
    the current application.
    FNc             C   sH   t |tst|ptj}|| _t | _|| _	g | _
|j| _|j| _d S )N)
isinstanceboolAssertionErrorr
   
__stdout__r   	threadingRLock_lockZ_raw_buffererrorsencoding)selfr	   r   r   r   r   __init__G   s    

zStdoutProxy.__init__c                s2   sdS  fddfdd}t  | dS )zx
        Write the given text to stdout and flush.
        If an application is running, use `run_in_terminal`.
        Nc                  s    j   j   d S )N)r   writer   r   )r   textr   r   write_and_flush_   s    z5StdoutProxy._write_and_flush.<locals>.write_and_flushc                  s   t  dd d S )NF)Zin_executor)r   r   )r    r   r   write_and_flush_in_loopc   s    z=StdoutProxy._write_and_flush.<locals>.write_and_flush_in_loop)r   Zcall_from_executor)r   r   r!   r   )r   r   r    r   _write_and_flushU   s
    zStdoutProxy._write_and_flushc             C   sT   d|krD| dd\}}| j|dg }|g| _d|}| | n| j| dS )a  
        Note: print()-statements cause to multiple write calls.
              (write('line') and write('
')). Of course we don't want to call
              `run_in_terminal` for every individual call, because that's too
              expensive, and as long as the newline hasn't been written, the
              text itself is again overwritten by the rendering of the input
              command line. Therefor, we have a little buffer which holds the
              text until a newline is written to stdout.
        
r    N)rsplitr   joinr"   append)r   dataZbeforeZafterZto_writer   r   r   r   _writel   s    

zStdoutProxy._writec             C   s    d | j}g | _| | d S )Nr$   )r&   r   r"   )r   r   r   r   r   _flush   s    zStdoutProxy._flushc          	   C   s    | j  | | W d Q R X d S )N)r   r)   )r   r(   r   r   r   r      s    zStdoutProxy.writec          	   C   s   | j  |   W dQ R X dS )z(
        Flush buffered output.
        N)r   r*   )r   r   r   r   r      s    zStdoutProxy.flush)FN)
__name__
__module____qualname____doc__r   r"   r)   r*   r   r   r   r   r   r   r   B   s   
)F)r.   Z
__future__r   Zapplicationr   Z	eventloopr   
contextlibr   r   r
   __all__r   objectr   r   r   r   r   <module>   s   