o
    Ëý°jl]  ã                   @   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	 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 dd	lmZmZ dd
lmZ er\ddlm Z m!Z!m"Z" edƒZ#G dd„ deƒZ$eee$ ee$e%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úe*dkrüd dl+m Z  d dl,m-Z- d dl.m/Z/ dZ0e/ 1d¡Z2e ƒ Ze 3d¡ e 4d¡ e 4d¡ e 4e-e0ddd �¡ e 4¡  e 4d!¡ e5e 6e2¡ƒZ7e 4e7¡ e 4¡  e 4d"¡ e 4e2¡ e 4d#¡ d$S d$S d$S )%é    )ÚIntEnum)Ú	lru_cache)Úfilterfalse)Ú	getLogger)Ú
attrgetter)
ÚTYPE_CHECKINGÚDictÚIterableÚListÚ
NamedTupleÚOptionalÚSequenceÚTupleÚTypeÚUnioné   )Ú_is_single_cell_widthsÚcell_lenÚget_character_cell_sizeÚset_cell_size)ÚResultÚ	rich_repr)ÚStyle)ÚConsoleÚConsoleOptionsÚRenderResultÚrichc                   @   sL   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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© r?   r?   ú‹/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pip/_vendor/rich/segment.pyr   "   s"    r   c                   @   s®  e Zd ZU dZdZeed< 	 dZee	 ed< 	 dZ
eee  ed< 	 defdd	„Zdefd
d„Zedefdd„ƒZedefdd„ƒZeedƒdd deded fdd„ƒƒZdeded fdd„ZedIdd„ƒZe		dJded  dee	 dee	 ded  fdd„ƒZe	dKded  deded  fd d!„ƒZeded  deed   fd"d#„ƒZe		$	$dLded  d%edee	 d&ed'edeed   fd(d)„ƒZ e		$dMd*ed  d%edee	 d&eded  f
d+d,„ƒZ!ed*ed  defd-d.„ƒZ"ed/eed   deeef fd0d1„ƒZ#e			dNd/eed   d2ed3ee dee	 d4edeed   fd5d6„ƒZ$e	dKd7e%d  d/eed   d2ed3ede	d4edeed   fd8d9„ƒZ&e	dKd7e%d  d/eed   d2ed3ede	d4edeed   fd:d;„ƒZ'e	dKd7e%d  d/eed   d2ed3ede	d4edeed   fd<d=„ƒZ(eded  ded  fd>d?„ƒZ)eded  ded  fd@dA„ƒZ*eded  ded  fdBdC„ƒZ+eded  ded  fdDdE„ƒZ,eded  dFee deed   fdGdH„ƒ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.
    Ú ÚtextNÚstyleÚcontrolÚreturnc                 c   sB   � | j V  | jd u r| jd ur| jV  d S d S | jV  | jV  d S ©N)rC   rE   rD   ©Úselfr?   r?   r@   Ú__rich_repr__M   s   €

ÿzSegment.__rich_repr__c                 C   s
   t | jƒS )z#Check if the segment contains text.)ÚboolrC   rH   r?   r?   r@   Ú__bool__V   s   
zSegment.__bool__c                 C   s   | j rdS t| jƒS )zGet cell length of segment.r   )rE   r   rC   rH   r?   r?   r@   Úcell_lengthZ   s   zSegment.cell_lengthc                 C   s
   | j duS )z,Check if the segment contains control codes.N)rE   rH   r?   r?   r@   Ú
is_control_   s   
zSegment.is_controli @  ÚsegmentÚcut)rA   rA   c                 C   s,  |\}}}t }|j}||kr||d||ƒfS t}t|| t|ƒ ƒ}	|d |	… }
t|
ƒ}||kr@||
||ƒ|||	d … ||ƒfS |	t|ƒk r”||	 }|	d7 }	|||ƒ7 }|d |	… }
||krn||
||ƒ|||	d … ||ƒfS ||krŒ||
d |	d … d ||ƒ|d||	d …  ||ƒfS |	t|ƒk sFd S d S )NrB   r   ú )rA   rM   r   ÚintÚlenr   )ÚclsrO   rP   rC   rD   rE   Ú_SegmentrM   Ú	cell_sizeÚposÚbeforeÚcell_posÚcharr?   r?   r@   Ú_split_cellsd   s6   

þ
þþõzSegment._split_cellsc                 C   sb   | \}}}t |ƒr+|t|ƒkr| td||ƒfS t|d|… ||ƒt||d… ||ƒfS |  | |¡S )a0  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.

        Returns:
            Tuple[Segment, Segment]: Two segments.
        rB   N)r   rS   rA   r[   )rI   rP   rC   rD   rE   r?   r?   r@   Úsplit_cellsŠ   s   
	þzSegment.split_cellsc                 C   s   | dƒS )zMake a new line segment.Ú
r?   )rT   r?   r?   r@   Úline    s   zSegment.lineÚsegmentsÚ
post_stylec                    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 rG   r?   ©Ú.0rC   Ú_stylerE   )ÚapplyrT   r?   r@   Ú	<genexpr>»   s
   € ÿ
ÿz&Segment.apply_style.<locals>.<genexpr>c                 3   s6   � | ]\}}}ˆ ||rd n|r|ˆ nˆ|ƒV  qd S rG   r?   ra   )rT   r`   r?   r@   re   À   s   € 
÷ÿù
ÿ)Ú__add__)rT   r_   rD   r`   Úresult_segmentsr?   )rd   rT   r`   r@   Úapply_style¥   s   þ
özSegment.apply_styleFrN   c                 C   s    |r	t tdƒ|ƒS tt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   )Úfilterr   r   )rT   r_   rN   r?   r?   r@   Úfilter_controlÎ   s   zSegment.filter_controlc           
      c   sŠ   � g }|j }|D ]3}d|jv r7|js7|\}}}|r6| d¡\}}	}|r*|| ||ƒƒ |	r4|V  g }|j }|sq||ƒ q|rC|V  dS 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)
rT   r_   r^   rk   rO   rC   rD   Ú_Ú_textÚnew_liner?   r?   r@   Úsplit_linesá   s(   €

ù€
	
ÿzSegment.split_linesTÚlengthÚpadÚinclude_new_linesc                 c   sÂ   � g }|j }| j}| dƒ}	|D ]B}
d|
jv rM|
jsM|
\}}}|rL| d¡\}}}|r1|| ||ƒƒ |rJ|||||d�}|rB|  |	¡ |V  |dd…= |s q||
ƒ q|r_|||||d�V  dS 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   rr   N)rk   Úadjust_line_lengthrC   rE   rl   )rT   r_   rq   rD   rr   rs   r^   rk   rt   Únew_line_segmentrO   rC   rm   rn   ro   Úcropped_liner?   r?   r@   Úsplit_and_crop_linesþ   s4   €
ÿ

õ€
ÿzSegment.split_and_crop_linesr^   c                 C   sÒ   t dd„ |D ƒƒ}||k r%|r|| d||  |ƒg }|S |dd… }|S ||krag }|j}d}|D ],}|j}	||	 |k s@|jrI||ƒ ||	7 }q2|\}
}}t|
|| ƒ}
|| |
|ƒƒ  |S |S |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 rG   )rM   ©rb   rO   r?   r?   r@   re   @  s   € z-Segment.adjust_line_length.<locals>.<genexpr>rQ   Nr   )Úsumrk   rM   rE   r   )rT   r^   rq   rD   rr   Úline_lengthro   rk   rO   Úsegment_lengthrC   Úsegment_stylerm   r?   r?   r@   rt   -  s.   ïð

ÿzSegment.adjust_line_lengthc                    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   � | ]}ˆ |j ƒV  qd S rG   )rC   rx   ©Ú	_cell_lenr?   r@   re   e  s   € z*Segment.get_line_length.<locals>.<genexpr>)r   ry   )rT   r^   r?   r}   r@   Úget_line_lengthZ  s   
zSegment.get_line_lengthÚlinesc                    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 rG   r?   ©rb   r^   ©r   r?   r@   re   r  s   € z$Segment.get_shape.<locals>.<genexpr>r   )r   ÚmaxrS   )rT   r€   Ú	max_widthr?   r‚   r@   Ú	get_shapeg  s   
zSegment.get_shapeÚwidthÚheightÚ	new_linesc           	         sŒ   |pt |ƒ}|r| dˆ d ˆƒgn| dˆ ˆƒg}| j‰ |d|… }‡ ‡‡fdd„|D ƒ|dd…< t |ƒ|k rD| |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.
        rQ   r]   Nc                    s   g | ]	}ˆ |ˆˆd �‘qS ))rD   r?   r�   ©rt   rD   r†   r?   r@   Ú
<listcomp>’  s    ÿz%Segment.set_shape.<locals>.<listcomp>)rS   rt   Úextend)	rT   r€   r†   r‡   rD   rˆ   Ú_heightÚblankÚshaped_linesr?   r‰   r@   Ú	set_shapeu  s   &ÿÿzSegment.set_shaperT   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.
        NrQ   r]   ©rS   ©rT   r€   r†   r‡   rD   rˆ   Úextra_linesr�   r?   r?   r@   Ú	align_top™  ó   $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.
        NrQ   r]   r�   r‘   r?   r?   r@   Úalign_bottom¶  r”   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.
        NrQ   r]   r   r�   )
rT   r€   r†   r‡   rD   rˆ   r’   r�   Ú	top_linesÚbottom_linesr?   r?   r@   Úalign_middleÓ  s   $zSegment.align_middlec                 c   sv   � t |ƒ}zt|ƒ}W n
 ty   Y dS w t}|D ]}|j|jkr0|js0||j|j |jƒ}q|V  |}q|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ÚStopIterationrA   rD   rE   rC   )rT   r_   Úiter_segmentsÚlast_segmentrU   rO   r?   r?   r@   Úsimplifyò  s    €
ÿÿ
zSegment.simplifyc                 c   sL   � |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)rT   r_   rO   rC   rD   Ú_controlr?   r?   r@   Ústrip_links  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?   )rT   r_   rC   rc   rE   r?   r?   r@   Ústrip_styles  s   €
ÿzSegment.strip_stylesc                 c   s\   � i }|D ]&\}}}|r$|  |¡}|du r|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)rT   r_   ÚcacherC   rD   rE   Úcolorless_styler?   r?   r@   Úremove_color+  s   €
øzSegment.remove_colorÚcutsc                 c   sª  � g }|j }t|ƒ}	 zt|ƒ}W n ty   g  Y S w |dkr"ng V  qd}|D ]¡}|jrË||j }	|	|k r?||ƒ |	}nŒzi|	|krr||ƒ |dd… V  |dd…= |	}W zt|ƒ}W q* tyq   |rm|dd… V  Y  dS w | || ¡\}
}||
ƒ |dd… V  |dd…= |}W zt|ƒ}W n3 ty¨   |r¤|dd… V  Y  dS w zt|ƒ}W w  tyÇ   |r¿|dd… V  Y    Y  dS w |js/q*|dd… 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.
        Tr   N)rk   r™   rš   r›   rC   rM   r\   )rT   r_   r¨   Úsplit_segmentsÚadd_segmentÚ	iter_cutsrP   rW   rO   Úend_posrX   r?   r?   r@   ÚdivideA  st   €ÿù

ýø
ýþýé€zSegment.divide)rF   rA   )NN©F)NTT)NT)NNF).r,   r-   r.   r/   rC   ÚstrÚ__annotations__rD   r   r   rE   r   ÚControlCoder   rJ   rK   rL   ÚpropertyrR   rM   rN   Úclassmethodr   r   r[   r\   r^   r	   rh   rj   r
   rp   rw   rt   r   r…   r�   r   r“   r•   r˜   rž   r¡   r¢   r§   r­   r?   r?   r?   r@   rA   ;   sD  
 		$üþýüû(ÿÿÿþ úþýüûú
ù.ûþýüûú,$ú
þýüûú
ù#úÿ
þýüûú
ùúÿ
þýüûú
ùúÿ
þýüûú
ùÿÿ
þrA   c                   @   sB   e Zd ZdZddee deddfdd„Z			
				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.
    Fr_   rˆ   rF   Nc                 C   s   t |ƒ| _|| _d S rG   )Úlistr_   rˆ   )rI   r_   rˆ   r?   r?   r@   Ú__init__„  s   

zSegments.__init__Úconsoler   Úoptionsr   r   c                 c   s<   � | j rt ¡ }| jD ]}|V  |V  qd S | jE d H  d S rG   )rˆ   rA   r^   r_   )rI   r·   r¸   r^   rO   r?   r?   r@   Ú__rich_console__ˆ  s   €
þzSegments.__rich_console__r®   ©r·   r   r¸   r   rF   r   )	r,   r-   r.   r/   r	   rA   rK   r¶   r¹   r?   r?   r?   r@   r´   {  s    ÿÿþr´   c                   @   sB   e Zd Zddeee  deddfdd„Z				
			ddd„ZdS )ÚSegmentLinesFr€   rˆ   rF   Nc                 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ˆ   )rI   r€   rˆ   r?   r?   r@   r¶   •  s   

zSegmentLines.__init__r·   r   r¸   r   r   c                 c   sJ   � | j rt ¡ }| jD ]
}|E d H  |V  qd S | jD ]}|E d H  qd S rG   )rˆ   rA   r^   r€   )rI   r·   r¸   ro   r^   r?   r?   r@   r¹      s   €

þ
ÿzSegmentLines.__rich_console__r®   rº   )	r,   r-   r.   r	   r
   rA   rK   r¶   r¹   r?   r?   r?   r@   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_numberszVWhen you call [b]print()[/b], Rich [i]renders[/i] the object in to the 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)8Úenumr   Ú	functoolsr   Ú	itertoolsr   Úloggingr   Úoperatorr   Útypingr   r   r	   r
   r   r   r   r   r   r   Úcellsr   r   r   r   Úreprr   r   rD   r   r·   r   r   r   Úlogr   rR   r±   rA   r´   r»   r,   Úpip._vendor.rich.consoleÚpip._vendor.rich.syntaxr½   Úpip._vendor.rich.textr¾   ÚcodeÚfrom_markuprC   ÚruleÚprintrµ   ÚrenderÚ	fragmentsr?   r?   r?   r@   Ú<module>   sl    0ÿ    C

ÿ
ÿ
ÿ
ÿß