U
    祡cP                     @   s^   d dl Z d dlmZmZmZmZ ddlmZ ddlm	Z	 erHddl
mZ G dd de jZdS )	    N)IOTYPE_CHECKINGAnyList   )AnsiDecoder)Text)Consolec                   @   sj   e Zd ZdZdee ddddZeee ddd	Zee	d
ddZ
eedddZddddZdS )	FileProxyzAWraps a file (e.g. sys.stdout) and redirects writes to a console.r	   N)consolefilereturnc                 C   s   || _ || _g | _t | _d S N)_FileProxy__console_FileProxy__file_FileProxy__bufferr   _FileProxy__ansi_decoder)selfr   r    r   3/tmp/pip-unpacked-wheel-qkhzhrww/rich/file_proxy.py__init__   s    zFileProxy.__init__)r   c                 C   s   | j S )zGet proxied file.)r   r   r   r   r   rich_proxied_file   s    zFileProxy.rich_proxied_file)namer   c                 C   s   t | j|S r   )getattrr   )r   r   r   r   r   __getattr__   s    zFileProxy.__getattr__)textr   c              	      s   t |tstdt|j  j}g }|rn|d\}}}|r`|d||  |d d = q(|| qnq(|r j	}|, t
d fdd|D }|| W 5 Q R X t|S )Nz"write() argument must be str, not 
 c                 3   s   | ]} j |V  qd S r   )r   Zdecode_line).0liner   r   r   	<genexpr>,   s    z"FileProxy.write.<locals>.<genexpr>)
isinstancestr	TypeErrortype__name__r   	partitionappendjoinr   r   printlen)r   r   bufferlinesr    new_liner   outputr   r   r   write   s&    

zFileProxy.writec                 C   s,   d | j}|r| j| | jd d = d S )Nr   )r)   r   r   r*   )r   r/   r   r   r   flush2   s    zFileProxy.flush)r&   
__module____qualname____doc__r   r#   r   propertyr   r   r   intr0   r1   r   r   r   r   r
      s   r
   )iotypingr   r   r   r   ansir   r   r   r   r	   
TextIOBaser
   r   r   r   r   <module>   s   