o
    Q `e                     @   s   d Z ddlmZ ddlmZ ddlZddlZddlmZmZm	Z	 ej
r=ddlmZmZmZmZmZ ddlmZ dd	lmZ 	
							dddZdS )zNRender a FS object as text tree views.

Color is supported on UNIX terminals.
    )print_function)unicode_literalsN)abspathjoinnormpath)ListOptionalTextTextIOTuple   )FS)Info/   Tc	                    s:  pt j|du rtddpd}tdo }	du r*t jd}
|
r(dn|	| dkr;r;d}dd	d
 nd}ddd d|d fddfddfdd	fdd
fdddd dd ddd 	
fd d!tt	|g  d" d# fS )$a  Render a directory structure in to a pretty tree.

    Arguments:
        fs (~fs.base.FS): A filesystem instance.
        path (str): The path of the directory to start rendering
            from (defaults to root folder, i.e. ``'/'``).
        file (io.IOBase): An open file-like object to render the
            tree, or `None` for stdout.
        encoding (str, optional): Unicode encoding, or `None` to
            auto-detect.
        max_levels (int, optional): Maximum number of levels to
            display, or `None` for no maximum.
        with_color (bool, optional): Enable terminal color output,
            or `None` to auto-detect terminal.
        dirs_first (bool): Show directories first.
        exclude (list, optional): Option list of directory patterns
            to exclude from the tree render.
        filter (list, optional): Optional list of files patterns to
            match in the tree render.

    Returns:
        (int, int): A tuple of ``(<directory count>, <file count>)``.

    Nencodingzutf-8isattywinFu   │u   ├u   ──u   └|z--`z    z   c                    s   t |  d dS )z$Write a line to the output.
        fileN)print)liner    )/usr/lib/python3/dist-packages/fs/tree.pywriteP   s   zrender.<locals>.writec                        s| S d|  S )z!Format the prefix lines.
        z[32m%s[0mr   )prefix
with_colorr   r   format_prefixY      zrender.<locals>.format_prefixc                    r   )z!Format a directory name.
        z[1;34m%s[0mr   )dirnamer   r   r   format_dirnamea   r"   zrender.<locals>.format_dirnamec                    r   )zFormat an error.
        z[31m%s[0mr   )msgr   r   r   format_errori   r"   zrender.<locals>.format_errorc                    s    s| S |  drd|  } | S )zFormat a filename.
        .z[33m%s[0m)
startswith)fnamer   r   r   format_filenameq   s
   
zrender.<locals>.format_filenamec                 S   s   | j  | j fS )z;Get the info sort function with directories first.
        )is_dirnamelowerinfor   r   r   sort_key_dirs_first{   s   z#render.<locals>.sort_key_dirs_firstc                 S   s
   | j  S )z@Get the default info sort function using resource name.
        )r,   r-   r.   r   r   r   sort_key   s   
zrender.<locals>.sort_keyr   )dirsfilesc           	         sj  zt j| drnd}W n3 tyE } z'dfdd|D    }d|	d| W Y d}~dS d}~ww t|d	 }t|D ]b\}}||k}|jr^d
nd  d	7  < dfdd|D }||rw n7 }|jrd| |j du st|k rt| |j||g  qPd| 
|j qPdS )z&Recursive directory function.
        )Zexclude_dirsr3   )key c                 3       | ]	}|r nV  qd S Nr   .0Zlastindentline_indentr   r   	<genexpr>       z3render.<locals>.format_directory.<locals>.<genexpr>z{} {}z
error ({})Nr   r2   r3   c                 3   r6   r7   r   r8   r:   r   r   r=      r>   )	sortedZ	filterdir	Exceptionr   formatlen	enumerater+   r,   )	pathZlevelsZ	directoryerrorr   Z_lastir/   Zis_last_entry)char_corner	char_linechar_newnodecounts
dirs_firstexcludefilterformat_directoryr$   r&   r*   r!   fsr;   r<   
max_levelsr1   r0   r   r   r   rN      sR   

z render.<locals>.format_directoryr2   r3   )
sysstdoutgetattrhasattrr   platformr(   r-   r   r   )rO   rD   r   r   rP   r    rK   rL   rM   Zis_ttyZ
is_windowsZchar_vertliner   )rG   rH   rI   rJ   rK   rL   r   rM   rN   r$   r&   r*   r!   rO   r;   r<   rP   r1   r0   r    r   r   render   s:   
$	

0*rV   )r   NNr   NTNN)__doc__Z
__future__r   r   rQ   typingZfs.pathr   r   r   ZTYPE_CHECKINGr   r   r	   r
   r   baser   r/   r   rV   r   r   r   r   <module>   s&   