B
    od^                 @   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rd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 )%    )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__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 rB   rB   J/var/www/html/venv/lib/python3.7/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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dEed  ee ee ed  dddZedFed  eed  dddZeed  eed   d d!d"ZedGed  eee eeeed   d$d%d&ZedHe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.d/Z"edIeed   eee ee eeed   d0d1d2Z#edJe$d  eed   eeeeeed   d3d4d5Z%edKe$d  eed   eeeeeed   d3d6d7Z&edLe$d  eed   eeeeeed   d3d8d9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 d@dAZ+eed  ee eed   dBdCdDZ,dS )M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   )selfrE   _stylerG   rB   rB   rC   cell_lengthQ   s    
zSegment.cell_lengthc             c   s:   | j V  | jd kr&| jd k	r6| jV  n| jV  | jV  d S )N)rE   rG   rF   )rI   rB   rB   rC   __rich_repr__[   s    


zSegment.__rich_repr__c             C   s
   t | jS )z#Check if the segment contains text.)boolrE   )rI   rB   rB   rC   __bool__d   s    zSegment.__bool__c             C   s
   | j dk	S )z,Check if the segment contains control codes.N)rG   )rI   rB   rB   rC   
is_controlh   s    zSegment.is_controli @  )rD   rD   )segmentcutrH   c             C   s0  |\}}}t }|j}||kr,||d||fS t}t|| t|d  }	|d |	 }
t|
}||kr||
|||||	d  ||fS x|	t|k r"||	 }|	d7 }	|||7 }|d |	 }
||kr||
|||||	d  ||fS ||kr||
d |	d  d |||d||	d   ||fS qW tdd S )N r    zWill never reach here)rD   rK   r   intlenr   AssertionError)clsrP   rQ   rE   rF   rG   _SegmentrK   	cell_sizeposbeforecell_poscharrB   rB   rC   _split_cellsm   s0    


zSegment._split_cells)rQ   rH   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.
        rR   N)r   rU   rD   r^   )rI   rQ   rE   rF   rG   rB   rB   rC   split_cells   s    	
zSegment.split_cellsc             C   s   | dS )zMake a new line segment.
rB   )rW   rB   rB   rC   line   s    zSegment.line)segmentsrF   
post_stylerH   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 )NrB   ).0rE   rJ   rG   )applyrW   rB   rC   	<genexpr>   s   z&Segment.apply_style.<locals>.<genexpr>c             3   s4   | ],\}}} ||rd n|r$| n|V  qd S )NrB   )rd   rE   rJ   rG   )rW   rc   rB   rC   rf      s   	)__add__)rW   rb   rF   rc   result_segmentsrB   )re   rW   rc   rC   apply_style   s    	zSegment.apply_styleF)rb   rO   rH   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.

        rG   N)filterr   r   )rW   rb   rO   rB   rB   rC   filter_control   s    zSegment.filter_control)rb   rH   c       
      c   s   g }|j }xp|D ]h}d|jkrp|jsp|\}}}xH|rl|d\}}	}|rV|| || |	r0|V  g }|j }q0W q|| qW |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)appendrE   rG   	partition)
rW   rb   ra   rl   rP   rE   rF   __textnew_linerB   rB   rC   split_lines   s     


zSegment.split_linesT)rb   lengthrF   padinclude_new_linesrH   c             c   s   g }|j }| j}| d}	x|D ]}
d|
jkr|
js|
\}}}xd|r|d\}}}|rd|| || |r>|||||d}|r| |	 |V  |  q>W q||
 qW |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`   )rF   rs   N)rl   adjust_line_lengthrE   rG   rm   clear)rW   rb   rr   rF   rs   rt   ra   rl   ru   new_line_segmentrP   rE   segment_stylern   ro   rp   cropped_linerB   rB   rC   split_and_crop_lines	  s*    


zSegment.split_and_crop_lines)ra   rr   rF   rs   rH   c             C   s   t dd |D }||k rF|r8|| d||  |g }q|dd }n||krg }|j}d}xl|D ]V}|j}	||	 |k s~|jr|| ||	7 }qb|\}
}}t|
|| }
|| |
| P qbW 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 )N)rK   )rd   rP   rB   rB   rC   rf   K  s    z-Segment.adjust_line_length.<locals>.<genexpr>rS   Nr   )sumrl   rK   rG   r   )rW   ra   rr   rF   rs   line_lengthrp   rl   rP   segment_lengthrE   rx   rn   rB   rB   rC   ru   8  s(    


zSegment.adjust_line_length)ra   rH   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    | ]\}}}|s |V  qd S )NrB   )rd   rE   rF   rG   )	_cell_lenrB   rC   rf   p  s    z*Segment.get_line_length.<locals>.<genexpr>)r   r{   )rW   ra   rB   )r~   rC   get_line_lengthe  s    
zSegment.get_line_length)linesrH   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 )NrB   )rd   ra   )r   rB   rC   rf   }  s    z$Segment.get_shape.<locals>.<genexpr>r   )r   maxrU   )rW   r   	max_widthrB   )r   rC   	get_shaper  s    
zSegment.get_shape)r   widthheightrF   	new_linesrH   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.
        rS   r`   Nc                s   g | ]} |d qS ))rF   rB   )rd   ra   )ru   rF   r   rB   rC   
<listcomp>  s    z%Segment.set_shape.<locals>.<listcomp>)rU   ru   extend)	rW   r   r   r   rF   r   _heightblankshaped_linesrB   )ru   rF   r   rC   	set_shape  s    (zSegment.set_shape)rW   r   r   r   rF   r   rH   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.
        NrS   r`   )rU   )rW   r   r   r   rF   r   extra_linesr   rB   rB   rC   	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.
        NrS   r`   )rU   )rW   r   r   r   rF   r   r   r   rB   rB   rC   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.
        NrS   r`   r   )rU   )
rW   r   r   r   rF   r   r   r   	top_linesbottom_linesrB   rB   rC   align_middle  s    $zSegment.align_middlec             c   sx   t |}yt|}W n tk
r(   dS X t}x>|D ]6}|j|jkr`|js`||j|j |j}q4|V  |}q4W |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StopIterationrD   rF   rG   rE   )rW   rb   iter_segmentslast_segmentrX   rP   rB   rB   rC   simplify  s    

zSegment.simplifyc             c   sN   xH|D ]@}|j s|jdkr"|V  q|\}}}| ||r>|dndV  qW dS )zRemove all links from an iterable of styles.

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

        Yields:
            Segment: Segments with link removed.
        N)rG   rF   update_link)rW   rb   rP   rE   rF   _controlrB   rB   rC   strip_links  s
    


zSegment.strip_linksc             c   s&   x |D ]\}}}| |d|V  qW 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
        NrB   )rW   rb   rE   rJ   rG   rB   rB   rC   strip_styles)  s    
zSegment.strip_stylesc             c   s^   i }xT|D ]L\}}}|rH| |}|dkr8|j}|||< | |||V  q
| |d|V  q
W 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)rW   rb   cacherE   rF   rG   colorless_stylerB   rB   rC   remove_color6  s    
zSegment.remove_color)rb   cutsrH   c             c   sN  g }|j }t|}x*t|d}|dkr*g S |dkr4P g V  qW d}|j}|j}	t}
x|D ]}|\}}}x|r<|rv|n
||
| }||k r|| |}P ||kr|| |	 V  |  |}t|d}|dkr|r|	 V  dS P n6||| \}}|\}}}|| |	 V  |  |}t|d}|dkrh|r6|	 V  dS qhW qXW |	 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)rl   r   r   rv   copyr   r_   )rW   rb   r   split_segmentsadd_segment	iter_cutsrQ   rZ   segments_clearsegments_copyr~   rP   rE   rJ   rG   end_posr[   rB   rB   rC   divideL  sZ    






zSegment.divide)NN)F)NTT)NT)NNF)F)F)F)-r.   r/   r0   r1   str__annotations__rF   r   r   rG   r   ControlCodepropertyrT   rK   r   rL   rM   rN   rO   classmethodr   r   r^   r_   ra   r	   ri   rk   r
   rq   rz   ru   r   r   r   r   r   r   r   r   r   r   r   r   rB   rB   rB   rC   rD   ?   s~   
		& #
  ' &"  rD   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   rH   c             C   s   t || _|| _d S )N)listrb   r   )rI   rb   r   rB   rB   rC   __init__  s    
zSegments.__init__r   r   r   )consoleoptionsrH   c             c   s<   | j r,t }x(| jD ]}|V  |V  qW n| jE d H  d S )N)r   rD   ra   rb   )rI   r   r   ra   rP   rB   rB   rC   __rich_console__  s    zSegments.__rich_console__)F)	r.   r/   r0   r1   r	   rD   rM   r   r   rB   rB   rB   rC   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   rH   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   rB   rB   rC   r     s    
zSegmentLines.__init__r   r   r   )r   r   rH   c             c   sN   | j r0t }x:| jD ]}|E d H  |V  qW nx| jD ]}|E d H  q8W d S )N)r   rD   ra   r   )rI   r   r   rp   ra   rB   rB   rC   r     s    
zSegmentLines.__rich_console__)F)	r.   r/   r0   r	   r
   rD   rM   r   r   rB   rB   rB   rC   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_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   rF   r   r   r   r   r   logr   rT   r   r   rD   r   r   r.   pip._vendor.rich.consoleZpip._vendor.rich.syntaxr   pip._vendor.rich.textr   codefrom_markuprE   ruleprintr   render	fragmentsrB   rB   rB   rC   <module>   s\   0    V






