
    Kid                     l   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!  G 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e             Z& G d d      Z' G d d      Z(e)dk(  rd dl*mZ d dl+m,Z, d dl-m.Z. dZ/ e.j`                  d      Z1 e       Zeje                  d       ejg                  d       ejg                  d       ejg                   e,e/dd             ejg                          ejg                  d        e4ejk                  e1            Z6ejg                  e6       ejg                          ejg                  d        ejg                  e1       ejg                  d!       y"y")#    )IntEnum)	lru_cache)filterfalse)
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RenderResultc                   P    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y)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     X/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/rich/segment.pyr   r       sY    NDODEKKIKNONMrC   r   c                      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       ZdefdZdefd	Zedefd
       Ze ed      dd deded   fd              Zdeded   fdZed0d       Ze	 	 d1ded    dee   dee   ded    fd       Ze	 d2ded    deded    fd       Zeded    deed       fd       Zeded    deeed    ef      fd       Ze	 	 	 d3ded    dedee   dededeed       fd       Z e	 	 d4ded    dedee   deded    f
d       Z!eded    defd       Z"ed eed       deeef   fd!       Z#e	 	 	 d5d eed       d"ed#ee   dee   d$edeed       fd%       Z$e	 d2d&e%d    d eed       d"ed#eded$edeed       fd'       Z&e	 d2d&e%d    d eed       d"ed#eded$edeed       fd(       Z'e	 d2d&e%d    d eed       d"ed#eded$edeed       fd)       Z(eded    ded    fd*       Z)eded    ded    fd+       Z*eded    ded    fd,       Z+eded    ded    fd-       Z,eded    d.ee   deed       fd/       Z-y)6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                 ,    | \  }}}|rdS t        |      S )zzThe number of terminal cells required to display self.text.

        Returns:
            int: A number of cells.
        r   )r   )selfrG   _stylerI   s       rD   cell_lengthzSegment.cell_lengthN   s!     !%fgq/$/rC   c              #      K   | j                    | j                  | j                  | j                   y y | j                   | j                   y wN)rG   rI   rH   rL   s    rD   __rich_repr__zSegment.__rich_repr__X   sH     ii<<zz%jj  & **,,s   AAc                 ,    t        | j                        S )z#Check if the segment contains text.)boolrG   rQ   s    rD   __bool__zSegment.__bool__a   s    DIIrC   c                     | j                   duS )z,Check if the segment contains control codes.N)rI   rQ   s    rD   
is_controlzSegment.is_controle   s     ||4''rC   i @  segmentcut)rF   rF   c                    |\  }}}t         }|j                  }||k\  r| |d||      fS t        }t        ||z  t	        |      z        }		 |d|	 }
t        |
      }||z
  }|s ||
||       |||	d ||      fS |dk(  r1 |||	         dk(  r# ||d|	 dz   ||       |d||	dz   d z   ||      fS |dk(  r4 |||	dz
           dk(  r# ||d|	dz
   dz   ||       |d||	d z   ||      fS ||k  r|	dz  }	n|	dz  }	)a\  Split a segment in to two at a given cell position.

        Note that splitting a double-width character, may result in that character turning
        into two spaces.

        Args:
            segment (Segment): A segment to split.
            cut (int): A cell position to cut on.

        Returns:
            A tuple of two segments.
         Nr    r   )rF   rN   r   intlenr   )clsrX   rY   rG   rH   rI   _SegmentrN   	cell_sizeposbeforecell_posout_bys                rD   _split_cellszSegment._split_cellsj   ss     'eW))+HR888+	3$D	12$3ZF'H^FVUG4T#$Z8  |	$s) 4 9T$3Z#-ug>S4a	?2E7C  |	$sQw- 8A =T)C!G_s2E7CS4:-ug>  #~qq- rC   c                     | \  }}}|dk\  sJ t        |      r=|t        |      k\  r| t        d||      fS t        |d| ||      t        ||d ||      fS | j                  | |      S )ax  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.

        Args:
            cut (int): Offset within the segment to cut.

        Returns:
            Tuple[Segment, Segment]: Two segments.
        r   r[   N)r   r_   rF   rg   )rL   rY   rG   rH   rI   s        rD   split_cellszSegment.split_cells   s      $eWaxx!$'c$iWR888Tc
E73ST
E73 
   s++rC   c                      | d      S )zMake a new line segment.
rB   )r`   s    rD   linezSegment.line   s     4yrC   segments
post_stylec                 `     |}|r|j                    fd|D        }r f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   N   K   | ]  \  }}} ||rd n |      |        y wrP   rB   ).0rG   rM   rI   applyr`   s       rD   	<genexpr>z&Segment.apply_style.<locals>.<genexpr>   s0      )D&' D'$uV}gFs   "%c              3   P   K   | ]  \  }}} ||rd n|r|z   n|        y wrP   rB   )rq   rG   rM   rI   r`   rn   s       rD   rs   z&Segment.apply_style.<locals>.<genexpr>   sB       *D&'  # 5;fz1s   #&)__add__)r`   rm   rH   rn   result_segmentsrr   s   `  ` @rD   apply_stylezSegment.apply_style   sC    & #MME-<O  .=O rC   rW   c                 Z    |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.

        rI   )filterr   r   )r`   rm   rW   s      rD   filter_controlzSegment.filter_control   s+     *Y/::z)4h??rC   c              #     K   g }|j                   }|D ]k  }d|j                  v rS|j                  sG|\  }}}|s&|j                  d      \  }}	}|r | | ||             |	r| g }|j                   }|r=d ||       m |r| yyw)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.
        rk   NappendrG   rI   	partition
r`   rm   rl   r}   rX   rG   rH   __textnew_lines
             rD   split_lineszSegment.split_lines   s      ! 	 Gw||#GOO!(eQ,0NN4,@)E8Ts5%01"
!!%  w	  J s   6B	<B	6B	c              #     K   g }|j                   }|D ]m  }d|j                  v rU|j                  sI|\  }}}|s&|j                  d      \  }}	}|r | | ||             |	r|df g }|j                   }|r?f ||       o |r|df yyw)a2  Split a sequence of segments in to a list of lines and a boolean to indicate if there was a new line.

        Args:
            segments (Iterable[Segment]): Segments potentially containing line feeds.

        Yields:
            Iterable[List[Segment]]: Iterable of segment lists, one per line.
        rk   TFNr|   r   s
             rD   split_lines_terminatorzSegment.split_lines_terminator  s      ! 	 Gw||#GOO!(eQ,0NN4,@)E8Ts5%01#Tl*!!%  w	  - s   6B>B8Blengthpadinclude_new_linesc              #     K   g }|j                   }| j                  } | d      }	|D ]  }
d|
j                  v rt|
j                  sh|
\  }}}|s&|j	                  d      \  }}}|r | | ||             |r3 |||||      }|r|j                  |	       | |j                          |r^ ||
        |r |||||       yyw)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.
        rk   )rH   r   N)r}   adjust_line_lengthrG   rI   r~   clear)r`   rm   r   rH   r   r   rl   r}   r   new_line_segmentrX   rG   segment_styler   r   r   cropped_lines                    rD   split_and_crop_lineszSegment.split_and_crop_lines2  s     * ! 33t9 	 Gw||#GOO)0&mQ,0NN4,@)E8Ts5-89'9 &3( -(//0@A**

  w!	 " $T6CHH s   A
CAC+Crl   c                 ^   t        d |D              }||k  r|r| | d||z
  z  |      gz   }|S |dd }|S ||kD  rmg }|j                  }d}|D ]V  }|j                  }	||	z   |k  s|j                  r ||       ||	z  }1|\  }
}}t	        |
||z
        }
 | | |
|              |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              3   4   K   | ]  }|j                     y wrP   )rN   )rq   rX   s     rD   rs   z-Segment.adjust_line_length.<locals>.<genexpr>t  s     B''--Bs   r]   Nr   )sumr}   rN   rI   r   )r`   rl   r   rH   r   line_lengthr   r}   rX   segment_lengthrG   r   r   s                rD   r   zSegment.adjust_line_lengtha  s    & BTBB 3sf{.B'CU#K"LL& #  7" ! 6!H__FK 	!(!4!4/&8GOO7O>1K-4*D-(v/CDD3t]34 	  AwHrC   c                 8    t         t        f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   <   K   | ]  \  }}}|r
 |        y wrP   rB   )rq   rG   rH   rI   	_cell_lens       rD   rs   z*Segment.get_line_length.<locals>.<genexpr>  s     S';tUG79T?Ss   )r   r   )r`   rl   r   s     @rD   get_line_lengthzSegment.get_line_length  s     	StSSSrC   linesc                 f    | j                   |rt        f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   .   K   | ]  } |        y wrP   rB   )rq   rl   r   s     rD   rs   z$Segment.get_shape.<locals>.<genexpr>  s     @$-@s   r   )r   maxr_   )r`   r   	max_widthr   s      @rD   	get_shapezSegment.get_shape  s3     --DIC@%@@q	3u:&&rC   widthheight	new_linesc                 $   |xs t        |      }|r | d|z  dz   |      gn | d|z  |      g}| j                  }|d| }	|D 
cg c]  }
 ||
||       c}
|	dd t        |	      |k  r!|	j                  |g|t        |	      z
  z         |	S c c}
w )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.
        r]   rk   N)rH   )r_   r   extend)r`   r   r   r   rH   r   _heightblankr   shaped_linesrl   s              rD   	set_shapezSegment.set_shape  s    * &CJ 1:Sut#U+,C%KQV@W?X 	 !33XgEJ
=AtU%8
Q |w&7S5F+F GH
s   Br`   c                     |t        |      z
  }|s|dd S |d| }|r | d|z  dz   |      n | d|z  |      }||gg|z  z   }|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.
        Nr]   rk   r_   r`   r   r   r   rH   r   extra_linesr   s           rD   	align_topzSegment.align_top  sf    * s5z)8Ogv2;C%K$&.S5[RWAX%	K//rC   c                     |t        |      z
  }|s|dd S |d| }|r | d|z  dz   |      n | d|z  |      }|gg|z  |z   }|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.
        Nr]   rk   r   r   s           rD   align_bottomzSegment.align_bottom  sf    * s5z)8Ogv2;C%K$&.S5[RWAX	K'%/rC   c                     |t        |      z
  }|s|dd S |d| }|r | d|z  dz   |      n | d|z  |      }|dz  }||z
  }	|gg|z  |z   |gg|	z  z   }|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.
        Nr]   rk   r   r   )
r`   r   r   r   rH   r   r   r   	top_linesbottom_liness
             rD   align_middlezSegment.align_middle  s    * s5z)8Ogv2;C%K$&.S5[RWAX1$	"Y.	I%-%	L0HHrC   c              #   &  K   t        |      }	 t        |      }t        }|D ]X  }|j                  |j                  k(  r7|j
                  s+ ||j                  |j                  z   |j                        }S| |}Z | y# t        $ r Y yw xY ww)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StopIterationrF   rH   rI   rG   )r`   rm   iter_segmentslast_segmentra   rX   s         rD   simplifyzSegment.simplify&  s      X	.L $ 	'G!!W]]27??' %%4l6H6H  #"&	'   		s(   BB A(B	BBBBc              #      K   |D ]C  }|j                   s|j                  |  |\  }}} | ||r|j                  d      nd       E yw)zRemove all links from an iterable of styles.

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

        Yields:
            Segment: Segments with link removed.
        N)rI   rH   update_link)r`   rm   rX   rG   rH   _controls         rD   strip_linkszSegment.strip_linksA  sV        	LG'--"7(/%eX$5 1 1$ 7dKK	Ls   A
Ac              #   :   K   |D ]  \  }}} | |d|        yw)zRemove all styles from an iterable of segments.

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

        Yields:
            Segment: Segments with styles replace with None
        NrB   )r`   rm   rG   rM   rI   s        rD   strip_styleszSegment.strip_stylesR  s-      &. 	+!D&'dD'**	+s   c              #      K   i }|D ]E  \  }}}|r1|j                  |      }||j                  }|||<    | |||       : | |d|       G yw)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)r`   rm   cacherG   rH   rI   colorless_styles          rD   remove_colorzSegment.remove_color_  sl      %'$, 	/ D%"'))E"2"*&+&9&9O#2E%L$99$g..	/s   AAcutsc              #   V  K   g }|j                   }t        |      }	 t        |d      }|dk(  ry|dk7  rng  d}|j                  }|j                  }	t
        }
|D ]  }|\  }}}|s|r|n
| |
|      z   }||k  r ||       |}+||k(  r9 ||        |	         |        |}t        |d      }|dk(  r|r	 |	         yi|j                  ||z
        \  }}|\  }}} ||        |	         |        |}t        |d      }|dk(  r|r	 |	         y|r  |	        yw)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\   Nr   )r}   r   r   r   copyr   ri   )r`   rm   r   split_segmentsadd_segment	iter_cutsrY   rc   segments_clearsegments_copyr   rX   rG   rM   rI   end_posrd   s                    rD   dividezSegment.divideu  sp     +-$++J	y"%CbyaxH  '--&++#	 #	G$+!D&'!(#cIdO.CS=(!Cc>('/)"$!Cy"-Cby)"//1 '.&9&9#)&DOFG,3)D&'''/)"$C9b)"9%+o-C #	J os   A%D)(B4D)D))rJ   rF   )NNF)NTT)NT)NNF).r.   r/   r0   r1   str__annotations__rH   r   r   rI   r   ControlCodepropertyr^   rN   r   rR   rT   rU   rW   classmethodr   r   rg   ri   rl   r	   rw   rz   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   rC   rD   rF   rF   <   s   
 I!E8E?!/3GXh{+,30S 0 0v $  (D ( ( y-9 -3 -5AU;V -  -^,s ,u-A'B ,4    "&&*	&9%& & UO	&
 
)	& &P ?D@	*@8<@	)	@ @$ 8I#6 8DO;T  8  	* 	%Y-.	/   < 
 "&"&,I9%,I ,I 	,I
 ,I  ,I 
$y/	",I ,I\ 
 "&*9o* * 	*
 * 
i* *X 
T4	? 
Ts 
T 
T 'd4	?3 'c3h ' ' 
 !%!%!DO$! ! 	!
 ! ! 
d9o	! !F   )_DO$  	
   
d9o	 8   )_DO$  	
   
d9o	 8   )_DO$  	
   
d9o	 <  3 8K  4 L8I#6 L8I;N L L  
+HY$7 
+HY<O 
+ 
+ /HY$7 /HY<O / /* B	*B2:3-B	$y/	"B BrC   rF   c                   @    e Zd ZdZddee   deddfdZ	 	 	 	 	 	 d	dZy)
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.
    rm   r   rJ   Nc                 2    t        |      | _        || _        y rP   )listrm   r   )rL   rm   r   s      rD   __init__zSegments.__init__  s    X"rC   c              #      K   | j                   r.t        j                         }| j                  D ]
  }| |  y | j                  E d {    y 7 wrP   )r   rF   rl   rm   )rL   consoleoptionsrl   rX   s        rD   __rich_console__zSegments.__rich_console__  sF      >><<>D== 
 }}$$s   A
AAAr   r   r   r   r   rJ   r   )	r.   r/   r0   r1   r	   rF   rT   r   r   rB   rC   rD   r   r     sD    #'!2 #t #PT #	% 	%+;	%		%rC   r   c                   B    e Zd Zddeee      deddfdZ	 	 	 	 	 	 ddZy)	SegmentLinesr   r   rJ   Nc                 2    t        |      | _        || _        y)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   )rL   r   r   s      rD   r   zSegmentLines.__init__  s     %[
"rC   c              #      K   | j                   r4t        j                         }| j                  D ]  }|E d {    |  y | j                  D ]  }|E d {     y 7 '7 	wrP   )r   rF   rl   r   )rL   r   r   r   rl   s        rD   r   zSegmentLines.__rich_console__  s`      >>||~H

  

       s!   5A#AA#A!A#!A#r   r   )	r.   r/   r0   r	   r
   rF   rT   r   r   rB   rC   rD   r   r     sC    	#htG}5 	#$ 	#SW 	#
  
 +;
 	
 rC   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)7enumr   	functoolsr   	itertoolsr   operatorr   typingr   r   r	   r
   r   r   r   r   r   r   cellsr   r   r   r   r   reprr   r   rH   r   r   r   r   r   r   r^   r   r   rF   r   r   r.   rich.consolerich.syntaxr   	rich.textr   codefrom_markuprG   ruleprintr   render	fragmentsrB   rC   rD   <module>r      s     !     $ >>' * 	+	+uS#X
&'	+sC
 " {	j {	 {	|% %2   2 z$"D
 4<=DiGLL MMg MM45MM&xd;<MMOMM] W^^D)*IMM)MMOMMVWMM$MM^; rC   