U cf^@sddlmZddlmZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZmZmZmZmZdd lmZmZdd lmZe rdd l m!Z!m"Z"m#Z#ed Z$Gd ddeZ%eee%ee%ee&e'ffee%e&e&ffZ(eGdddeZ)GdddZ*GdddZ+e,dkrddl-m!Z!ddl.m/Z/ddl0m1Z1dZ2e13dZ4e!Z e 5de 6de 6de 6e/e2ddd e 6e 6d!e7e 8e4Z9e 6e9e 6e 6d"e 6e4e 6d#d$S)%)IntEnum) lru_cache) filterfalse) getLogger) attrgetter) TYPE_CHECKINGDictIterableList NamedTupleOptionalSequenceTupleTypeUnion)_is_single_cell_widthscached_cell_lencell_lenget_character_cell_size set_cell_size)Result rich_repr)Style)ConsoleConsoleOptions RenderResultrichc@sPeZdZdZdZdZdZdZdZdZ 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_TITLErBrBF/opt/hc_python/lib/python3.8/site-packages/pip/_vendor/rich/segment.pyr#s"rc @s*eZdZUdZeed<dZeeed<dZ ee e ed<e e dddZedd d Zedd d Ze edd dZeedde eddddZe eddddZeddddZedEedeeeeeddddZedFedeeddddZeedeedd d!d"ZedGede eeeeeedd$d%d&ZedHede eeeedd'd(d)Z eede d*d+d,Z!eeedee e fd-d.d/Z"edIeede ee eeeeedd0d1d2Z#edJe$deede e eeeedd3d4d5Z%edKe$deede e eeeedd3d6d7Z&edLe$deede e eeeedd3d8d9Z'eededd d:d;Z(eededd dd?Z*eededd d@dAZ+eedee eeddBdCdDZ,dS)MSegmentaA 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. textNstylecontrol)returncCs|\}}}|rdSt|S)zzThe number of terminal cells required to display self.text. Returns: int: A number of cells. r)r)selfrE_stylerGrBrBrC cell_lengthQs zSegment.cell_lengthccs:|jV|jdkr&|jdk r6|jVn|jV|jVdSN)rErGrFrIrBrBrC __rich_repr__[s    zSegment.__rich_repr__cCs t|jS)z#Check if the segment contains text.)boolrErMrBrBrC__bool__dszSegment.__bool__cCs |jdk S)z,Check if the segment contains control codes.N)rGrMrBrBrC is_controlhszSegment.is_controli@)rDrD)segmentcutrHc Cs,|\}}}t}|j}||kr,||d||fSt}t||t|d} |d| } t| } | |kr|| ||||| d||fS| t|kr || } | d7} | || 7} |d| } | |kr|| ||||| d||fS| |kr|| d| dd|||d|| d||fSqtddS)Nr zWill never reach here)rDrKrintlenrAssertionError) clsrRrSrErFrG_SegmentrK cell_sizeposbeforecell_poscharrBrBrC _split_cellsms6      zSegment._split_cells)rSrHcCsb|\}}}t|rV|t|kr.|td||fSt|d|||t||d||fS|||S)a0Split 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. rTN)rrWrDr`)rIrSrErFrGrBrBrC split_cellss  zSegment.split_cellscCs|dS)zMake a new line segment. rB)rYrBrBrClinesz Segment.line)segmentsrF post_stylerHcs>|}|r"|jfdd|D}r:fdd|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). c3s,|]$\}}}||rdn||VqdSrLrB.0rErJrG)applyrYrBrC sz&Segment.apply_style..c3s4|],\}}}||rdn|r$|n|VqdSrLrBrf)rYrerBrCris )__add__)rYrdrFreresult_segmentsrB)rhrYrerC apply_styles   zSegment.apply_styleF)rdrQrHcCs$|rttd|Sttd|SdS)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. rGN)filterrr)rYrdrQrBrBrCfilter_controlszSegment.filter_control)rdrHc csg}|j}|D]d}d|jkrj|jsj|\}}}|rr|d\}} }|rR||||| r,|Vg}|j}q,q||q|r~|VdS)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. rbN)appendrErG partition) rYrdrcrorRrErF__textnew_linerBrBrC split_liness    zSegment.split_linesT)rdlengthrFpadinclude_new_linesrHccsg}|j}|j}|d} |D]} d| jkr| js| \} } } | r| d\}}} |r`|||| |r:|||||d}|r|| |V|q:q|| q|r|||||dVdS)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. rb)rFrvN)roadjust_line_lengthrErGrpclear)rYrdrurFrvrwrcrorxnew_line_segmentrRrE segment_stylerqrrrs cropped_linerBrBrCsplit_and_crop_liness2    zSegment.split_and_crop_lines)rcrurFrvrHc Cstdd|D}||krF|r8||d|||g}q|dd}n||krg}|j}d}|D]X}|j} || |ks||jr|||| 7}q`|\} } } t| ||} ||| | qq`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. css|] }|jVqdSrL)rK)rgrRrBrBrCriJsz-Segment.adjust_line_length..rUNr)sumrorKrGr) rYrcrurFrv line_lengthrsrorRsegment_lengthrEr{rqrBrBrCrx7s(   zSegment.adjust_line_length)rcrHcsttfdd|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. c3s |]\}}}|s|VqdSrLrB)rgrErFrG _cell_lenrBrCriosz*Segment.get_line_length..)rr~)rYrcrBrrCget_line_lengthds zSegment.get_line_length)linesrHcs0|j|r tfdd|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. c3s|]}|VqdSrLrBrgrcrrBrCri|sz$Segment.get_shape..r)rmaxrW)rYr max_widthrBrrC get_shapeqs zSegment.get_shape)rwidthheightrF new_linesrHc s|p t|}|r$|ddgn|dg}|j|d|}fdd|D|dd<t||kr||g|t||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. rUrbNcsg|]}|dqS))rFrBrrxrFrrBrC sz%Segment.set_shape..)rWrxextend) rYrrrrFr_heightblank shaped_linesrBrrC set_shapes &  zSegment.set_shape)rYrrrrFrrHcCs`|t|}|s|ddS|d|}|r>|d|d|n |d||}||gg|}|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. NrUrbrWrYrrrrFr extra_linesrrBrBrC align_tops   $zSegment.align_topcCs`|t|}|s|ddS|d|}|r>|d|d|n |d||}|gg||}|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. NrUrbrrrBrBrC align_bottoms   $zSegment.align_bottomc Cs||t|}|s|ddS|d|}|r>|d|d|n |d||}|d}||} |gg|||gg| }|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. NrUrbrr) rYrrrrFrrr top_lines bottom_linesrBrBrC align_middles   $zSegment.align_middleccsvt|}z t|}Wntk r*YdSXt}|D]6}|j|jkr`|js`||j|j|j}q4|V|}q4|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 StopIterationrDrFrGrE)rYrd iter_segments last_segmentrZrRrBrBrCsimplifys   zSegment.simplifyccsJ|D]@}|js|jdkr |Vq|\}}}|||r<|dndVqdS)zRemove all links from an iterable of styles. Args: segments (Iterable[Segment]): An iterable segments. Yields: Segment: Segments with link removed. N)rGrF update_link)rYrdrRrErF_controlrBrBrC strip_linkss  zSegment.strip_linksccs"|D]\}}}||d|VqdS)zRemove all styles from an iterable of segments. Args: segments (Iterable[Segment]): An iterable segments. Yields: Segment: Segments with styles replace with None NrB)rYrdrErJrGrBrBrC strip_styles(s zSegment.strip_stylesccsZi}|D]L\}}}|rF||}|dkr6|j}|||<||||Vq||d|VqdS)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)rYrdcacherErFrGcolorless_stylerBrBrC remove_color5s  zSegment.remove_color)rdcutsrHccsDg}|j}t|}t|d}|dkr(gS|dkr2q:gVqd}|j}|j} t} |D]} | \} } }| rR|rl|n || | }||kr|| |}qR||kr|| | V||}t|d}|dkr|r| VdSqRn6| ||\}} | \} } }||| V||}t|d}|dkr`|r.| VdSq`qR| 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. rN)rorrrycopyrra)rYrdrsplit_segments add_segment iter_cutsrSr\segments_clear segments_copyrrRrErJrGend_posr]rBrBrCdivideKsX       zSegment.divide)NN)F)NTT)NT)NNF)F)F)F)-r.r/r0r1str__annotations__rFr rrGr ControlCodepropertyrVrKrrNrOrPrQ classmethodrrr`rarcr rlrnr rtr}rxrrrrrrrrrrrrrBrBrBrCrD?s    %( ., "   #        rDc@s:eZdZdZd eeeddddZddd d d d ZdS)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)rdrrHcCst||_||_dSrL)listrdr)rIrdrrBrBrC__init__s zSegments.__init__rrrconsoleoptionsrHccs8|jr(t}|jD]}|V|Vqn |jEdHdSrL)rrDrcrd)rIrrrcrRrBrBrC__rich_console__s   zSegments.__rich_console__)F) r.r/r0r1r rDrOrrrBrBrBrCrs rc@s:eZdZd eeeeddddZdddd d d ZdS) SegmentLinesFN)rrrHcCst||_||_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)rIrrrBrBrCrs zSegmentLines.__init__rrrrccsF|jr,t}|jD]}|EdH|Vqn|jD]}|EdHq2dSrL)rrDrcr)rIrrrsrcrBrBrCrs    zSegmentLines.__rich_console__)F) r.r/r0r r rDrOrrrBrBrBrCrs  r__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):enumr functoolsr itertoolsrloggingroperatorrtypingrrr r r r r rrrcellsrrrrrreprrrrFrrrrrlogrrVrrrDrrr.pip._vendor.rich.consoleZpip._vendor.rich.syntaxrpip._vendor.rich.textrcode from_markuprEruleprintrrender fragmentsrBrBrBrCsf     0   U