o �J�h�`�@s�ddlmZddlmZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZmZmZmZmZdd lmZmZdd lmZe r^dd l m!Z!m"Z"m#Z#ed �Z$Gd d�de�Z%eee%ee%ee&e'ffee%e&e&ffZ(e�Gdd�de��Z)Gdd�d�Z*Gdd�d�Z+e,dkr�ddl-m!Z!ddl.m/Z/ddl0m1Z1dZ2e1�3d�Z4e!�Z e �5d�e �6d�e �6d�e �6e/e2ddd ��e �6�e �6d!�e7e �8e4��Z9e �6e9�e �6�e �6d"�e �6e4�e �6d#�d$Sd$S)%�)�IntEnum)� lru_cache)� filterfalse)� getLogger)� 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� RenderResult�richc@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__ZBELLZCARRIAGE_RETURN�HOMEZCLEARZ SHOW_CURSORZ HIDE_CURSORZENABLE_ALT_SCREENZDISABLE_ALT_SCREENZ CURSOR_UPZ CURSOR_DOWNZCURSOR_FORWARDZCURSOR_BACKWARDZCURSOR_MOVE_TO_COLUMNZCURSOR_MOVE_TOZ ERASE_IN_LINEZSET_WINDOW_TITLE�r3r3�JC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\rich\segment.pyr#s$rc@s�eZdZUdZeed<dZeeed<dZ ee e ed<e de fdd��Zdefd d �Zdefd d �Ze defd d��Zeed�ddde dedfdd���Zde dedfdd�ZedHdd��Ze  dIdeddeedeededfdd��Ze dJdeddededfdd ��Zededdeedfd!d"��Ze  # #dKdedd$e deed%ed&edeedf d'd(��Ze  #dLd)edd$e deed%ededf d*d+��Z ed)edde fd,d-��Z!ed.eeddee e ffd/d0��Z"e   dMd.eedd1e d2ee deed3edeedf d4d5��Z#e dJd6e$dd.eedd1e d2e ded3edeedfd7d8��Z%e dJd6e$dd.eedd1e d2e ded3edeedfd9d:��Z&e dJd6e$dd.eedd1e d2e ded3edeedfd;d<��Z'ededdedfd=d>��Z(ededdedfd?d@��Z)ededdedfdAdB��Z*ededdedfdCdD��Z+ededdEee deedfdFdG��Z,dS)N�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�returncCs|\}}}|r dSt|�S)zzThe number of terminal cells required to display self.text. Returns: int: A number of cells. r)r)�selfr6�_styler8r3r3r4� cell_lengthQs zSegment.cell_lengthccsB�|jV|jdur|jdur|jVdSdS|jV|jVdS�N)r6r8r7�r:r3r3r4� __rich_repr__[s�   � zSegment.__rich_repr__cCs t|j�S)z#Check if the segment contains text.)�boolr6r>r3r3r4�__bool__ds zSegment.__bool__cCs |jduS)z,Check if the segment contains control codes.N)r8r>r3r3r4� is_controlhs zSegment.is_controli@�segment�cut)r5r5c Cs>|\}}}t}|j}||kr||d||�fSt}t||t|��} |d| �} t| �} | |} | sC|| ||�||| d�||�fS| dkri||| �dkri||d| �d||�|d|| dd�||�fS| dkr�||| d�dkr�||d| d�d||�|d|| d�||�fS| |kr�| d7} n| d8} q#)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. �TN�����r� r)r5r<r�int�lenr) �clsrCrDr6r7r8�_Segmentr<� cell_size�pos�beforeZcell_posZout_byr3r3r4� _split_cellsms6   ��� �zSegment._split_cellscCsn|\}}}|dks J�t|�r1|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. rrEN)rrIr5rO)r:rDr6r7r8r3r3r4� split_cells�s  � zSegment.split_cellscCs|d�S)zMake a new line segment.� r3)rJr3r3r4�line�sz Segment.line�segments� post_stylecs>|}|r|j���fdd�|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.�|]\}}}�||r dn�|�|�VqdSr=r3��.0r6r;r8)�applyrJr3r4� <genexpr>�s �� �z&Segment.apply_style.<locals>.<genexpr>c3s6�|]\}}}�||r dn|r|�n�|�VqdSr=r3rU)rJrTr3r4rX�s� ��� �)�__add__)rJrSr7rTZresult_segmentsr3)rWrJrTr4� apply_style�s �  � zSegment.apply_styleFrBcCs |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. r8)�filterrr)rJrSrBr3r3r4�filter_control�szSegment.filter_controlc cs��g}|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. rQN)�appendr6r8� partition) rJrSrRr]rCr6r7�_�_text�new_liner3r3r4� split_lines�s(�  ��  �zSegment.split_linesT�length�pad�include_new_linesccs��g}|j}|j}|d�} |D]A} d| jvrL| jsL| \} } } | rK| �d�\}}} |r1|||| ��|rI|||||d�}|rB|�| �|V|��| s q|| �q|r^|||||d�VdSdS)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. rQ)r7rdN)r]�adjust_line_lengthr6r8r^�clear)rJrSrcr7rdrerRr]rfZnew_line_segmentrCr6� segment_styler_r`raZ cropped_liner3r3r4�split_and_crop_liness4� � �� �zSegment.split_and_crop_linesrRc Cs�tdd�|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�|]}|jVqdSr=)r<)rVrCr3r3r4rXXs�z-Segment.adjust_line_length.<locals>.<genexpr>rGNr)�sumr]r<r8r) rJrRrcr7rdZ line_lengthrar]rCZsegment_lengthr6rhr_r3r3r4rfEs. ��   �zSegment.adjust_line_lengthcst�t�fdd�|D��S)z�Get 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�|�VqdSr=r3)rVr6r7r8�� _cell_lenr3r4rX}s� z*Segment.get_line_length.<locals>.<genexpr>)rrj)rJrRr3rkr4�get_line_lengthrs zSegment.get_line_length�linescs0|j�|rt�fdd�|D��nd}|t|�fS)z�Get 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�|]}�|�VqdSr=r3�rVrR�rmr3r4rX�s�z$Segment.get_shape.<locals>.<genexpr>r)rm�maxrI)rJrn� max_widthr3rpr4� get_shapes  zSegment.get_shape�width�height� new_linesc s�|pt|�}|r|d�d��gn|d���g}|j�|d|�}���fdd�|D�|dd�<t|�|krD|�|g|t|��|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. new_lines (bool, optional): Padded lines should include " ". Defaults to False. Returns: List[List[Segment]]: New list of lines. rGrQNcsg|] }�|��d��qS))r7r3ro�rfr7rtr3r4� <listcomp>�s�z%Segment.set_shape.<locals>.<listcomp>)rIrf�extend) rJrnrtrur7rv�_height�blankZ shaped_linesr3rwr4� set_shape�s &� � zSegment.set_shaperJcCs`|t|�}|s|dd�S|d|�}|r|d|d|�n|d||�}||gg|}|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. NrGrQ�rI�rJrnrtrur7rv� extra_linesr{r3r3r4� align_top��   $zSegment.align_topcCs`|t|�}|s|dd�S|d|�}|r|d|d|�n|d||�}|gg||}|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. NrGrQr}r~r3r3r4� align_bottom�r�zSegment.align_bottomc Cs||t|�}|s|dd�S|d|�}|r|d|d|�n|d||�}|d}||} |gg|||gg| }|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. NrGrQrr}) rJrnrtrur7rvrr{Z top_linesZ bottom_linesr3r3r4� align_middle�s   $zSegment.align_middleccsv�t|�}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)�iter�next� StopIterationr5r7r8r6)rJrS� iter_segmentsZ last_segmentrKrCr3r3r4�simplify s �   �� zSegment.simplifyccsL�|D] }|js |jdur|Vq|\}}}|||r|�d�nd�VqdS)z�Remove all links from an iterable of styles. Args: segments (Iterable[Segment]): An iterable segments. Yields: Segment: Segments with link removed. N)r8r7� update_link)rJrSrCr6r7�_controlr3r3r4� strip_links%s�  �zSegment.strip_linksccs$�|D] \}}}||d|�VqdS)z�Remove all styles from an iterable of segments. Args: segments (Iterable[Segment]): An iterable segments. Yields: Segment: Segments with styles replace with None Nr3)rJrSr6r;r8r3r3r4� strip_styles6s� �zSegment.strip_stylesccs\�i}|D]&\}}}|r$|�|�}|dur|j}|||<||||�Vq||d|�VqdS)z�Remove all color from an iterable of segments. Args: segments (Iterable[Segment]): An iterable segments. Yields: Segment: Segments with colorless style. N)�get� without_color)rJrS�cacher6r7r8Zcolorless_styler3r3r4� remove_colorCs�  �zSegment.remove_color�cutsccsF�g}|j}t|�} t|d�}|dkrdS|dkrngVq d}|j}|j} t} |D]q} | \} } }| r�|r8|n|| | �}||krI|| �|}nS||krm|| �| �V|�|}t|d�}|dkrl|ri| �VdSn/| �||�\}} | \} } }||�| �V|�|}t|d�}|dkr�|r�| �VdS| s4q+| �VdS)z�Divides 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. TrFNr)r]r�r�rg�copyrrP)rJrSr�Zsplit_segmentsZ add_segmentZ iter_cutsrDrMZsegments_clearZ segments_copyrlrCr6r;r8Zend_posrNr3r3r4�divideYsb�  �    �� #zSegment.divide)r9r5)NN�F)NTT)NT)NNF)-r.r/r0r1�str�__annotations__r7r rr8r � ControlCode�propertyrHr<rr?r@rArB� classmethodrrrOrPrRr rZr\r rbrirfrmrsr|rr�r�r�r�r�r�r�r�r3r3r3r4r5?s>    / ���� �(��� � ������  �.����� �, $ � �����  �#�� �����  ��� �����  ��� �����  � �� �r5c@sBeZdZdZddeededdfdd�Z  ddd�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. FrSrvr9NcCst|�|_||_dSr=)�listrSrv)r:rSrvr3r3r4�__init__�s  zSegments.__init__�consoler�optionsrrccs<�|jrt��}|jD]}|V|Vq dS|jEdHdSr=)rvr5rRrS)r:r�r�rRrCr3r3r4�__rich_console__�s� �zSegments.__rich_console__r��r�rr�rr9r) r.r/r0r1r r5r@r�r�r3r3r3r4r��s���r�c@sBeZdZddeeededdfdd�Z   dd d�ZdS)� SegmentLinesFrnrvr9NcCst|�|_||_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)r�rnrv)r:rnrvr3r3r4r��s  zSegmentLines.__init__r�rr�rrccsJ�|jrt��}|jD] }|EdH|Vq dS|jD]}|EdHqdSr=)rvr5rRrn)r:r�r�rarRr3r3r4r��s�  �  �zSegmentLines.__rich_console__r�r�) r.r/r0r r r5r@r�r�r3r3r3r4r��s  ���r��__main__)r)�Syntax)�Textz�from 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)Z 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� itertoolsr�loggingr�operatorr�typingrrr r r r r rrr�cellsrrrrr�reprrrr7rr�rrr�logrrHr�r�r5r�r�r.� rich.console� rich.syntaxr�� rich.textr��code� from_markupr6�rule�printr��render� fragmentsr3r3r3r4�<module>sj    0   ��c     � �   ��
Memory