o
    IJe^                     @   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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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rd dl-m!Z! d dl.m/Z/ d dl0m1Z1 dZ2e13dZ4e! Z e 5d e 6d e 6d e 6e/e2ddd  e 6  e 6d! e7e 8e4Z9e 6e9 e 6  e 6d" e 6e4 e 6d# 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cached_cell_lencell_lenget_character_cell_sizeset_cell_size)Result	rich_repr)Style)ConsoleConsoleOptionsRenderResultrichc                   @   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__ZBELLZCARRIAGE_RETURNHOMEZCLEARSHOW_CURSORHIDE_CURSORZENABLE_ALT_SCREENZDISABLE_ALT_SCREENZ	CURSOR_UPZCURSOR_DOWNZCURSOR_FORWARDZCURSOR_BACKWARDZCURSOR_MOVE_TO_COLUMNZCURSOR_MOVE_TOZERASE_IN_LINEZSET_WINDOW_TITLE r5   r5   f/home/seandotau/au.memorygallery.club/gallery/lib/python3.10/site-packages/pip/_vendor/rich/segment.pyr   #   s$    r   c                   @   s  e Zd ZU dZeed< dZee ed< dZ	ee
e  ed< edefddZdefd	d
Z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dHddZe		dIded  dee dee ded  fddZe	dJded  deded  fdd Zeded  deed   fd!d"Ze		#	#dKded  d$edee d%ed&edeed   fd'd(Ze		#dLd)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d/d0Z"e			dMd.eed   d1ed2ee dee d3edeed   fd4d5Z#e	dJd6e$d  d.eed   d1ed2eded3edeed   fd7d8Z%e	dJd6e$d  d.eed   d1ed2eded3edeed   fd9d:Z&e	dJd6e$d  d.eed   d1ed2eded3e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?d@Z)eded  ded  fdAdBZ*eded  ded  fdCdDZ+eded  dEee deed   fdFdG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.

    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   )selfr8   _styler:   r5   r5   r6   cell_lengthQ   s   
zSegment.cell_lengthc                 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)r8   r:   r9   r<   r5   r5   r6   __rich_repr__[   s   

zSegment.__rich_repr__c                 C   s
   t | jS )z#Check if the segment contains text.)boolr8   r@   r5   r5   r6   __bool__d   s   
zSegment.__bool__c                 C   s
   | j duS )z,Check if the segment contains control codes.N)r:   r@   r5   r5   r6   
is_controlh   s   
zSegment.is_controli @  segmentcut)r7   r7   c                 C   s0  |\}}}t }|j}||kr||d||fS t}t|| t|d  }	|d |	 }
t|
}||krB||
|||||	d  ||fS |	t|k r||	 }|	d7 }	|||7 }|d |	 }
||krp||
|||||	d  ||fS ||kr||
d |	d  d |||d||	d   ||fS |	t|k sHtd)N r    zWill never reach here)r7   r>   r   intlenr   AssertionError)clsrE   rF   r8   r9   r:   _Segmentr>   	cell_sizeposbeforeZcell_poscharr5   r5   r6   _split_cellsm   s8   


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.
        rG   N)r   rJ   r7   rR   )r<   rF   r8   r9   r:   r5   r5   r6   split_cells   s   
	zSegment.split_cellsc                 C   s   | dS )zMake a new line segment.
r5   )rL   r5   r5   r6   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 r?   r5   .0r8   r=   r:   )applyrL   r5   r6   	<genexpr>   s
    
z&Segment.apply_style.<locals>.<genexpr>c                 3   s6    | ]\}}} ||rd n|r| n|V  qd S r?   r5   rX   )rL   rW   r5   r6   r[      s    

)__add__)rL   rV   r9   rW   Zresult_segmentsr5   )rZ   rL   rW   r6   apply_style   s   
zSegment.apply_styleFrD   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.

        r:   )filterr   r   )rL   rV   rD   r5   r5   r6   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.
        rT   N)appendr8   r:   	partition)
rL   rV   rU   r`   rE   r8   r9   __textnew_liner5   r5   r6   split_lines   s(   


	
zSegment.split_linesTlengthpadinclude_new_linesc                 c   s    g }|j }| j}| d}	|D ]A}
d|
jv rL|
jsL|
\}}}|rK|d\}}}|r1|| || |rI|||||d}|rB| |	 |V  |  |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.
        rT   )r9   rg   N)r`   adjust_line_lengthr8   r:   ra   clear)rL   rV   rf   r9   rg   rh   rU   r`   ri   Znew_line_segmentrE   r8   segment_stylerb   rc   rd   Zcropped_liner5   r5   r6   split_and_crop_lines	  s4   


zSegment.split_and_crop_linesrU   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 r?   )r>   )rY   rE   r5   r5   r6   r[   K  s    z-Segment.adjust_line_length.<locals>.<genexpr>rH   Nr   )sumr`   r>   r:   r   )rL   rU   rf   r9   rg   Zline_lengthrd   r`   rE   Zsegment_lengthr8   rk   rb   r5   r5   r6   ri   8  s.   

zSegment.adjust_line_lengthc                    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"    | ]\}}}|s |V  qd S r?   r5   )rY   r8   r9   r:   	_cell_lenr5   r6   r[   p  s     z*Segment.get_line_length.<locals>.<genexpr>)r   rm   )rL   rU   r5   rn   r6   get_line_lengthe  s   
zSegment.get_line_lengthlinesc                    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 r?   r5   rY   rU   rp   r5   r6   r[   }  s    z$Segment.get_shape.<locals>.<genexpr>r   )rp   maxrJ   )rL   rq   	max_widthr5   rs   r6   	get_shaper  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.
        rH   rT   Nc                    s   g | ]	} |d qS ))r9   r5   rr   ri   r9   rw   r5   r6   
<listcomp>  s    z%Segment.set_shape.<locals>.<listcomp>)rJ   ri   extend)	rL   rq   rw   rx   r9   ry   _heightblankZshaped_linesr5   rz   r6   	set_shape  s   &zSegment.set_shaperL   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.
        NrH   rT   rJ   rL   rq   rw   rx   r9   ry   extra_linesr~   r5   r5   r6   	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.
        NrH   rT   r   r   r5   r5   r6   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.
        NrH   rT   r   r   )
rL   rq   rw   rx   r9   ry   r   r~   Z	top_linesZbottom_linesr5   r5   r6   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StopIterationr7   r9   r:   r8   )rL   rV   iter_segmentsZlast_segmentrM   rE   r5   r5   r6   simplify  s    

zSegment.simplifyc                 c   sL    |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)r:   r9   update_link)rL   rV   rE   r8   r9   _controlr5   r5   r6   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
        Nr5   )rL   rV   r8   r=   r:   r5   r5   r6   strip_styles)  s   
zSegment.strip_stylesc                 c   s\    i }|D ]&\}}}|r$| |}|du r|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)rL   rV   cacher8   r9   r:   Zcolorless_styler5   r5   r6   remove_color6  s   
zSegment.remove_colorcutsc                 c   sF   g }|j }t|}	 t|d}|dkrg S |dkrng V  qd}|j}|j}	t}
|D ]q}|\}}}|r|r8|n||
| }||k rI|| |}nS||krm|| |	 V  |  |}t|d}|dkrl|ri|	 V   dS n/||| \}}|\}}}|| |	 V  |  |}t|d}|dkr|r|	 V   dS |s4q+|	 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.
        Tr   N)r`   r   r   rj   copyr   rS   )rL   rV   r   Zsplit_segmentsZadd_segmentZ	iter_cutsrF   rO   Zsegments_clearZsegments_copyro   rE   r8   r=   r:   Zend_posrP   r5   r5   r6   divideL  sb   




#zSegment.divide)r;   r7   )NNF)NTT)NT)NNF)-r.   r/   r0   r1   str__annotations__r9   r   r   r:   r   ControlCodepropertyrI   r>   r   rA   rB   rC   rD   classmethodr   r   rR   rS   rU   r	   r]   r_   r
   re   rl   ri   rp   rv   r   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   r7   ?   s>  
 		&( 
.,$

#






r7   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.
    FrV   ry   r;   Nc                 C   s   t || _|| _d S r?   )listrV   ry   )r<   rV   ry   r5   r5   r6   __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 r?   )ry   r7   rU   rV   )r<   r   r   rU   rE   r5   r5   r6   __rich_console__  s   
zSegments.__rich_console__r   r   r   r   r   r;   r   )	r.   r/   r0   r1   r	   r7   rB   r   r   r5   r5   r5   r6   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 )SegmentLinesFrq   ry   r;   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   rq   ry   )r<   rq   ry   r5   r5   r6   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 r?   )ry   r7   rU   rq   )r<   r   r   rd   rU   r5   r5   r6   r     s   


zSegmentLines.__rich_console__r   r   )	r.   r/   r0   r	   r
   r7   rB   r   r   r5   r5   r5   r6   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)Z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):enumr   	functoolsr   	itertoolsr   loggingr   operatorr   typingr   r   r	   r
   r   r   r   r   r   r   cellsr   r   r   r   r   reprr   r   r9   r   r   r   r   r   logr   rI   r   r   r7   r   r   r.   pip._vendor.rich.consoleZpip._vendor.rich.syntaxr   pip._vendor.rich.textr   codefrom_markupr8   ruleprintr   render	fragmentsr5   r5   r5   r6   <module>   sj    0    V





