U
    ;»{j´6  ã                   @   st  d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZmZmZ ddlm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dl"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z* e
rød dl+m,Z, G dd„ deƒZ-ede#f Z.ede-f Z/G dd„ de0ƒZ1G dd„ de1ƒZ2G dd„ dƒZ3G dd„ deƒZ4G dd„ de4ƒZ5G dd „ d e4ƒZ6e&G d!d„ dƒƒZ7e8d"k�rpd d#l9mZ eƒ Ze7ƒ Z:e: ;e7d$d%d&�e7dd'd(�e7d)d*d+�¡ e:d'  <e7d,d-�e7d.d/d0�¡ e:d.  <e7d1d/d0�e7d2d-�¡ e:d2  ;e7d3d-�e7d4d-�e7d5d-�¡ e:d,  ;e7e:j=d6d-�e7d7d-�¡ e:d1  >d8¡ e ?e:¡ d9S ):é    )ÚABCÚabstractmethod)Úislice)Ú
itemgetter)ÚRLock)	ÚTYPE_CHECKINGÚDictÚIterableÚListÚ
NamedTupleÚOptionalÚSequenceÚTupleÚUnioné   )Úratio_resolve)ÚAlign)ÚConsoleÚConsoleOptionsÚRenderableTypeÚRenderResult)ÚReprHighlighter)ÚPanel)ÚPretty)ÚRegion)ÚResultÚ	rich_repr)ÚSegment)Ú	StyleType©ÚTreec                   @   s*   e Zd ZU dZeed< eee  ed< dS )ÚLayoutRenderzAn individual layout render.ÚregionÚrenderN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r
   r   © r)   r)   ú8/tmp/pip-install-zsn1f9xv/pip/pip/_vendor/rich/layout.pyr!       s   
r!   ÚLayoutc                   @   s   e Zd ZdZdS )ÚLayoutErrorzLayout related error.N©r$   r%   r&   r'   r)   r)   r)   r*   r,   +   s   r,   c                   @   s   e Zd ZdZdS )Ú
NoSplitterz"Requested splitter does not exist.Nr-   r)   r)   r)   r*   r.   /   s   r.   c                   @   s<   e Zd ZdZeƒ Zddeddœdd„Zee	e
dœd	d
„ZdS )Ú_Placeholderz4An internal renderable used as a Layout placeholder.Ú r+   N)ÚlayoutÚstyleÚreturnc                 C   s   || _ || _d S ©N)r1   r2   )Úselfr1   r2   r)   r)   r*   Ú__init__8   s    z_Placeholder.__init__©ÚconsoleÚoptionsr3   c                 c   sx   |j }|jp|jj}| j}|jr8|j›d|› d|› d�nd|› d|› d�}ttjt|ƒdd�| j	|  
|¡d|d�V  d S )	Nz (z x ú)ú(Úmiddle)ZverticalZblue)r2   ÚtitleZborder_styleÚheight)Ú	max_widthr>   Úsizer1   Únamer   r   Úcenterr   r2   Úhighlighter)r5   r8   r9   Úwidthr>   r1   r=   r)   r)   r*   Ú__rich_console__<   s    ÿýûz_Placeholder.__rich_console__)r0   )r$   r%   r&   r'   r   rC   r   r6   r   r   r   rE   r)   r)   r)   r*   r/   3   s    þr/   c                   @   sV   e Zd ZU dZdZeed< eedœdd„ƒZee	d e
eede
f  dœd	d
„ƒZdS )ÚSplitterzBase class for a splitter.r0   rA   ©r3   c                 C   s   dS )z(Get the icon (emoji) used in layout.treeNr)   ©r5   r)   r)   r*   Úget_tree_iconU   s    zSplitter.get_tree_iconr+   ©Úchildrenr"   r3   c                 C   s   dS )zÇDivide a region amongst several child layouts.

        Args:
            children (Sequence(Layout)): A number of child layouts.
            region (Region): A rectangular region to divide.
        Nr)   )r5   rK   r"   r)   r)   r*   ÚdivideY   s    zSplitter.divideN)r$   r%   r&   r'   rA   Ústrr(   r   rI   r   r   r	   r   rL   r)   r)   r)   r*   rF   P   s   
 þrF   c                   @   sD   e Zd ZdZdZedœdd„Zed ee	e
def  dœdd	„Zd
S )ÚRowSplitterz!Split a layout region in to rows.ÚrowrG   c                 C   s   dS )Nu   [layout.tree.row]â¬Œr)   rH   r)   r)   r*   rI   j   s    zRowSplitter.get_tree_iconr+   rJ   c                 c   sV   |\}}}}t ||ƒ}d}t}	t||ƒD ](\}
}|
|	|| |||ƒfV  ||7 }q(d S ©Nr   ©r   r   Úzip)r5   rK   r"   ÚxÚyrD   r>   Zrender_widthsÚoffsetÚ_RegionÚchildZchild_widthr)   r)   r*   rL   m   s    
zRowSplitter.divideN©r$   r%   r&   r'   rA   rM   rI   r   r   r	   r   rL   r)   r)   r)   r*   rN   e   s    þrN   c                   @   sD   e Zd ZdZdZedœdd„Zed ee	e
def  dœdd	„Zd
S )ÚColumnSplitterz$Split a layout region in to columns.ÚcolumnrG   c                 C   s   dS )Nu   [layout.tree.column]â¬�r)   rH   r)   r)   r*   rI   ~   s    zColumnSplitter.get_tree_iconr+   rJ   c                 c   sV   |\}}}}t ||ƒ}d}t}	t||ƒD ](\}
}|
|	||| ||ƒfV  ||7 }q(d S rP   rQ   )r5   rK   r"   rS   rT   rD   r>   Zrender_heightsrU   rV   rW   Zchild_heightr)   r)   r*   rL   �   s    
zColumnSplitter.divideNrX   r)   r)   r)   r*   rY   y   s    þrY   c                
   @   s¤  e Zd ZdZeedœZd8ddddddœee ee	 ee
 e
e
eddœdd	„Zed
œdd„Zeed
œdd„ƒZeed  d
œdd„ƒZeed
œdd„ƒZe	ed  dœdd„Ze	d dœdd„Zedd
œdd„ƒZddœed ef eee	f ddœdd„Zed ef dd œd!d"„Zed ef dd œd#d$„Zed ef dd œd%d&„Zdd
œd'd(„Zedd)œd*d+„Zd,e	dd-œd.d/„Z e
e
e!d0œd1d2„Z"e#e$ed3œd4d5„Z%e#e$e&d3œd6d7„Z'dS )9r+   aB  A renderable to divide a fixed height in to rows or columns.

    Args:
        renderable (RenderableType, optional): Renderable content, or None for placeholder. Defaults to None.
        name (str, optional): Optional identifier for Layout. Defaults to None.
        size (int, optional): Optional fixed size of layout. Defaults to None.
        minimum_size (int, optional): Minimum size of layout. Defaults to 1.
        ratio (int, optional): Optional ratio for flexible layout. Defaults to 1.
        visible (bool, optional): Visibility of layout. Defaults to True.
    )rO   rZ   Nr   T)rA   r@   Úminimum_sizeÚratioÚvisible)Ú
renderablerA   r@   r[   r\   r]   r3   c                C   sR   |p
t | ƒ| _|| _|| _|| _|| _|| _| jd ƒ | _g | _	i | _
tƒ | _d S )NrZ   )r/   Ú_renderabler@   r[   r\   rA   r]   Ú	splittersÚsplitterÚ	_childrenÚ_render_mapr   Ú_lock)r5   r^   rA   r@   r[   r\   r]   r)   r)   r*   r6   œ   s    
zLayout.__init__rG   c                 c   s<   d| j d fV  d| jd fV  d| jdfV  d| jdfV  d S )NrA   r@   r[   r   r\   )rA   r@   r[   r\   rH   r)   r)   r*   Ú__rich_repr__±   s    zLayout.__rich_repr__c                 C   s   | j r
| S | jS )zLayout renderable.)rb   r_   rH   r)   r)   r*   r^   ·   s    zLayout.renderablec                 C   s   dd„ | j D ƒS )zGets (visible) layout children.c                 S   s   g | ]}|j r|‘qS r)   )r]   )Ú.0rW   r)   r)   r*   Ú
<listcomp>¿   s      z#Layout.children.<locals>.<listcomp>©rb   rH   r)   r)   r*   rK   ¼   s    zLayout.childrenc                 C   s   | j S )zGet a map of the last render.)rc   rH   r)   r)   r*   ÚmapÁ   s    z
Layout.map)rA   r3   c                 C   s8   | j |kr| S | jD ]}| |¡}|dk	r|  S qdS )zÐGet a named layout, or None if it doesn't exist.

        Args:
            name (str): Name of layout.

        Returns:
            Optional[Layout]: Layout instance or None if no layout was found.
        N)rA   rb   Úget)r5   rA   rW   Znamed_layoutr)   r)   r*   rj   Æ   s    	



z
Layout.getc                 C   s$   |   |¡}|d kr td|›�ƒ‚|S )NzNo layout with name )rj   ÚKeyError)r5   rA   r1   r)   r)   r*   Ú__getitem__Ø   s    
zLayout.__getitem__r    c                    s€   ddl m‰  ddlm‰ ddlm} dˆdœ‡ ‡fdd„‰| }|ˆ|ƒd	|jj› �d
d�}ddddœ‡‡fdd„‰ˆ|| ƒ |S )z/Get a tree renderable to show layout structure.r   )ÚStyled)ÚTabler   r+   )r1   r3   c                    sF   | j  ¡ }ˆjdd�}| jr$t| ƒnˆ t| ƒdƒ}| ||¡ |}|S )N)r   r   r   r   )ÚpaddingZdim)ra   rI   Zgridr]   r   Zadd_row)r1   ZiconÚtableÚtextZ_summary)rm   rn   r)   r*   Úsummaryå   s    
ÿzLayout.tree.<locals>.summaryúlayout.tree.T)Úguide_styleZ	highlightr    N)Útreer1   r3   c                    s2   |j D ]&}ˆ | jˆ|ƒd|jj› �d�|ƒ qd S )Nrs   )rt   )rb   Úaddra   rA   )ru   r1   rW   )Úrecurserr   r)   r*   rw   ø   s    
þûzLayout.tree.<locals>.recurse)Zpip._vendor.rich.styledrm   Zpip._vendor.rich.tablern   Úpip._vendor.rich.treer    ra   rA   )r5   r    r1   ru   r)   )rm   rn   rw   rr   r*   ru   Þ   s    ý

zLayout.treerZ   )ra   )Úlayoutsra   r3   c                G   sd   dd„ |D ƒ}z t |tƒr|n
| j| ƒ | _W n" tk
rP   td|›�ƒ‚Y nX || jdd…< dS )zîSplit the layout in to multiple sub-layouts.

        Args:
            *layouts (Layout): Positional arguments should be (sub) Layout instances.
            splitter (Union[Splitter, str]): Splitter instance or name of splitter.
        c                 S   s"   g | ]}t |tƒr|nt|ƒ‘qS r)   ©Ú
isinstancer+   ©rf   r1   r)   r)   r*   rg     s   ÿz Layout.split.<locals>.<listcomp>zNo splitter called N)r{   rF   r`   ra   rk   r.   rb   )r5   ra   ry   Ú_layoutsr)   r)   r*   Úsplit  s    þÿ
ýzLayout.split)ry   r3   c                 G   s   dd„ |D ƒ}| j  |¡ dS )z»Add a new layout(s) to existing split.

        Args:
            *layouts (Union[Layout, RenderableType]): Positional arguments should be renderables or (sub) Layout instances.

        c                 s   s$   | ]}t |tƒr|nt|ƒV  qd S r4   rz   r|   r)   r)   r*   Ú	<genexpr>%  s   ÿz#Layout.add_split.<locals>.<genexpr>N)rb   Úextend)r5   ry   r}   r)   r)   r*   Ú	add_split  s    þzLayout.add_splitc                 G   s   | j |ddiŽ dS )z¢Split the layout in to a row (layouts side by side).

        Args:
            *layouts (Layout): Positional arguments should be (sub) Layout instances.
        ra   rO   N©r~   ©r5   ry   r)   r)   r*   Ú	split_row+  s    zLayout.split_rowc                 G   s   | j |ddiŽ dS )zµSplit the layout in to a column (layouts stacked on top of each other).

        Args:
            *layouts (Layout): Positional arguments should be (sub) Layout instances.
        ra   rZ   Nr‚   rƒ   r)   r)   r*   Úsplit_column3  s    zLayout.split_columnc                 C   s   | j dd…= dS )zReset splits to initial state.Nrh   rH   r)   r)   r*   Úunsplit;  s    zLayout.unsplit)r^   r3   c              	   C   s   | j � || _W 5 Q R X dS )zjUpdate renderable.

        Args:
            renderable (RenderableType): New renderable object.
        N)rd   r_   )r5   r^   r)   r)   r*   Úupdate?  s    zLayout.updater   )r8   Úlayout_namer3   c              	   C   sl   | j �\ | | }| j| \}}|\}}}}	| ||j ||	¡¡}
t||
ƒ| j|< | |
||¡ W 5 Q R X dS )z¬Refresh a sub-layout.

        Args:
            console (Console): Console instance where Layout is to be rendered.
            layout_name (str): Name of layout.
        N)rd   rc   Úrender_linesr9   Úupdate_dimensionsr!   Zupdate_screen_lines)r5   r8   rˆ   r1   r"   Ú_linesrS   rT   rD   r>   Úlinesr)   r)   r*   Úrefresh_screenH  s     ÿzLayout.refresh_screen)rD   r>   r3   c                 C   sŠ   | t dd||ƒfg}|j}|j}g }|j}|rl||ƒ ƒ |d \}}	|j}
|
r*|j |
|	¡D ]}||ƒ q\q*dd„ t|tdƒd�D ƒ}|S )z,Create a dict that maps layout on to Region.r   éÿÿÿÿc                 S   s   i | ]\}}||“qS r)   r)   ©rf   r1   r"   r)   r)   r*   Ú
<dictcomp>h  s   ÿ z+Layout._make_region_map.<locals>.<dictcomp>r   )Úkey)r   ÚappendÚpoprK   ra   rL   Úsortedr   )r5   rD   r>   ÚstackÚpushr“   Úlayout_regionsZappend_layout_regionr1   r"   rK   Zchild_and_regionÚ
region_mapr)   r)   r*   Ú_make_region_mapY  s     
þzLayout._make_region_mapr7   c                 C   sv   |j }|jp|j}|  ||¡}dd„ | ¡ D ƒ}i }|j}|j}	|D ],\}
}||
j|	|j|jƒƒ}t||ƒ||
< qD|S )z÷Render the sub_layouts.

        Args:
            console (Console): Console instance.
            options (ConsoleOptions): Console options.

        Returns:
            RenderMap: A dict that maps Layout on to a tuple of Region, lines
        c                 S   s   g | ]\}}|j s||f‘qS r)   )rK   r�   r)   r)   r*   rg   {  s   þz!Layout.render.<locals>.<listcomp>)	r?   r>   r™   Úitemsr‰   rŠ   r^   rD   r!   )r5   r8   r9   Zrender_widthZrender_heightr˜   r—   Ú
render_mapr‰   rŠ   r1   r"   rŒ   r)   r)   r*   r#   n  s     
þ ÿzLayout.renderc              	   c   sÈ   | j �¸ |jp|j}|jp|j}|  || ||¡¡}|| _dd„ t|ƒD ƒ}t}| 	¡ D ]>\}}	|\}
}}}t
||||| ƒ|	ƒD ]\}}| |¡ q‚qXt ¡ }|D ]}|E d H  |V  q¤W 5 Q R X d S )Nc                 S   s   g | ]}g ‘qS r)   r)   )rf   Ú_r)   r)   r*   rg   “  s     z+Layout.__rich_console__.<locals>.<listcomp>)rd   r?   rD   r>   r#   rŠ   rc   Úranger   ÚvaluesrR   r€   r   Úline)r5   r8   r9   rD   r>   r›   Zlayout_linesÚ_islicer"   rŒ   Ú_xrT   Z_layout_widthZlayout_heightrO   rŸ   Únew_lineZ
layout_rowr)   r)   r*   rE   ‹  s$     ÿ
zLayout.__rich_console__)N)(r$   r%   r&   r'   rN   rY   r`   r   r   rM   ÚintÚboolr6   r   re   Úpropertyr^   r
   rK   Ú	RenderMapri   rj   rl   ru   r   rF   r~   r�   r„   r…   r†   r‡   r�   Ú	RegionMapr™   r   r   r#   r   rE   r)   r)   r)   r*   r+   �   s^   
 þø÷)ý

ü	 þÚ__main__)r   Úheaderé   )rA   r@   Úmain)r\   rA   é
   Úfooter)r@   rA   Zside)rA   Úbodyé   )rA   r\   ÚcontentÚs2Útopr<   ZbottomZleft1Zleft2ZfooN)@Úabcr   r   Ú	itertoolsr   Úoperatorr   Ú	threadingr   Útypingr   r   r	   r
   r   r   r   r   r   Z_ratior   Zalignr   r8   r   r   r   r   rC   r   Zpanelr   Úprettyr   r"   r   Úreprr   r   Úsegmentr   r2   r   rx   r    r!   r§   r¦   Ú	Exceptionr,   r.   r/   rF   rN   rY   r+   r$   Zpip._vendor.rich.consoler1   r…   r„   ru   r‡   Úprintr)   r)   r)   r*   Ú<module>   s`   ,  



ý  ÿ 