a
    )ñ»jŒd  ã                   @   sæ  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
mZmZmZmZmZmZmZmZ ddlmZmZmZmZmZ ddlmZmZ dd	lmZ e	r°dd
lmZm Z m!Z! G dd„ deƒZ"eee" ee"ee#e$f f ee"e#e#f f Z%eƒ G dd„ deƒƒZ&G dd„ dƒZ'G dd„ dƒZ(e)dk�râd dl*mZ d dl+m,Z, d dl-m.Z. dZ/e. 0d¡Z1eƒ Ze 2d¡ e 3d¡ e 3d¡ e 3e,e/ddd�¡ e 3¡  e 3d¡ e4e 5e1¡ƒZ6e 3e6¡ e 3¡  e 3d ¡ e 3e1¡ e 3d!¡ d"S )#é    )ÚIntEnum)Ú	lru_cache)Úfilterfalse)Ú
attrgetter)
ÚTYPE_CHECKINGÚDictÚIterableÚListÚ
NamedTupleÚOptionalÚSequenceÚTupleÚTypeÚUnioné   )Ú_is_single_cell_widthsÚcached_cell_lenÚcell_lenÚget_character_cell_sizeÚset_cell_size)ÚResultÚ	rich_repr)ÚStyle)ÚConsoleÚConsoleOptionsÚRenderResultc                   @   sP   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdS )ÚControlTypezDNon-printable control codes which typically translate to ANSI codes.r   é   é   é   é   é   é   é   é	   é
   é   é   é   é   é   é   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚBELLÚCARRIAGE_RETURNÚHOMEÚCLEARÚSHOW_CURSORÚHIDE_CURSORÚENABLE_ALT_SCREENÚDISABLE_ALT_SCREENÚ	CURSOR_UPÚCURSOR_DOWNÚCURSOR_FORWARDÚCURSOR_BACKWARDÚCURSOR_MOVE_TO_COLUMNÚCURSOR_MOVE_TOÚERASE_IN_LINEÚSET_WINDOW_TITLE© r@   r@   ú2E:\soccer\ml_env\Lib\site-packages\rich/segment.pyr       s"   r   c                   @   sR  e Zd ZU dZeed< dZee ed< dZ	ee
e  ed< eedœdd„ƒZedœd	d
„Zedœdd„Zeedœdd„ƒZeedƒd eed dœdd„ƒƒZeed dœdd„Zed dœdd„ƒZedGed  ee ee ed  dœdd„ƒZedHed  eed  dœdd„ƒZeed  eed   d œd!d"„ƒZeed  eeed  ef  d œd#d$„ƒZedIed  eee eeeed   d&œd'd(„ƒZ edJed  eee eed  d)œd*d+„ƒZ!eed  ed,œd-d.„ƒZ"eeed   eeef d/œd0d1„ƒZ#edKeed   eee ee eeed   d2œd3d4„ƒZ$edLe%d  eed   eeeeeed   d5œd6d7„ƒZ&edMe%d  eed   eeeeeed   d5œd8d9„ƒZ'edNe%d  eed   eeeeeed   d5œd:d;„ƒZ(eed  ed  d œd<d=„ƒZ)eed  ed  d œd>d?„ƒZ*eed  ed  d œd@dA„ƒZ+eed  ed  d œdBdC„ƒZ,eed  ee eed   dDœdEdF„ƒZ-dS )OÚSegmentaÜ  A piece of text with associated style. Segments are produced by the Console render process and
    are ultimately converted in to strings to be written to the terminal.

    Args:
        text (str): A piece of text.
        style (:class:`~rich.style.Style`, optional): An optional style to apply to the text.
        control (Tuple[ControlCode], optional): Optional sequence of control codes.

    Attributes:
        cell_length (int): The cell length of this Segment.
    ÚtextNÚstyleÚcontrol)Úreturnc                 C   s   | \}}}|rdS t |ƒS )zzThe number of terminal cells required to display self.text.

        Returns:
            int: A number of cells.
        r   )r   )ÚselfrC   Ú_stylerE   r@   r@   rA   Úcell_lengthN   s    
zSegment.cell_lengthc                 c   s:   | j V  | jd u r&| jd ur6| jV  n| jV  | jV  d S ©N)rC   rE   rD   ©rG   r@   r@   rA   Ú__rich_repr__X   s    


zSegment.__rich_repr__c                 C   s
   t | jƒS )z#Check if the segment contains text.)ÚboolrC   rK   r@   r@   rA   Ú__bool__a   s    zSegment.__bool__c                 C   s
   | j duS )z,Check if the segment contains control codes.N)rE   rK   r@   r@   rA   Ú
is_controle   s    zSegment.is_controli @  )rB   rB   )ÚsegmentÚcutrF   c                 C   sF  |\}}}t }|j}||kr,||d||ƒfS t}t|| t|ƒ ƒ}	|d|	… }
t|
ƒ}|| }|s„||
||ƒ|||	d… ||ƒfS |dkrÐ|||	 ƒdkrÐ||d|	… d ||ƒ|d||	d d…  ||ƒfS |dk�r$|||	d  ƒdk�r$||d|	d … d ||ƒ|d||	d…  ||ƒfS ||k �r8|	d7 }	qD|	d8 }	qDdS )a\  Split a segment in to two at a given cell position.

        Note that splitting a double-width character, may result in that character turning
        into two spaces.

        Args:
            segment (Segment): A segment to split.
            cut (int): A cell position to cut on.

        Returns:
            A tuple of two segments.
        Ú Néÿÿÿÿr   ú r   )rB   rI   r   ÚintÚlenr   )ÚclsrP   rQ   rC   rD   rE   Ú_SegmentrI   Ú	cell_sizeÚposÚbeforeÚcell_posÚout_byr@   r@   rA   Ú_split_cellsj   s2    

þþ þ

zSegment._split_cells)rQ   rF   c                 C   sn   | \}}}|dksJ ‚t |ƒrb|t|ƒkr:| td||ƒfS t|d|… ||ƒt||d… ||ƒfS |  | |¡S )ax  Split segment in to two segments at the specified column.

        If the cut point falls in the middle of a 2-cell wide character then it is replaced
        by two spaces, to preserve the display width of the parent segment.

        Args:
            cut (int): Offset within the segment to cut.

        Returns:
            Tuple[Segment, Segment]: Two segments.
        r   rR   N)r   rV   rB   r^   )rG   rQ   rC   rD   rE   r@   r@   rA   Úsplit_cells›   s    
þzSegment.split_cellsc                 C   s   | dƒS )zMake a new line segment.Ú
r@   )rW   r@   r@   rA   Úlineµ   s    zSegment.line)ÚsegmentsrD   Ú
post_stylerF   c                    s>   |}|r"|j ‰ ‡ ‡fdd„|D ƒ}ˆr:‡‡fdd„|D ƒ}|S )a  Apply style(s) to an iterable of segments.

        Returns an iterable of segments where the style is replaced by ``style + segment.style + post_style``.

        Args:
            segments (Iterable[Segment]): Segments to process.
            style (Style, optional): Base style. Defaults to None.
            post_style (Style, optional): Style to apply on top of segment style. Defaults to None.

        Returns:
            Iterable[Segments]: A new iterable of segments (possibly the same iterable).
        c                 3   s,   | ]$\}}}ˆ||rd nˆ |ƒ|ƒV  qd S rJ   r@   ©Ú.0rC   rH   rE   )ÚapplyrW   r@   rA   Ú	<genexpr>Ð   s   ÿz&Segment.apply_style.<locals>.<genexpr>c                 3   s4   | ],\}}}ˆ ||rd n|r$|ˆ nˆ|ƒV  qd S rJ   r@   rd   )rW   rc   r@   rA   rg   Õ   s   
÷ÿù)Ú__add__)rW   rb   rD   rc   Úresult_segmentsr@   )rf   rW   rc   rA   Úapply_styleº   s    þ
özSegment.apply_styleF)rb   rO   rF   c                 C   s$   |rt tdƒ|ƒS ttdƒ|ƒS dS )a2  Filter segments by ``is_control`` attribute.

        Args:
            segments (Iterable[Segment]): An iterable of Segment instances.
            is_control (bool, optional): is_control flag to match in search.

        Returns:
            Iterable[Segment]: And iterable of Segment instances.

        rE   N)Úfilterr   r   )rW   rb   rO   r@   r@   rA   Úfilter_controlã   s    zSegment.filter_control)rb   rF   c           
      c   s‚   g }|j }|D ]d}d|jv rj|jsj|\}}}|rr| d¡\}}	}|rR|| ||ƒƒ |	r,|V  g }|j }q,q||ƒ q|r~|V  dS )a   Split a sequence of segments in to a list of lines.

        Args:
            segments (Iterable[Segment]): Segments potentially containing line feeds.

        Yields:
            Iterable[List[Segment]]: Iterable of segment lists, one per line.
        r`   N©ÚappendrC   rE   Ú	partition©
rW   rb   ra   rn   rP   rC   rD   Ú_Ú_textÚnew_liner@   r@   rA   Úsplit_linesö   s     



zSegment.split_linesc           
      c   sŠ   g }|j }|D ]h}d|jv rn|jsn|\}}}|rv| d¡\}}	}|rR|| ||ƒƒ |	r,|dfV  g }|j }q,q||ƒ q|r†|dfV  dS )a2  Split a sequence of segments in to a list of lines and a boolean to indicate if there was a new line.

        Args:
            segments (Iterable[Segment]): Segments potentially containing line feeds.

        Yields:
            Iterable[List[Segment]]: Iterable of segment lists, one per line.
        r`   TFNrm   rp   r@   r@   rA   Úsplit_lines_terminator  s     



zSegment.split_lines_terminatorT)rb   ÚlengthrD   ÚpadÚinclude_new_linesrF   c                 c   s¸   g }|j }| j}| dƒ}	|D ]€}
d|
jv r”|
js”|
\}}}|rœ| d¡\}}}|r`|| ||ƒƒ |r:|||||d�}|r‚|  |	¡ |V  | ¡  q:q||
ƒ q|r´|||||d�V  dS )aò  Split segments in to lines, and crop lines greater than a given length.

        Args:
            segments (Iterable[Segment]): An iterable of segments, probably
                generated from console.render.
            length (int): Desired line length.
            style (Style, optional): Style to use for any padding.
            pad (bool): Enable padding of lines that are less than `length`.

        Returns:
            Iterable[List[Segment]]: An iterable of lines of segments.
        r`   )rD   rw   N)rn   Úadjust_line_lengthrC   rE   ro   Úclear)rW   rb   rv   rD   rw   rx   ra   rn   ry   Únew_line_segmentrP   rC   Úsegment_stylerq   rr   rs   Úcropped_liner@   r@   rA   Úsplit_and_crop_lines2  s,    
ÿ

zSegment.split_and_crop_lines)ra   rv   rD   rw   rF   c                 C   sÌ   t dd„ |D ƒƒ}||k rF|r8|| d||  |ƒg }qÈ|dd… }n‚||kr¼g }|j}d}|D ]X}|j}	||	 |k s||jrŽ||ƒ ||	7 }q`|\}
}}t|
|| ƒ}
|| |
|ƒƒ  qÈq`n|dd… }|S )a  Adjust a line to a given width (cropping or padding as required).

        Args:
            segments (Iterable[Segment]): A list of segments in a single line.
            length (int): The desired width of the line.
            style (Style, optional): The style of padding if used (space on the end). Defaults to None.
            pad (bool, optional): Pad lines with spaces if they are shorter than `length`. Defaults to True.

        Returns:
            List[Segment]: A line of segments with the desired length.
        c                 s   s   | ]}|j V  qd S rJ   )rI   )re   rP   r@   r@   rA   rg   t  ó    z-Segment.adjust_line_length.<locals>.<genexpr>rT   Nr   )Úsumrn   rI   rE   r   )rW   ra   rv   rD   rw   Úline_lengthrs   rn   rP   Úsegment_lengthrC   r|   rq   r@   r@   rA   ry   a  s(    

zSegment.adjust_line_length)ra   rF   c                    s   t ‰ t‡ fdd„|D ƒƒS )zÜGet the length of list of segments.

        Args:
            line (List[Segment]): A line encoded as a list of Segments (assumes no '\\n' characters),

        Returns:
            int: The length of the line.
        c                 3   s    | ]\}}}|sˆ |ƒV  qd S rJ   r@   )re   rC   rD   rE   ©Ú	_cell_lenr@   rA   rg   ™  r   z*Segment.get_line_length.<locals>.<genexpr>)r   r€   )rW   ra   r@   rƒ   rA   Úget_line_lengthŽ  s    
zSegment.get_line_length)ÚlinesrF   c                    s0   | j ‰ |r t‡ fdd„|D ƒƒnd}|t|ƒfS )zîGet the shape (enclosing rectangle) of a list of lines.

        Args:
            lines (List[List[Segment]]): A list of lines (no '\\n' characters).

        Returns:
            Tuple[int, int]: Width and height in characters.
        c                 3   s   | ]}ˆ |ƒV  qd S rJ   r@   ©re   ra   ©r…   r@   rA   rg   ¦  r   z$Segment.get_shape.<locals>.<genexpr>r   )r…   ÚmaxrV   )rW   r†   Ú	max_widthr@   rˆ   rA   Ú	get_shape›  s    
zSegment.get_shape)r†   ÚwidthÚheightrD   Ú	new_linesrF   c           	         sŒ   |p
t |ƒ}|r$| dˆ d ˆƒgn| dˆ ˆƒg}| j‰ |d|… }‡ ‡‡fdd„|D ƒ|dd…< t |ƒ|k rˆ| |g|t |ƒ  ¡ |S )aÞ  Set the shape of a list of lines (enclosing rectangle).

        Args:
            lines (List[List[Segment]]): A list of lines.
            width (int): Desired width.
            height (int, optional): Desired height or None for no change.
            style (Style, optional): Style of any padding added.
            new_lines (bool, optional): Padded lines should include "
". Defaults to False.

        Returns:
            List[List[Segment]]: New list of lines.
        rT   r`   Nc                    s   g | ]}ˆ |ˆˆd �‘qS ))rD   r@   r‡   ©ry   rD   rŒ   r@   rA   Ú
<listcomp>Æ  s   z%Segment.set_shape.<locals>.<listcomp>)rV   ry   Úextend)	rW   r†   rŒ   r�   rD   rŽ   Ú_heightÚblankÚshaped_linesr@   r�   rA   Ú	set_shape©  s    &ÿÿzSegment.set_shape)rW   r†   rŒ   r�   rD   rŽ   rF   c                 C   s`   |t |ƒ }|s|dd… S |d|… }|r>| d| d |ƒn| d| |ƒ}||gg|  }|S )aÚ  Aligns lines to top (adds extra lines to bottom as required).

        Args:
            lines (List[List[Segment]]): A list of lines.
            width (int): Desired width.
            height (int, optional): Desired height or None for no change.
            style (Style): Style of any padding added.
            new_lines (bool, optional): Padded lines should include "
". Defaults to False.

        Returns:
            List[List[Segment]]: New list of lines.
        NrT   r`   ©rV   ©rW   r†   rŒ   r�   rD   rŽ   Úextra_linesr“   r@   r@   rA   Ú	align_topÍ  s    $zSegment.align_topc                 C   s`   |t |ƒ }|s|dd… S |d|… }|r>| d| d |ƒn| d| |ƒ}|gg| | }|S )aì  Aligns render to bottom (adds extra lines above as required).

        Args:
            lines (List[List[Segment]]): A list of lines.
            width (int): Desired width.
            height (int, optional): Desired height or None for no change.
            style (Style): Style of any padding added. Defaults to None.
            new_lines (bool, optional): Padded lines should include "
". Defaults to False.

        Returns:
            List[List[Segment]]: New list of lines.
        NrT   r`   r–   r—   r@   r@   rA   Úalign_bottomê  s    $zSegment.align_bottomc           
      C   s|   |t |ƒ }|s|dd… S |d|… }|r>| d| d |ƒn| d| |ƒ}|d }|| }	|gg| | |gg|	  }|S )aæ  Aligns lines to middle (adds extra lines to above and below as required).

        Args:
            lines (List[List[Segment]]): A list of lines.
            width (int): Desired width.
            height (int, optional): Desired height or None for no change.
            style (Style): Style of any padding added.
            new_lines (bool, optional): Padded lines should include "
". Defaults to False.

        Returns:
            List[List[Segment]]: New list of lines.
        NrT   r`   r   r–   )
rW   r†   rŒ   r�   rD   rŽ   r˜   r“   Ú	top_linesÚbottom_linesr@   r@   rA   Úalign_middle  s    $zSegment.align_middlec                 c   st   t |ƒ}zt|ƒ}W n ty(   Y dS 0 t}|D ]6}|j|jkr^|js^||j|j |jƒ}q2|V  |}q2|V  dS )a)  Simplify an iterable of segments by combining contiguous segments with the same style.

        Args:
            segments (Iterable[Segment]): An iterable of segments.

        Returns:
            Iterable[Segment]: A possibly smaller iterable of segments that will render the same way.
        N)ÚiterÚnextÚStopIterationrB   rD   rE   rC   )rW   rb   Úiter_segmentsÚlast_segmentrX   rP   r@   r@   rA   Úsimplify&  s    
ÿzSegment.simplifyc                 c   sJ   |D ]@}|j s|jdu r |V  q|\}}}| ||r<| d¡ndƒV  qdS )zÆRemove all links from an iterable of styles.

        Args:
            segments (Iterable[Segment]): An iterable segments.

        Yields:
            Segment: Segments with link removed.
        N)rE   rD   Úupdate_link)rW   rb   rP   rC   rD   Ú_controlr@   r@   rA   Ústrip_linksA  s
    

zSegment.strip_linksc                 c   s"   |D ]\}}}| |d|ƒV  qdS )zÔRemove all styles from an iterable of segments.

        Args:
            segments (Iterable[Segment]): An iterable segments.

        Yields:
            Segment: Segments with styles replace with None
        Nr@   )rW   rb   rC   rH   rE   r@   r@   rA   Ústrip_stylesR  s    
zSegment.strip_stylesc                 c   sZ   i }|D ]L\}}}|rF|  |¡}|du r6|j}|||< | |||ƒV  q| |d|ƒV  qdS )zËRemove all color from an iterable of segments.

        Args:
            segments (Iterable[Segment]): An iterable segments.

        Yields:
            Segment: Segments with colorless style.
        N)ÚgetÚwithout_color)rW   rb   ÚcacherC   rD   rE   Úcolorless_styler@   r@   rA   Úremove_color_  s    
zSegment.remove_color)rb   ÚcutsrF   c                 c   sD  g }|j }t|ƒ}t|dƒ}|dkr(dS |dkr2q:g V  qd}|j}|j}	t}
|D ]ä}|\}}}|rR|rl|n
||
|ƒ }||k rŽ||ƒ |}qR||krØ||ƒ |	ƒ V  |ƒ  |}t|dƒ}|dkrÔ|rÎ|	ƒ V   dS qRn6| || ¡\}}|\}}}||ƒ |	ƒ V  |ƒ  |}t|dƒ}|dkr`|�r.|	ƒ V   dS q`qR|	ƒ V  dS )zâDivides an iterable of segments in to portions.

        Args:
            cuts (Iterable[int]): Cell positions where to divide.

        Yields:
            [Iterable[List[Segment]]]: An iterable of Segments in List.
        rS   Nr   )rn   rž   rŸ   rz   Úcopyr   r_   )rW   rb   r­   Úsplit_segmentsÚadd_segmentÚ	iter_cutsrQ   rZ   Úsegments_clearÚsegments_copyr„   rP   rC   rH   rE   Úend_posr[   r@   r@   rA   Údivideu  sX    





zSegment.divide)NN)F)NTT)NT)NNF)F)F)F).r,   r-   r.   r/   ÚstrÚ__annotations__rD   r   r   rE   r   ÚControlCodeÚpropertyrU   rI   r   rL   rM   rN   rO   Úclassmethodr   r   r^   r_   ra   r   rj   rl   r	   rt   ru   r~   ry   r…   r‹   r•   r   r™   rš   r�   r£   r¦   r§   r¬   rµ   r@   r@   r@   rA   rB   <   sð   
		/  üû( ÿþþ   ú
ù.  ûú,"   ú

ù# ú

ù ú

ù ú

ù
þrB   c                   @   s:   e Zd ZdZdee eddœdd„Zddd	d
œdd„ZdS )ÚSegmentsa=  A simple renderable to render an iterable of segments. This class may be useful if
    you want to print segments outside of a __rich_console__ method.

    Args:
        segments (Iterable[Segment]): An iterable of segments.
        new_lines (bool, optional): Add new lines between segments. Defaults to False.
    FN)rb   rŽ   rF   c                 C   s   t |ƒ| _|| _d S rJ   )Úlistrb   rŽ   )rG   rb   rŽ   r@   r@   rA   Ú__init__Ä  s    
zSegments.__init__r   r   r   ©ÚconsoleÚoptionsrF   c                 c   s8   | j r(t ¡ }| jD ]}|V  |V  qn| jE d H  d S rJ   )rŽ   rB   ra   rb   )rG   r¿   rÀ   ra   rP   r@   r@   rA   Ú__rich_console__È  s    

zSegments.__rich_console__)F)	r,   r-   r.   r/   r   rB   rM   r½   rÁ   r@   r@   r@   rA   r»   »  s
   þr»   c                   @   s:   e Zd Zdeee  eddœdd„Zdddd	œd
d„ZdS )ÚSegmentLinesFN)r†   rŽ   rF   c                 C   s   t |ƒ| _|| _dS )a=  A simple renderable containing a number of lines of segments. May be used as an intermediate
        in rendering process.

        Args:
            lines (Iterable[List[Segment]]): Lists of segments forming lines.
            new_lines (bool, optional): Insert new lines after each line. Defaults to False.
        N)r¼   r†   rŽ   )rG   r†   rŽ   r@   r@   rA   r½   Õ  s    
zSegmentLines.__init__r   r   r   r¾   c                 c   sF   | j r,t ¡ }| jD ]}|E d H  |V  qn| jD ]}|E d H  q2d S rJ   )rŽ   rB   ra   r†   )rG   r¿   rÀ   rs   ra   r@   r@   rA   rÁ   à  s    



zSegmentLines.__rich_console__)F)	r,   r-   r.   r   r	   rB   rM   r½   rÁ   r@   r@   r@   rA   rÂ   Ô  s   þrÂ   Ú__main__)r   )ÚSyntax)ÚTextz‚from rich.console import Console
console = Console()
text = Text.from_markup("Hello, [bold magenta]World[/]!")
console.print(text)zHello, [bold magenta]World[/]!zrich.Segmentz]A Segment is the last step in the Rich render process before generating text with ANSI codes.z
Consider the following code:
ÚpythonT)Úline_numberszRWhen you call [b]print()[/b], Rich [i]renders[/i] the object in to the following:
zAThe Segments are then processed to produce the following output:
zS
You will only need to know this if you are implementing your own Rich renderables.N)7Úenumr   Ú	functoolsr   Ú	itertoolsr   Úoperatorr   Útypingr   r   r   r	   r
   r   r   r   r   r   Úcellsr   r   r   r   r   Úreprr   r   rD   r   r¿   r   r   r   r   rU   r¶   r¸   rB   r»   rÂ   r,   Zrich.consoleZrich.syntaxrÄ   Z	rich.textrÅ   ÚcodeÚfrom_markuprC   ÚruleÚprintr¼   ÚrenderÚ	fragmentsr@   r@   r@   rA   Ú<module>   sd   0þÿ     


ÿ
ÿ


ÿ