a
    [fl]                     @   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rd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re*dkrd 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 )%    )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?   jC:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\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< eddd	Zedd
dZe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dFed  ee	 ee	 ed  dddZedGed  eed  ddd Zeed  eed   d!d"d#ZedHed  eee	 eeeed   d%d&d'Z edIe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.d/d0Z#edJeed   eee ee	 eeed   d1d2d3Z$edKe%d  eed   eee	eeed   d4d5d6Z&edLe%d  eed   eee	eeed   d4d7d8Z'edMe%d  eed   eee	eeed   d4d9d: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?d@Z+eed  ed  d!dAdBZ,eed  ee eed   dCdDdEZ-dS )N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   s:   | j V  | jd u r&| jd ur6| jV  n| 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 @  )rA   rA   )segmentcutrF   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r||
|||||	d  ||fS ||kr||
d |	d  d |||d||	d   ||fS qd 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   s4    


zSegment._split_cells)rP   rF   c                 C   sb   | \}}}t |rV|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)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 rG   r?   .0rC   _stylerE   )applyrT   r?   r@   	<genexpr>   s   z&Segment.apply_style.<locals>.<genexpr>c                 3   s4   | ],\}}} ||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_styleF)r_   rN   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   )rT   r_   rN   r?   r?   r@   filter_control   s    zSegment.filter_control)r_   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)
rT   r_   r^   rk   rO   rC   rD   __textnew_liner?   r?   r@   split_lines   s     



zSegment.split_linesT)r_   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  |dd= 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   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   s,    


zSegment.split_and_crop_lines)r^   rq   rD   rr   rF   c                 C   s   t dd |D }||k rF|r8|| d||  |g }q|dd }n||krg }|j}d}|D ]X}|j}	||	 |k s||jr|| ||	7 }q`|\}
}}t|
|| }
|| |
|  qq`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 rG   )rM   rb   rO   r?   r?   r@   re   @      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_length)r^   rF   c                    s   t  t fdd|D S )zGet 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  ry   z*Segment.get_line_length.<locals>.<genexpr>)r   rz   )rT   r^   r?   r~   r@   get_line_lengthZ  s    
zSegment.get_line_length)linesrF   c                    s0   | j  |r t fdd|D nd}|t|fS )zGet 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  ry   z$Segment.get_shape.<locals>.<genexpr>r   )r   maxrS   )rT   r   	max_widthr?   r   r@   	get_shapeg  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.
        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_shape)rT   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.
        NrQ   r]   rS   rT   r   r   r   rD   r   extra_linesr   r?   r?   r@   	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.
        NrQ   r]   r   r   r?   r?   r@   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.
        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   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StopIterationrA   rD   rE   rC   )rT   r_   iter_segmentslast_segmentrU   rO   r?   r?   r@   simplify  s    
zSegment.simplifyc                 c   sJ   |D ]@}|j s|jdu r |V  q|\}}}| ||r<|dndV  qdS )zRemove 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 )zRemove 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   sZ   i }|D ]L\}}}|rF| |}|du r6|j}|||< | |||V  q| |d|V  qdS )zRemove 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)r_   cutsrF   c                 c   s  g }|j }t|}zt|}W n ty4   g  Y S 0 |dkr@qHg V  qd}|D ]N}|jrP||j }	|	|k r||| |	}qPz|	|kr|| |dd V  |dd= |	}W zt|}W n( ty   |r|dd V  Y  dS 0 qPn6||| \}
}||
 |dd V  |dd= |}W zt|}W n, tyV   |rN|dd V  Y  dS 0 qVzt|}W n4 ty   |r|dd V  Y    Y  dS 0 0 qVqP|dd V  dS )zDivides 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.
        r   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  sd    





zSegment.divide)NN)F)NTT)NT)NNF)F)F)F).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   ;   s   
		$  (    
.  ,"   

# 

 

 


rA   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)r_   r   rF   c                 C   s   t || _|| _d S rG   )listr_   r   )rI   r_   r   r?   r?   r@   __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 rG   )r   rA   r^   r_   )rI   r   r   r^   rO   r?   r?   r@   __rich_console__  s    

zSegments.__rich_console__)F)	r,   r-   r.   r/   r	   rA   rK   r   r   r?   r?   r?   r@   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   )rI   r   r   r?   r?   r@   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 rG   )r   rA   r^   r   )rI   r   r   ro   r^   r?   r?   r@   r     s    



zSegmentLines.__rich_console__)F)	r,   r-   r.   r	   r
   rA   rK   r   r   r?   r?   r?   r@   r     s   r   __main__)r   )Syntax)Textzfrom 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.consoleZpip._vendor.rich.syntaxr   pip._vendor.rich.textr   codefrom_markuprC   ruleprintr   render	fragmentsr?   r?   r?   r@   <module>   sf   0    C






