o
    MEg9                     @   s~  d dl mZ d dlmZmZmZ ddlmZ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 G d	d
 d
eZeee eeef eeeef f ZG dd deZedkrd dlmZ d dlmZ d dlmZ dZe dZ!e Z"e"#d e"$d e"$d e"$eeddd e"$  e"$d e%e"&e!Z'e"$e' e"$  e"$d e"$e! e"$d dS dS )    )IntEnum)Dict
NamedTupleOptional   )cell_lenset_cell_size)Style)filterfalse)
attrgetter)IterableListSequenceUnionTuplec                   @   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_ROWCURSOR_MOVE_TOERASE_IN_LINE r3   r3   f/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/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d7ddZe		d8ded  dee	 dee	 ded  fddZe	d9ded  deded  fddZeded  deed   fddZe			d:ded  dedee	 dededeed   fd d!Ze		d;d"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(d)Ze			d<d'eed   d*ed+ee dee	 d,edeed   fd-d.Z eded  ded  fd/d0Z!eded  ded  fd1d2Z"eded  ded  fd3d4Z#eded  ded  fd5d6Z$dS )=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 rd| jd| jd| j dS d| jd| jdS )zSimplified repr.zSegment(z, ))r9   r7   r8   selfr3   r3   r4   __repr__7   s   zSegment.__repr__c                 C   s
   t | jS )z#Check if the segment contains text.)boolr7   r<   r3   r3   r4   __bool__>   s   
zSegment.__bool__c                 C   s   | j rdS t| jS )zGet cell length of segment.r   )r9   r   r7   r<   r3   r3   r4   cell_lengthB   s   zSegment.cell_lengthc                 C   s
   | j duS )z,Check if the segment contains control codes.N)r9   r<   r3   r3   r4   
is_controlG   s   
zSegment.is_controlc                 C   s   | dS )zMake a new line segment.
r3   )clsr3   r3   r4   lineL   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 Nr3   .0r7   _styler9   )applyrD   r3   r4   	<genexpr>f   s
    
z&Segment.apply_style.<locals>.<genexpr>c                 3   s6    | ]\}}} ||rd n|r| n|V  qd S rH   r3   rI   )rD   rG   r3   r4   rM   k   s    
)__add__)rD   rF   r8   rG   r3   )rL   rD   rG   r4   apply_styleQ   s   
zSegment.apply_styleFrB   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.

        r9   )filterr   r
   )rD   rF   rB   r3   r3   r4   filter_controlw   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.
        rC   N)appendr7   r9   	partition)
rD   rF   rE   rR   segmentr7   r8   __textnew_liner3   r3   r4   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.
        rC   )r8   rZ   N)rR   adjust_line_lengthr7   r9   rS   )rD   rF   rY   r8   rZ   r[   rE   rR   r\   new_line_segmentrT   r7   rU   rV   rW   cropped_liner3   r3   r4   split_and_crop_lines   s4   



zSegment.split_and_crop_linesrE   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       | ]}|j V  qd S rH   rA   rJ   rT   r3   r3   r4   rM          z-Segment.adjust_line_length.<locals>.<genexpr> Nr   )sumrR   rA   r9   r   )rD   rE   rY   r8   rZ   line_lengthrW   rR   rT   segment_lengthr7   segment_stylerU   r3   r3   r4   r\      s.   

zSegment.adjust_line_lengthc                 C   s   t 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                 s   r`   rH   ra   rb   r3   r3   r4   rM     rc   z*Segment.get_line_length.<locals>.<genexpr>)re   )rD   rE   r3   r3   r4   get_line_length  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 rH   r3   )rJ   rE   ri   r3   r4   rM     s    z$Segment.get_shape.<locals>.<genexpr>r   )ri   maxlen)rD   rj   	max_widthr3   rk   r4   	get_shape  s   
zSegment.get_shapewidthheight	new_linesc                 C   s   |du rt |}g }|rtd| |tdgntd| |g}|j}| j}	t|}
t|D ]}t|
d}|du r=|| q-||	|||d q-|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. Defaults to None.
            new_lines (bool, optional): Padded lines should include "
". Defaults to False.

        Returns:
            List[List[Segment]]: New list of lines that fits width x height.
        Nrd   rC   )r8   )rm   r5   rR   r\   iterrangenext)rD   rj   rp   rq   r8   rr   shaped_linespad_linerR   r\   
iter_linesrU   rE   r3   r3   r4   	set_shape  s    

zSegment.set_shapec                 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)rs   ru   StopIterationr5   r8   r9   r7   )rD   rF   iter_segmentslast_segment_SegmentrT   r3   r3   r4   simplifyG  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)r9   r8   update_link)rD   rF   rT   r7   r8   _controlr3   r3   r4   strip_linksb  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
        Nr3   )rD   rF   r7   rK   r9   r3   r3   r4   strip_styless  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)rD   rF   cacher7   r8   r9   colorless_styler3   r3   r4   remove_color  s   
zSegment.remove_color)r:   r5   )NN)F)NTT)NT)NNF)%r    r!   r"   r#   r7   str__annotations__r8   r   r	   r9   r   ControlCoder>   r?   r@   propertyintrA   rB   classmethodrE   r   rO   rQ   r   rX   r_   r\   ri   r   ro   ry   r~   r   r   r   r3   r3   r3   r4   r5   &   s   
 	% 
.,$

) r5   __main__)Syntax)Text)Consolezfrom 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)(enumr   typingr   r   r   cellsr   r   r8   r	   	itertoolsr
   operatorr   r   r   r   r   r   r   r   r   r5   r    rich.syntaxr   	rich.textr   rich.consoler   codefrom_markupr7   consoleruleprintlistrender	fragmentsr3   r3   r3   r4   <module>   sP      s





