o 7]hl]@sddlmZddlmZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZmZmZmZdd lmZmZdd lmZe r\dd lm Z m!Z!m"Z"ed Z#Gd ddeZ$eee$ee$e%fee$e%e%ffZ&eGdddeZ'GdddZ(GdddZ)e*dkre*dkrddl+m Z ddl,m-Z-ddl.m/Z/dZ0e/1dZ2e Ze3de4de4de4e-e0ddd e4e4d!e5e6e2Z7e4e7e4e4d"e4e2e4d#d$Sd$Sd$S)%)IntEnum) lru_cache) filterfalse) getLogger) attrgetter) TYPE_CHECKINGDictIterableList NamedTupleOptionalSequenceTupleTypeUnion)_is_single_cell_widthscell_lenget_character_cell_size set_cell_size)Result rich_repr)Style)ConsoleConsoleOptions RenderResultrichc@sLeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdS) 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_LINEr?r?:/usr/lib/python3/dist-packages/pip/_vendor/rich/segment.pyr"s"rc@seZdZUdZdZeed< dZee ed< dZ ee e ed< de fdd Zdefd d Zedefd d ZedefddZeeddddededfddZdededfddZedIddZe  dJdeddee dee dedfddZe dKdeddededfd d!Zededdeedfd"d#Ze  $ $dLdedd%edee d&ed'edeedf d(d)Z e  $dMd*edd%edee d&ededf d+d,Z!ed*eddefd-d.Z"ed/eeddeeeffd0d1Z#e   dNd/eedd2ed3eedee d4edeedf d5d6Z$e dKd7e%dd/eedd2ed3ede d4edeedfd8d9Z&e dKd7e%dd/eedd2ed3ede d4edeedfd:d;Z'e dKd7e%dd/eedd2ed3ede d4edeedfdd?Z)ededdedfd@dAZ*ededdedfdBdCZ+ededdedfdDdEZ,ededdFeedeedfdGdHZ-dS)OSegmentaA 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. textNstylecontrolreturnccsB|jV|jdur|jdur|jVdSdS|jV|jVdSN)rCrErDselfr?r?r@ __rich_repr__Ms    zSegment.__rich_repr__cCs t|jS)z#Check if the segment contains text.)boolrCrHr?r?r@__bool__Vs zSegment.__bool__cCs|jrdSt|jS)zGet cell length of segment.r)rErrCrHr?r?r@ cell_lengthZszSegment.cell_lengthcCs |jduS)z,Check if the segment contains control codes.N)rErHr?r?r@ is_control_s zSegment.is_controli@segmentcut)rArAc Cs,|\}}}t}|j}||kr||d||fSt}t||t|} |d| } t| } | |kr@|| ||||| d||fS| t|kr|| } | d7} | || 7} |d| } | |krn|| ||||| d||fS| |kr|| d| dd|||d|| d||fS| t|ksFdSdS)NrBr )rArMrintlenr) clsrOrPrCrDrE_SegmentrM cell_sizeposbeforecell_poscharr?r?r@ _split_cellsds6       zSegment._split_cellscCsb|\}}}t|r+|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. rBN)rrSrAr[)rIrPrCrDrEr?r?r@ split_cellss   zSegment.split_cellscCs|dS)zMake a new line segment. r?)rTr?r?r@linesz Segment.linesegments post_stylecs>|}|r|jfdd|D}rfdd|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.|]\}}}||r dn||VqdSrGr?.0rC_stylerE)applyrTr?r@ s  z&Segment.apply_style..c3s6|]\}}}||r dn|r|n|VqdSrGr?ra)rTr`r?r@res  )__add__)rTr_rDr`result_segmentsr?)rdrTr`r@ apply_styles    zSegment.apply_styleFrNcCs |r ttd|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. rE)filterrr)rTr_rNr?r?r@filter_controlszSegment.filter_controlc csg}|j}|D]3}d|jvr7|js7|\}}}|r6|d\}} }|r*||||| r4|Vg}|j}|sq||q|rC|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. r]N)appendrCrE partition) rTr_r^rkrOrCrD__textnew_liner?r?r@ split_liness(    zSegment.split_linesTlengthpadinclude_new_linesccsg}|j}|j}|d} |D]B} d| jvrM| jsM| \} }} | rL| d\} }} | r1||| ||rJ|||||d}|rB|| |V|dd=| s q|| q|r_|||||dVdSdS)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. r])rDrrN)rkadjust_line_lengthrCrErl)rTr_rqrDrrrsr^rkrtnew_line_segmentrOrCrmrnro cropped_liner?r?r@split_and_crop_liness4    zSegment.split_and_crop_linesr^c Cstdd|D}||kr%|r||d|||g}|S|dd}|S||krag}|j}d}|D],}|j} || |ks@|jrI|||| 7}q2|\} } } t| ||} ||| | |S|S|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|]}|jVqdSrG)rMrbrOr?r?r@re@sz-Segment.adjust_line_length..rQNr)sumrkrMrEr) rTr^rqrDrr line_lengthrorkrOsegment_lengthrC segment_stylermr?r?r@rt-s.    zSegment.adjust_line_lengthcsttfdd|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|]}|jVqdSrG)rCrx _cell_lenr?r@reesz*Segment.get_line_length..)rry)rTr^r?r}r@get_line_lengthZs zSegment.get_line_lengthlinescs0|j|rtfdd|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|]}|VqdSrGr?rbr^rr?r@rersz$Segment.get_shape..r)rmaxrS)rTr max_widthr?rr@ get_shapegs  zSegment.get_shapewidthheight new_linesc s|pt|}|r|ddgn|dg}|j|d|}fdd|D|dd<t||krD||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. rQr]Ncsg|] }|dqS))rDr?rrtrDrr?r@ sz%Segment.set_shape..)rSrtextend) rTrrrrDr_heightblank shaped_linesr?rr@ set_shapeus &  zSegment.set_shaperTcCs`|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. NrQr]rSrTrrrrDr extra_linesrr?r?r@ align_top   $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. NrQr]rrr?r?r@ align_bottomrzSegment.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. NrQr]rr) rTrrrrDrrr top_lines bottom_linesr?r?r@ align_middles   $zSegment.align_middleccsvt|}zt|}Wn tyYdSwt}|D]}|j|jkr0|js0||j|j|j}q|V|}q|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 StopIterationrArDrErC)rTr_ iter_segments last_segmentrUrOr?r?r@simplifys     zSegment.simplifyccsL|D] }|js |jdur|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)rErD update_link)rTr_rOrCrD_controlr?r?r@ strip_links s  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 Nr?)rTr_rCrcrEr?r?r@ strip_styless zSegment.strip_stylesccs\i}|D]&\}}}|r$||}|dur|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)rTr_cacherCrDrEcolorless_styler?r?r@ remove_color+s  zSegment.remove_colorcutsc csg}|j}t|} zt|}Wn tygYSw|dkr"ngVq d}|D]}|jr||j} | |kr?||| }nzi| |krr|||ddV|dd=| }Wzt|}Wq*tyq|rm|ddVYdSw|||\} }|| |ddV|dd=|}Wzt|}Wn3ty|r|ddVYdSwzt|}Wwty|r|ddVYYdSw|js/q*|ddVdS)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. TrN)rkrrrrCrMr\) rTr_rsplit_segments add_segment iter_cutsrPrWrOend_posrXr?r?r@divideAst            zSegment.divide)rFrA)NNF)NTT)NT)NNF).r,r-r.r/rCstr__annotations__rDr rrEr ControlCoderrJrKrLpropertyrRrMrN classmethodrrr[r\r^r rhrjr rprwrtrrrrrrrrrrrrr?r?r?r@rA;sD   $  (    . , $    #           rAc@sBeZdZdZddeededdfddZ  dddZdS)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. Fr_rrFNcCst||_||_dSrG)listr_r)rIr_rr?r?r@__init__s  zSegments.__init__consoleroptionsrrccs<|jrt}|jD]}|V|Vq dS|jEdHdSrG)rrAr^r_)rIrrr^rOr?r?r@__rich_console__s zSegments.__rich_console__rrrrrrFr) r,r-r.r/r rArKrrr?r?r?r@r{src@sBeZdZddeeededdfddZ   dd dZdS) SegmentLinesFrrrFNcCst||_||_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)rIrrr?r?r@rs  zSegmentLines.__init__rrrrrccsJ|jrt}|jD] }|EdH|Vq dS|jD]}|EdHqdSrG)rrAr^r)rIrrror^r?r?r@rs    zSegmentLines.__rich_console__rr) r,r-r.r r rArKrrr?r?r?r@rs  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_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)8enumr functoolsr itertoolsrloggingroperatorrtypingrrr r r r r rrrcellsrrrrreprrrrDrrrrrlogrrRrrArrr,pip._vendor.rich.consolepip._vendor.rich.syntaxrpip._vendor.rich.textrcode from_markuprCruleprintrrender fragmentsr?r?r?r@sl    0  C