xid"ddlmZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZddlmZmZmZmZmZddlmZmZdd lmZe r dd lmZm Z m!Z!Gd d eZ"eee"ee"ee#e$ffee"e#e#ffZ%eGd de Z&GddZ'GddZ(e)dkr?ddl*mZddl+m,Z,ddl-m.Z.dZ/e.j0dZ1eZe2de3de3de3e,e/dde3e3de4e5e1Z6e3e6e3e3d e3e1e3d!d"Sd"S)#)IntEnum) lru_cache) filterfalse) attrgetter) TYPE_CHECKINGDictIterableList NamedTupleOptionalSequenceTupleTypeUnion)_is_single_cell_widthscached_cell_lencell_lenget_character_cell_size set_cell_size)Result rich_repr)Style)ConsoleConsoleOptions RenderResultcReZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdS) ControlTypezDNon-printable control codes which typically translate to ANSI codes.r N)__name__ __module__ __qualname____doc__BELLCARRIAGE_RETURNHOMECLEAR SHOW_CURSOR HIDE_CURSORENABLE_ALT_SCREENDISABLE_ALT_SCREEN CURSOR_UP CURSOR_DOWNCURSOR_FORWARDCURSOR_BACKWARDCURSOR_MOVE_TO_COLUMNCURSOR_MOVE_TO ERASE_IN_LINESET_WINDOW_TITLEd/home/jenkins/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/rich/segment.pyrr sjNN DO D EKKIKNONMrCrceZdZUdZeed<dZeeed<dZ ee e ed<e de fdZdefdZdefd Ze defd Zeed d dd e dedfdZd e dedfdZed2dZe d3deddeedeededfdZe d4deddededfdZededdeedfdZededdeeedeffdZe d5dedde deedededeedf dZ e d6dedde deedededf d Z!ededde fd!Z"ed"eeddee e ffd#Z#e d7d"eedd$e d%ee deed&edeedf d'Z$e d4d(e%dd"eedd$e d%e ded&edeedfd)Z&e d4d(e%dd"eedd$e d%e ded&edeedfd*Z'e d4d(e%dd"eedd$e d%e ded&edeedfd+Z(ededdedfd,Z)ededdedfd-Z*ededdedfd.Z+ededdedfd/Z,ededd0ee deedfd1Z-dS)8SegmentaA 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. textNstylecontrolreturnc4|\}}}|rdnt|S)zzThe number of terminal cells required to display self.text. Returns: int: A number of cells. r)r)selfrG_stylerIs rD cell_lengthzSegment.cell_lengthNs%!%fg/qq$/rCc#vK|jV|j|j |jVdSdS|jV|jVdSN)rGrIrHrLs rD __rich_repr__zSegment.__rich_repr__Xsai < z%j     &%*   ,     rCc*t|jS)z#Check if the segment contains text.)boolrGrQs rD__bool__zSegment.__bool__asDIrCc|jduS)z,Check if the segment contains control codes.N)rIrQs rD is_controlzSegment.is_controles|4''rCi@segmentcut)rFrFc|\}}}t}|j}||kr||d||fSt}t||z t |z} |d| } t | } | |z } | s"|| ||||| d||fS| dkrH||| dkr3||d| dz|||d|| dzdz||fS| dkrK||| dz dkr3||d| dz dz|||d|| dz||fS| |kr| dz } n| dz} )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. TNr r)rFrNrintlenr) clsrXrYrGrHrI_SegmentrN cell_sizeposbeforecell_posout_bys rD _split_cellszSegment._split_cellsjs 'eW) +  HHR888 8+ 3$D 122 $3$ZF''H^F HVUG44HT#$$Z88|| $s) 4 4 9 9HT$3$Z#-ug>>HS4a ?2E7CC|| $sQw- 8 8A = =HT)C!G)_s2E7CCHS4:-ug>>#~~qq- rCc|\}}}|dksJt|rX|t|kr|td||fSt|d|||t||d||fS|||S)axSplit 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. rr[N)rr_rFrg)rLrYrGrHrIs rD split_cellszSegment.split_cellss $eWaxxxx !$ ' ' c$iiWR8888TcT E733STT E733    s+++rCc|dS)zMake a new line segment. rB)r`s rDlinez Segment.liness4yyrCsegments post_stylecb|}|r|jfd|D}rfd|D}|S)aApply 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). c3TK|]"\}}}||rdn ||V#dSrPrB).0rGrMrIapplyr`s rD z&Segment.apply_style..s\)D&'D'<$$uuV}}gFFrCc3PK|] \}}}||rdn|r|zn|V!dSrPrB)rqrGrMrIr`rns rDrsz&Segment.apply_style..sr  *D&'#M5;Kfz11      rC)__add__)r`rmrHrnresult_segmentsrrs` ` @rD apply_stylezSegment.apply_styles&#  ME-<O       .=   OrCFrWcz|rttd|Sttd|S)a2Filter 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)filterrr)r`rmrWs rDfilter_controlzSegment.filter_controls=  @*Y//:: :z)44h?? ?rCc#Kg}|j}|D]g}d|jvrQ|jsJ|\}}}|rA|d\}} }|r||||| r |Vg}|j}|A\||h|r|VdSdS)aSplit 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. rkNappendrGrI partition r`rmrlr}rXrGrH__textnew_lines rD split_lineszSegment.split_liness! Gw|##GO#!(eQ-,0NN4,@,@)E8T2ss5%00111-" !!%-w  JJJJJ  rCc#Kg}|j}|D]i}d|jvrS|jsL|\}}}|rC|d\}} }|r||||| r|dfVg}|j}|C^||j|r|dfVdSdS)a2Split 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. rkTFNr|rs rDsplit_lines_terminatorzSegment.split_lines_terminators! Gw|##GO#!(eQ-,0NN4,@,@)E8T2ss5%00111-#Tl***!!%-w  -       rCTlengthpadinclude_new_linesc#Kg}|j}|j}|d} |D]} d| jvr| js{| \} } } | rr| d\}}} |r|||| |r>|||||}|r|| |V|| r|| |r|||||VdSdS)aSplit 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)rHrN)r}adjust_line_lengthrGrIr~clear)r`rmrrHrrrlr}rnew_line_segmentrXrG segment_stylerrr cropped_lines rDsplit_and_crop_lineszSegment.split_and_crop_lines2sV*! 33t99  Gw|##GO#)0&mQ %,0NN4,@,@)E8T:ss5-88999%'9'9 &3((( -B(//0@AAA****  %w  I$$T6CHHH H H H H H I IrCrlcztd|D}||kr$|r||d||z z|gz}n|dd}nw||krgg}|j}d}|D]X}|j} || z|ks|jr|||| z }*|\} } } t | ||z } ||| | n |dd}|S)aAdjust 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. c3$K|] }|jV dSrP)rN)rqrXs rDrsz-Segment.adjust_line_length..ts%BB''-BBBBBBrCr]Nr)sumr}rNrIr) r`rlrrHr line_lengthrr}rXsegment_lengthrGrrs rDrzSegment.adjust_line_lengthas&BBTBBBBB     #33sf{.B'CU#K#K"LL7 6 ! !H_FK  !(!4/&88GO8F7OOO>1KK-4*D-(v /CDDDF33t]33444AAAwHrCcHttfd|DS)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. c3:K|]\}}}||VdSrPrB)rqrGrHrI _cell_lens rDrsz*Segment.get_line_length..s7SS';tUG7S99T??SSSSSSrC)rr)r`rlrs @rDget_line_lengthzSegment.get_line_lengths- SSSStSSSSSSrClinescr|j|rtfd|Dnd}|t|fS)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. c3.K|]}|VdSrPrB)rqrlrs rDrsz$Segment.get_shape..s-@@$--@@@@@@rCr)rmaxr_)r`r max_widthrs @rD get_shapezSegment.get_shapesJ-DIPC@@@@%@@@@@@q 3u::&&rCwidthheight new_linesc< |pt|}|r|dzdzgn|dzg}|j |d|} fd|D|dd<t||kr)||g|t|z z|S)aSet 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]rkNc,g|]}|S))rHrB)rqrlrrHrs rD z%Segment.set_shape..s9   =A  tU% 8 8 8   rC)r_rextend) r`rrrrHr_heightblank shaped_linesrs ` ` @rD set_shapezSegment.set_shapes*&CJJ1: XSSut#U + + , ,C%KQV@W@W?X !3XgX       EJ   QQQ |  w & &   7S5F5F+F G H H HrCr`c|t|z }|s |ddS|d|}|r|d|zdz|n|d|z|}||gg|zz}|S)aAligns 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]rkr_r`rrrrHr extra_linesrs rD align_topzSegment.align_tops*s5zz)  8Ogvg2;XC%K$&...S5[RWAXAX% K// rCc|t|z }|s |ddS|d|}|r|d|zdz|n|d|z|}|gg|z|z}|S)aAligns 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]rkrrs rD align_bottomzSegment.align_bottoms*s5zz)  8Ogvg2;XC%K$&...S5[RWAXAX K'%/ rCc|t|z }|s |ddS|d|}|r|d|zdz|n|d|z|}|dz}||z } |gg|z|z|gg| zz}|S)aAligns 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]rkrr) r`rrrrHrrr top_lines bottom_liness rD align_middlezSegment.align_middles*s5zz)  8Ogvg2;XC%K$&...S5[RWAXAX1$ "Y.  I%-% L0HH rCc#Kt|} t|}n#t$rYdSwxYwt}|D]>}|j|jkr&|js||j|jz|j}8|V|}?|VdS)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)iternext StopIterationrFrHrIrG)r`rm iter_segments last_segmentrarXs rDsimplifyzSegment.simplify&sX   ..LL    FF $ ' 'G!W]227?2'x % 4l6H   #"""& s # 11c#K|D]@}|js|j|V|\}}}|||r|dndVAdS)zRemove all links from an iterable of styles. Args: segments (Iterable[Segment]): An iterable segments. Yields: Segment: Segments with link removed. N)rIrH update_link)r`rmrXrGrH_controls rD strip_linkszSegment.strip_linksAs  L LG L'-"7 (/%eXc$5 J 1 1$ 7 7 7dKKKKKK  L LrCc#:K|D]\}}}||d|VdS)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`rmrGrMrIs rD strip_styleszSegment.strip_stylesRsE&. + + !D&'#dD'** * * * * + +rCc#Ki}|D]J\}}}|r3||}| |j}|||<||||V;||d|VKdS)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`rmcacherGrHrIcolorless_styles rD remove_colorzSegment.remove_color_s%'$, / / D% /"'))E"2"2"*&+&9O#2E%Lc$999999c$g...... / /rCcutsc#Kg}|j}t|} t|d}|dkrdS|dkrngV$d}|j}|j} t } |D]} | \} } }| r|r|n || | z}||kr|| |}n||krK|| | V||}t|d}|dkr|r | VdSnm| ||z \}} | \} } }||| V||}t|d}|dkr|r | VdS| | VdS)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\Nr)r}rrrcopyrri)r`rmrsplit_segments add_segment iter_cutsrYrcsegments_clear segments_copyrrXrGrMrIend_posrds rDdividezSegment.divideus+-$+ JJ  y"%%CbyyaxxHHH  '-&+ # # # G$+ !D&'! !(C##cIIdOO.CS==K(((!Cc>>K((('-//)))"N$$$!Cy"--Cbyy)2"/-//111'.&9&9#)&D&DOFG,3)D&'K''''-//)))"N$$$C9b))"99%.+moo---FFC! FmoorC)rJrF)NNF)NTT)NT)NNF).r.r/r0r1str__annotations__rHr rrIr ControlCodepropertyr^rNrrRrTrUrW classmethodrrrgrirlr rwrzr rrrrrrrrrrrrrrrrrBrCrDrFrF<s]   III!E8E?!!!/3GXh{+ ,333 0S000X0v$(D(((X(Yy-9-3-5AU;V---[-^,s,u-A'B,,,,4["&&* &&9%&&UO & )  &&&[&P?D@@ *@8<@ ) @@@[@$8I#68DO;T[8  * %Y-. /   [ < "&"& ,I,I9%,I,I ,I  ,I  ,I $y/ ",I,I,I[,I\ "& **9o** *  * i ***[*X T4 ? Ts T T T[ T 'd4 ?3 'c3h ' ' '[ ' !%!% !!DO$!! !  !  ! d9o !!![!F  )_DO$      d9o [8  )_DO$      d9o [8  )_DO$      d9o [< 38K[4L8I#6L8I;NLLL[L  +HY$7 +HY %<<>>D=      } $ $ $ $ $ $ $ $ $rCrrrrrrJr) r.r/r0r1r rFrTrrrBrCrDrrs~##'!2#t#PT#### %  %+; %  % % % % % %rCrcPeZdZd deeededdfdZ dd ZdS) SegmentLinesFrrrJNc<t||_||_dS)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)rrr)rLrrs rDrzSegmentLines.__init__s%[[ "rCrrrrrc#K|jr1t}|jD]}|Ed{V|VdS|jD] }|Ed{V dSrP)rrFrlr)rLrrrrls rDrzSegmentLines.__rich_console__s > ||~~H          rCrr) r.r/r0r r rFrTrrrBrCrDrrsx # #htG}5 #$ #SW # # # #     +;         rCr__main__)r)Syntax)Textzfrom rich.console import Console console = Console() text = Text.from_markup("Hello, [bold magenta]World[/]!") console.print(text)zHello, [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)7enumr functoolsr itertoolsroperatorrtypingrrr r r r r rrrcellsrrrrrreprrrrHrrrrrrr^rrrFrrr. rich.console rich.syntaxr rich.textrcode from_markuprGruleprintrrender fragmentsrBrCrDrs!!!!!!                        $#######?>>>>>>>>>>'* + +uS#X &' +sC  "  { { { { { j{ {  { |%%%%%%%%2        2 z$$$$$$"""""" D 4 < = =DgiiG LL    MMg MM4555 MM&&xd;;;<<< MMOOO MM]W^^D))**I MM) MMOOO MMVWWW MM$ MM^;rC