
    ob                         d Z ddlmZ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	Zy)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)Infoc	                     xs t         j                  |t        dd      xs d}t        d      xr j	                         }	%t         j
                  j                  d      }
|
rdn|	|j                         dk(  rr	d}ddd	nd
}d
ddd|dz   fdfdfdfdfdd d ddd f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>)``.

    encodingzutf-8isattywinFu   │u   ├u   ──u   └|z--`z    z   c                      t        |        y)zWrite a line to the output.)fileN)print)liner   s    )/usr/lib/python3/dist-packages/fs/tree.pywritezrender.<locals>.writeP   s     	d    c                     s| S d| z  S )zFormat the prefix lines.z[32m%s[0m )prefix
with_colors    r   format_prefixzrender.<locals>.format_prefixX   s     M"V++r   c                     s| S d| z  S )zFormat a directory name.z[1;34m%s[0mr   )dirnamer    s    r   format_dirnamezrender.<locals>.format_dirname_   s     N$w..r   c                     s| S d| z  S )zFormat an error.z[31m%s[0mr   )msgr    s    r   format_errorzrender.<locals>.format_errorf   s     J"S((r   c                 <    s| S | j                  d      rd| z  } | S )zFormat a filename..z[33m%s[0m)
startswith)fnamer    s    r   format_filenamezrender.<locals>.format_filenamem   s*     LC '%/Er   c                 P    | j                    | j                  j                         fS )z2Get the info sort function with directories first.)is_dirnamelowerinfos    r   sort_key_dirs_firstz#render.<locals>.sort_key_dirs_firstv   s      KK!233r   c                 6    | j                   j                         S )z7Get the default info sort function using resource name.)r/   r0   r1   s    r   sort_keyzrender.<locals>.sort_key{   s     yy  r   r   )dirsfilesc                 .   	 t        j                  |       rn      }t        |      dz
  }t        |      D ]  \  }}||k(  }|j                  rd	nd
xx   dz  cc<   dj                  fd|D              }||r	nz  }|j                  rd dj	                   |
z          |j                                     t        |      k  s t        | |j                        ||gz           dj	                   |
z          |j                                      y# t        $ r^}dj                  fd|D              	z   
z   } dj	                   |       dj	                  |                         Y d}~yd}~ww xY w)zRecursive directory function.)exclude_dirsr7   )key c              3   *   K   | ]
  }|rn  y wNr   .0lastindentline_indents     r   	<genexpr>z3render.<locals>.format_directory.<locals>.<genexpr>   s     KD$K7K   z{} {}z
error ({})Nr   r6   r7   c              3   *   K   | ]
  }|rn  y wr=   r   r>   s     r   rC   z3render.<locals>.format_directory.<locals>.<genexpr>   s     PtV<PrD   )	sorted	filterdir	Exceptionr   formatlen	enumerater.   r/   )pathlevels	directoryerrorr   _lastir2   is_last_entrychar_corner	char_linechar_newnodecounts
dirs_firstexcludefilterformat_directoryr$   r'   r,   r!   fsrA   rB   
max_levelsr5   r3   r   s            r   rZ   z render.<locals>.format_directory   s   	TvF+5'8I  I" + 	GAtJMT[[6g6!;6WWPPPF]kDF{{NN%fy&89>$));T
 %Vz)A$T$		%:Fm_<TUNN%fy&89?499;U	  	KFKK 
 !&)<8K8KE8R+S
 	s   #D- -	F6AFFr6   r7   )
sysstdoutgetattrhasattrr   platformr*   r0   r   r   )r[   rL   r   r   r\   r    rW   rX   rY   is_tty
is_windowschar_vertlinerS   rT   rU   rV   rZ   r$   r'   r,   r!   rA   rB   r5   r3   r   s   ` ` `````   @@@@@@@@@@@@@@r   renderre      s   H 3::D4W5@T8$6F\\,,U3
(Uf
~~7"z		F')K,/)4
!
 !$F' ' 'R WXd^,b1&>6'?**r   )/NN   NTNN)__doc__
__future__r   r   r]   typingfs.pathr   r   r   TYPE_CHECKINGr   r	   r
   r   r   baser   r2   r   re   r   r   r   <module>rn      sP   
 8 
  + +	::
 
	W+r   