o �J�h���@sJUddlZddlmZmZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZddlmZddlmZdd lmZdd lmZdd lmZmZdd lm Z dd l!m"Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,m-Z-e r�ddl.m/Z/m0Z0m1Z1m2Z2dZ3de4d<dZ5de4d<e�6d�Z7ee8dfZ9 e e8gee-fZ:Gdd�de�Z;Gdd�de&�Z<e=dk�r#dd l>m/Z/e<d!�Z?e?�@d"gd#�e?�@d$gd%�e/�Z.e.�Ad&�e.jBe?d'd(�e.�B�e.�Ad)�e.jBe?d*d+d,�e.�B�e.�Ad-�e.jBe?d.d/d,�e.�B�e.�Ad0�e.jBe?d1d2d,�e.�B�dSdS)3�N)�partial�reduce)�gcd)� itemgetter) � TYPE_CHECKING�Any�Callable�Dict�Iterable�List� NamedTuple�Optional�Pattern�Tuple�Union�)� loop_last)� pick_bool)� divide_line)� AlignMethod)�cell_len� set_cell_size)�Lines)�strip_control_codes)� EmojiVariant)� JupyterMixin)� Measurement)�Segment)�Style� StyleType)�Console�ConsoleOptions� JustifyMethod�OverflowMethod�defaultr"�DEFAULT_JUSTIFY�foldr#�DEFAULT_OVERFLOWz\s+$�Textc@s�eZdZUdZeed< eed< eeefed< defdd�Z de fdd �Z d ede de dffd d �Zd eddfd d�Zd eddfdd�Zdeddfdd�ZdS)�Spanz A marked up region in some text.�start�end�style�returncCsd|j�d|j�d|j�d�S)NzSpan(z, �))r*r+r,��self�r1�GC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\rich\text.py�__repr__9�z Span.__repr__cCs |j|jkS�N)r+r*r/r1r1r2�__bool__<s z Span.__bool__�offsetcCsV||jkr |dfS||jkr|dfS|\}}}t|t||�|�}t|j||�}||fS)z)Split a span in to 2 from a given offset.N)r*r+r)�min)r0r7r*r+r,Zspan1Zspan2r1r1r2�split?s   z Span.splitcCs|\}}}t|||||�S)z�Move start and end by a given offset. Args: offset (int): Number of characters to add to start and end. Returns: TextSpan: A new TextSpan with adjusted position. �r)�r0r7r*r+r,r1r1r2�moveLs z Span.movecCs(|\}}}||kr |St|t||�|�S)z�Crop the span at the given offset. Args: offset (int): A value between start and end. Returns: Span: A new (possibly smaller) span. )r)r8r;r1r1r2� right_cropXs zSpan.right_crop�cellscCs"|r|\}}}t||||�S|S)z�Extend the span by the given number of cells. Args: cells (int): Additional space to add to end of span. Returns: Span: A span. r:)r0r>r*r+r,r1r1r2�extendfs z Span.extendN)�__name__� __module__� __qualname__�__doc__�int�__annotations__r�strrr3�boolr6rr r9r<r=r?r1r1r1r2r)/s   r)c@s�eZdZdZgd�Z  d�ddddddd�dedeeefd ed d ed d ee dedee dee e ddfdd�Z de fdd�Zde fdd�Zdefdd�Zdefdd�Zdeddfdd�Zdede fdd �Zdede fd!d"�Zd#ee efddfd$d%�Zede fd&d'��Zedefd(d)��Zedd*ddddd+�dedeeefd,e d-eed ed d ed deddfd.d/��Zedddddd0d1�dedeeefd ed d ed d ee dedee ddfd2d3��Ze d�ddd4�dede d ed d ed ddf d5d6��Z!edddddd0dd7�d8eede"ee ffdeeefd ed d ed d ee dede d9ee#eefddfd:d;��Z$edefd<d=��Z%e%j&d>eddfd?d=��Z%ede e fd@dA��Z'e'j&de e ddfdBdA��Z'd�dCeddfdDdE�Z(d�dFdG�Z) H d�deeefdIe dee ddfdJdK�Z* H d�deeefdIe dee ddfdLdM�Z+ d�d9e#eefdIe dee ddfdNdO�Z,d�d9ee#eefdPeddfdQdR�Z-dSeddfdTdU�Z.dVdWdXe defdYdZ�Z/d[e ddfd\d]�Z0 d�dd^�d_ee1eefdeee2e fd`ede fdadb�Z3d*dc�dde4edeeefdee de fdfdg�Z5d�dhdi�Z6dje ddfdkdl�Z7dme ddfdndo�Z8dVdWdpdqde4e9fdrds�Z:dVdWdpdqde;fdtdu�Z<d�dVdWdede4dvfdwdx�Z=dye4dddfdzd{�Z>d�dee ddfd|d}�Z?dd~d�d�e d ed d�e ddfd�d��Z@d�d�d��ZAd�d�e d�eddfd�d��ZBd�d�e d�eddfd�d��ZCd�d�e d�eddfd�d��ZDd�d�eEd�e d�eddfd�d��ZF d�dedefdeeed�fddfd�d��ZGd�d�d��ZHd�e4e"eee fddfd�d��ZId�d�d��ZJ d�d~d~d��d�ed�e d�e deKfd�d��ZLd�e4e deKfd�d��ZMd�d�e ddfd�d��ZNddd0dd��dVdWd�e d ed d ed de d ee deKfd�d��ZOd�e deKfd�d��ZPde fd�d��ZQ d�d�d�d��d�ee d�ede ddfd�d��ZRdS)�r(a�Text with color / style. Args: text (str, optional): Default unstyled text. Defaults to "". style (Union[str, Style], optional): Base style for text. Defaults to "". justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None. no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None. end (str, optional): Character to end text with. Defaults to "\\n". tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None. spans (List[Span], optional). A list of predefined style spans. Defaults to None. ) �_textr,�justify�overflow�no_wrapr+�tab_size�_spans�_length�N� )rIrJrKr+rL�spans�textr,rIr"rJr#rKr+rLrQr-c CsLt|�} | g|_||_||_||_||_||_||_|pg|_t | �|_ dSr5) rrHr,rIrJrKr+rLrM�lenrN) r0rRr,rIrJrKr+rLrQ�sanitized_textr1r1r2�__init__�s  z Text.__init__cC�|jSr5)rNr/r1r1r2�__len__��z Text.__len__cC� t|j�Sr5)rGrNr/r1r1r2r6�s z Text.__bool__cCrVr5��plainr/r1r1r2�__str__�rXz Text.__str__cCsd|j�d|j�d|j�d�S)Nz<text � �>)r[rMr,r/r1r1r2r3�r4z Text.__repr__�othercCs(t|ttf�r|��}|�|�|StSr5)� isinstancerFr(�copy�append�NotImplemented)r0r_�resultr1r1r2�__add__�s  z Text.__add__cCs&t|t�stS|j|jko|j|jkSr5)r`r(rcr[rM�r0r_r1r1r2�__eq__�s z Text.__eq__cCs.t|t�r ||jvSt|t�r|j|jvSdS)NF)r`rFr[r(rfr1r1r2� __contains__�s    zText.__contains__�slicecsddtddf�fdd� }t|t�r||�S|�t�j��\}}}|dkr.��||g�}|dStd��)Nr7r-r(cs.t�t�j���fdd��jD�dd�}|S)Ncs6g|]\}}}|�kr|krnn�dd|��qS)rrr1��.0r*r+r,��_Spanr7r1r2� <listcomp>�s  �z9Text.__getitem__.<locals>.get_text_at.<locals>.<listcomp>rO)rQr+)r)r(r[rM)r7rRr/rlr2� get_text_at�s �� z%Text.__getitem__.<locals>.get_text_atrz%slices with step!=1 are not supported)rDr`�indicesrSr[�divide� TypeError)r0riror*�stop�step�linesr1r/r2� __getitem__�s zText.__getitem__cCrY)z5Get the number of cells required to render this text.)rr[r/r1r1r2r�s z Text.cell_lenc Cs�ddlm}g}|j}dd|jfgdd�|jD��dd�|jD��t|�d|jf�}|jtdd�d �d}|j}|D]'\}}} ||krO|||||���|}| ra||rZd | �d �nd | �d ��q:d � |�} | S)z~Get console markup to render this Text. Returns: str: A string potentially creating markup tags. r)�escaperFcs��|] }|jd|jfVqdS�FN)r*r,�rk�spanr1r1r2� <genexpr>���zText.markup.<locals>.<genexpr>csrx�TN)r+r,rzr1r1r2r|�r}T��keyz[/�]�[rO) �markuprwr[r,rMrS�sortrrb�join) r0rw�outputr[Z markup_spansZpositionrbr7�closingr,r�r1r1r2r��s.  ���� � z Text.markupT)r,�emoji� emoji_variantrIrJr+r�r�c Cs2ddlm}|||||d�} || _|| _|| _| S)a�Create Text instance from markup. Args: text (str): A string containing console markup. style (Union[str, Style], optional): Base style for text. Defaults to "". emoji (bool, optional): Also render emoji code. Defaults to True. emoji_variant (str, optional): Optional emoji variant, either "text" or "emoji". Defaults to None. justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None. end (str, optional): Character to end text with. Defaults to "\\n". Returns: Text: A Text instance with markup rendered. r)�render)r�r�)r�r�rIrJr+) �clsrRr,r�r�rIrJr+r�Z rendered_textr1r1r2� from_markups zText.from_markup��r,rIrJrKr+rLc CsFddlm}td||||||d�} |�} | �dd�| �|�D��} | S)a�Create a Text object from a string containing ANSI escape codes. Args: text (str): A string containing escape codes. style (Union[str, Style], optional): Base style for text. Defaults to "". justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None. no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None. end (str, optional): Character to end text with. Defaults to "\\n". tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None. r)� AnsiDecoderrP)rIrJrKr+rLr,css�|]}|VqdSr5r1�rk�liner1r1r2r|Hs�z!Text.from_ansi.<locals>.<genexpr>)�ansir�r(r��decode) r�rRr,rIrJrKr+rLr�Zjoiner�decoderrdr1r1r2� from_ansi%s � zText.from_ansi�rIrJcCs||||d�}|�|�|S)aaConstruct a Text instance with a pre-applied styled. A style applied in this way won't be used to pad the text when it is justified. Args: text (str): A string containing console markup. style (Union[str, Style]): Style to apply to the text. Defaults to "". justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None. Returns: Text: A text instance with a style applied to the entire string. r�)�stylize)r�rRr,rIrJZ styled_textr1r1r2�styledKs z Text.styled)r,rIrJrKr+rL�meta�partsr�c GsZ|||||||d�} | j} t} |D]} t| | tf�r| | �q| | �q|r+| �|�| S)a�Construct a text instance by combining a sequence of strings with optional styles. The positional arguments should be either strings, or a tuple of string + style. Args: style (Union[str, Style], optional): Base style for text. Defaults to "". justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None. no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None. end (str, optional): Character to end text with. Defaults to "\\n". tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to None. meta (Dict[str, Any], optional). Meta data to apply to text, or None for no meta data. Default to None Returns: Text: A new text instance. r�)rbr(r`rF� apply_meta) r�r,rIrJrKr+rLr�r�rRrb�_Text�partr1r1r2�assembleds"�   z Text.assemblecCs0t|j�dkrd�|j�g|jdd�<|jdS)z Get the text as a single string.rrONr)rSrHr�r/r1r1r2r[�s z Text.plain�new_textcCsP||jkr$t|�}|g|jdd�<|j}t|�|_||jkr&|��dSdSdS)zSet the text to a new value.N)r[rrHrNrS� _trim_spans)r0r�rTZ old_lengthr1r1r2r[�s    �cCrV)z.Get a reference to the internal list of spans.�rMr/r1r1r2rQ�sz Text.spanscCs|dd�|_dS)z Set spans.Nr�)r0rQr1r1r2rQ�sr[c Cs&t||j|j|j|j|j|jd�}|S)zNReturn a new Text instance with copied metadata (but not the string or spans).r�)r(r,rIrJrKr+rL)r0r[� copy_selfr1r1r2� blank_copy�s� zText.blank_copyc Cs8t|j|j|j|j|j|j|jd�}|j|jdd�<|S)zReturn a copy of this instance.r�N) r(r[r,rIrJrKr+rLrM)r0r�r1r1r2ra�s� z Text.copyrr*cCsn|r5t|�}|dkr||}|dur|}|dkr||}||ks$||kr&dS|j�t|t||�|��dSdS)azApply a style to the text, or a portion of the text. Args: style (Union[str, Style]): Style instance or style definition to apply. start (int): Start offset (negative indexing is supported). Defaults to 0. end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None. rN)rSrMrbr)r8�r0r,r*r+�lengthr1r1r2r��s �z Text.stylizecCsp|r6t|�}|dkr||}|dur|}|dkr||}||ks$||kr&dS|j�dt|t||�|��dSdS)a�Apply a style to the text, or a portion of the text. Styles will be applied before other styles already present. Args: style (Union[str, Style]): Style instance or style definition to apply. start (int): Start offset (negative indexing is supported). Defaults to 0. end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None. rN)rSrM�insertr)r8r�r1r1r2�stylize_before�s  �zText.stylize_beforecCst�|�}|j|||d�dS)agApply metadata to the text, or a portion of the text. Args: meta (Dict[str, Any]): A dict of meta information. start (int): Start offset (negative indexing is supported). Defaults to 0. end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None. )r*r+N)r� from_metar�)r0r�r*r+r,r1r1r2r��s zText.apply_meta�handlerscKs<|durin|}|�dd�|��D��|�t�|��|S)a�Apply event handlers (used by Textual project). Example: >>> from rich.text import Text >>> text = Text("hello world") >>> text.on(click="view.toggle('world')") Args: meta (Dict[str, Any]): Mapping of meta information. **handlers: Keyword args are prefixed with "@" to defined handlers. Returns: Text: Self is returned to method may be chained. NcSsi|] \}}d|��|�qS)�@r1)rkr��valuer1r1r2� <dictcomp>�zText.on.<locals>.<dictcomp>)�update�itemsr�rr�)r0r�r�r1r1r2�on szText.on�suffixcCs"|j�|�r|�t|��dSdS)zaRemove a suffix if it exists. Args: suffix (str): Suffix to remove. N)r[�endswithr=rS)r0r�r1r1r2� remove_suffixs �zText.remove_suffix�consoler r7cCsf|dkr t|�|}|j}||j���}|jD]\}}}||kr&|kr0nq|||dd�7}q|S)a Get the style of a character at give offset. Args: console (~Console): Console where text will be rendered. offset (int): Offset in to text (negative indexing supported) Returns: Style: A Style instance. rrO�r$)rS� get_styler,rarM)r0r�r7r�r,r*r+Z span_styler1r1r2�get_style_at_offset(s �zText.get_style_at_offset�spacescst�dkrdS|j}d�}|r1t|����fdd�|D�|jdd�<|j�|�|j�7_dS|j|7_dS)z�Extend the Text given number of spaces where the spaces have the same style as the last character. Args: spaces (int): Number of spaces to add to the Text. rNr]cs$g|]}|j�kr|���n|�qSr1)r+r?rz�� end_offsetr�r1r2rnHs��z%Text.extend_style.<locals>.<listcomp>)rQrSrMrHrbrNr[)r0r�rQZ new_spacesr1r�r2� extend_style<s � zText.extend_style)� style_prefix� re_highlightr�cCs�d}|jj}t}|j}t|t�rt�|�}|�|�D]Q}|j } |rD| �\} } t |�r2||| | ��n|} | durD| | krD||| | | ��|d7}|� �� �D]} | | �\} } | dkrj| | krj||| | |�| ����qNq|S)a%Highlight text with a regular expression, where group names are translated to styles. Args: re_highlight (Union[re.Pattern, str]): A regular expression object or string. style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable which accepts the matched text and returns a style. Defaults to None. style_prefix (str, optional): Optional prefix to add to style group names. Returns: int: Number of regex matches rNr�����) rMrbr)r[r`rF�re�compile�finditerr{�callable� groupdict�keys)r0r�r,r��count� append_spanrmr[�matchZget_spanr*r+Z match_style�namer1r1r2�highlight_regexQs*    ��zText.highlight_regex)�case_sensitive�wordsr�c Cspd�dd�|D��}|jj}d}t}tj||j|rdntjd�D]}|�d�\} } ||| | |��|d7}q |S)aBHighlight words with a style. Args: words (Iterable[str]): Words to highlight. style (Union[str, Style]): Style to apply. case_sensitive (bool, optional): Enable case sensitive matching. Defaults to True. Returns: int: Number of words highlighted. �|css�|]}t�|�VqdSr5)r�rw�rk�wordr1r1r2r|�s�z'Text.highlight_words.<locals>.<genexpr>r��flagsr) r�rMrbr)r�r�r[� IGNORECASEr{) r0r�r,r�Zre_wordsZadd_spanr�rmr�r*r+r1r1r2�highlight_wordsys � zText.highlight_wordscCs|j��|_dS)z"Strip whitespace from end of text.N)r[�rstripr/r1r1r2r��sz Text.rstrip�sizecCsVt|�}||kr'||}t�|j�}|dur)t|�d��}|�t||��dSdSdS)z�Remove whitespace beyond a certain width at the end of the text. Args: size (int): The desired size of the text. Nr)rS�_re_whitespace�searchr[�groupr=r8)r0r�� text_lengthZexcessZwhitespace_matchZwhitespace_countr1r1r2� rstrip_end�s �zText.rstrip_end� new_lengthcCs@t|�}||kr||kr|�||�dS|�||�dSdS)z<Set new length of the text, clipping or padding is required.N)rS� pad_rightr=)r0r�r�r1r1r2� set_length�s �zText.set_length�optionsr!c cs��|jdur |jn|j}|jp|jpt}|jp|jpt}|j||j|||p&dt|j|jd�d�}t d�� |�}|j ||j d�EdHdS)Nr�F�rIrJrLrKrP�r+) rLrIr%rJr'�wrap� max_widthrrKr(r�r�r+)r0r�r�rLrIrJruZ all_linesr1r1r2�__rich_console__�s��zText.__rich_console__cCsT|j}|��}|rtdd�|D��nd}|��}|r#tdd�|D��n|}t||�S)Ncs��|]}t|�VqdSr5�rr�r1r1r2r|���z(Text.__rich_measure__.<locals>.<genexpr>rcsr�r5r�r�r1r1r2r|�r�)r[� splitlines�maxr9r)r0r�r�rRruZmax_text_widthr�Zmin_text_widthr1r1r2�__rich_measure__�s� zText.__rich_measure__rc#s^�t}|j}|jst|�V|r||�VdSt|jt��d��tt|jd��}�fdd�|D���|j ��d<dgdd �|D��d d �|D��t |�d df�}|j t dd�d �g��j }�j}i��j�tj�d tf�����fdd� } t||dd��D]$\\} } } \} }}| r�|| �n|| �| | kr�||| | �| ��Vq|r�||�VdSdS)aRender the text as Segments. Args: console (Console): Console instance. end (Optional[str], optional): Optional end character. Returns: Iterable[Segment]: Result of render that may be written to the console. Nr�rcsi|] \}}|�|j��qSr1�r,�rk�indexr{)r�r1r2r��r�zText.render.<locals>.<dictcomp>r)rFrcs� �|] \}}|jd|fVqdSry)r*r�r1r1r2r|���zText.render.<locals>.<genexpr>csr�r~r�r�r1r1r2r|�r�Trr-csBt�fdd�t��D��}�|�}|dur|S�|�}|�|<|S)z#Construct current style from stack.c3s�|]}�|VqdSr5r1)rkZ _style_id)� style_mapr1r2r|�r�z9Text.render.<locals>.get_current_style.<locals>.<genexpr>N)�tuple�sorted)�stylesZ cached_styleZ current_style)�combine�stack� style_cache�style_cache_getr�r1r2�get_current_style�sz&Text.render.<locals>.get_current_style)rr[rMrr�r�null�list� enumerater,rSr�rrb�remove�getr��zip)r0r�r+�_SegmentrRZenumerated_spansrQZ stack_appendZ stack_popr�r7ZleavingZstyle_idZ next_offset�_r1)r�r�r�r�r�r�r2r��sL�   � � � �&  ��z Text.renderrucs����}dtdf��fdd� }|jj}|jj}|jj}d�t�|�D]*}||j�|jr:|���t|�|j��|��fdd�|jD���t|�7�q#�|_ |S)z�Join text together with this instance as the separator. Args: lines (Iterable[Text]): An iterable of Text instances to join. Returns: Text: A new text instance containing join text. r-r(c3s<��jrt��D] \}}|V|s�VqdS�EdHdSr5)r[r)�lastr�)rur0r1r2� iter_texts���zText.join.<locals>.iter_textrc3s*�|]\}}}��|�||�VqdSr5r1rjrlr1r2r|*� �� �zText.join.<locals>.<genexpr>) r�r rHr?rMrbr)r,rSrN)r0rur�rZ extend_textr�Z extend_spansrRr1)rmrur7r0r2r� s"    �z Text.joinc Csd|jvrdS|dur|j}|durd}g}|j}|jddd�D]P}d|jvr,||�q d}|jddd�}|D]8}|j�d�rf|jddd�d |jd<||j7}||}|re||} |�| �|| 7}n||j7}||�q7q td �� |�} | jg|_t |j�|_ | j |j dd�<dS) ztConverts tabs to spaces. Args: tab_size (int, optional): Size of tabs. Defaults to 8. � Nr�rPT)�include_separatorrr�r]rO) r[rLrbr9r�rHrr�r(r�rSrNrM) r0rLr�rbr�Z cell_positionr�r�Z tab_remainderr�rdr1r1r2� expand_tabs2s<      �  �  zText.expand_tabsF)rJ�padr�rcCs�|p|jpt}|dkrHt|j�}||kr+|dkr$t|j|d�d|_nt|j|�|_|rJ||krL||}|j�d|��g|_t|j�|_dSdSdSdS)auTruncate text if it is longer that a given width. Args: max_width (int): Maximum number of characters in text. overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow. pad (bool, optional): Pad with spaces if the length is less than max_width. Defaults to False. �ignore�ellipsisru…r]N)rJr'rr[rrHrSrN)r0r�rJrZ _overflowr�r�r1r1r2�truncate\s  �z Text.truncatecs2t|j��t���fdd�|jD�|jdd�<dS)z=Remove or modify any spans that are over the end of the text.c�<g|]}|j�kr|j�kr|n �|jt�|j�|j��qSr1�r*r+r8r,rz�rmZ max_offsetr1r2rn{�  ��z$Text._trim_spans.<locals>.<listcomp>N�rSr[r)rMr/r1r r2r�ws  �zText._trim_spansr]r�� charactercs`t|�dks Jd���r.|�}|�|j�|��|_t���fdd�|jD�|jdd�<dSdS)z�Pad left and right with a given number of characters. Args: count (int): Width of padding. character (str): The character to pad with. Must be a string of length 1. r�&Character must be a string of length 1c�&g|]\}}}�|�|�|��qSr1r1rj�rmr�r1r2rn����zText.pad.<locals>.<listcomp>Nr )r0r�rZpad_charactersr1rr2r�s ��zText.padcsXt|�dks Jd���r*|��|j��|_t���fdd�|jD�|jdd�<dSdS)z�Pad the left with a given character. Args: count (int): Number of characters to pad. character (str, optional): Character to pad with. Defaults to " ". rrcrr1r1rjrr1r2rn�rz!Text.pad_left.<locals>.<listcomp>Nr �r0r�rr1rr2�pad_left�s ��z Text.pad_leftcCs4t|�dks Jd��|r|j�||��|_dSdS)z�Pad the right with a given character. Args: count (int): Number of characters to pad. character (str, optional): Character to pad with. Defaults to " ". rrN)rSr[rr1r1r2r��s�zText.pad_right�align�widthcCsx|�|�|t|j�}|r:|dkr|�||�dS|dkr2|d}|�||�|�|||�dS|�||�dSdS)z�Align text to a given width. Args: align (AlignMethod): One of "left", "center", or "right". width (int): Desired width. character (str, optional): Character to pad with. Defaults to " ". �left�center�N)rrr[r�r)r0rrrZ excess_spacerr1r1r2r�s  �z Text.alignrcst|ttf�s td��t|�r�t|t�r=t|�}|j�|�t|�}t|��|r4|j�t ||�|��|j �7_ |St|t�r�t �|durLt d��|j �|j ra|j����t|�|j ��|j�|j �|j���fdd�|j��D��|j t|�7_ |S)z�Add text with an optional style. Args: text (Union[Text, str]): A str or Text to append. style (str, optional): A style name. Defaults to None. Returns: Text: Returns self for chaining. z(Only str or Text can be appended to TextNz2style must not be set when appending Text instancec3�*�|]\}}}�|�|�|�VqdSr5r1rj�rmr�r1r2r|�rzText.append.<locals>.<genexpr>)r`rFr(rrrSrrHrbrMr)rN� ValueErrorr,r[r?ra)r0rRr,rTr7r1rr2rb�s:    ����z Text.appendcstt�|j�|jr|j����t|�|j��|j�|j�|j���fdd�|j� �D��|jt|�7_|S)a Append another Text instance. This method is more performant that Text.append, but only works for Text. Args: text (Text): The Text instance to append to this instance. Returns: Text: Returns self for chaining. c3rr5r1rjrr1r2r|rz#Text.append_text.<locals>.<genexpr>) r)rNr,rMrbrSrHr[r?ra�r0rRr1rr2� append_text�s �zText.append_text�tokenscCsl|jj}|jj}t}t|�}|D] \}}t|�}||�|r*||||t|�|��|t|�7}q||_|S)a)Append iterable of str and style. Style may be a Style instance or a str style definition. Args: tokens (Iterable[Tuple[str, Optional[StyleType]]]): An iterable of tuples containing str content and style. Returns: Text: Returns self for chaining. )rHrbrMr)rSrrN)r0rrr�rmr7�contentr,r1r1r2� append_tokenss  zText.append_tokenscCs|j�|j�dS)z�Copy styles from another Text instance. Args: text (Text): A Text instance to copy styles from, must be the same length. N)rMr?rr1r1r2� copy_stylesszText.copy_styles)r� allow_blank� separatorrr#cs��sJd��|j���vrt|��g�S|r(|�dd�t�t�����D��}ndttf��fdd� }t�fdd�|�|��D��}|sN�� ��rN|� �|S)a�Split rich text in to lines, preserving styles. Args: separator (str, optional): String to split on. Defaults to "\\n". include_separator (bool, optional): Include the separator in the lines. Defaults to False. allow_blank (bool, optional): Return a blank line if the text ends with a separator. Defaults to False. Returns: List[RichText]: A list of rich text, one per line of the original. zseparator must not be emptycss�|]}|��VqdSr5r��rkr�r1r1r2r|?s� �zText.split.<locals>.<genexpr>r-c3s6�t�t�����D]}|��\}}|V|Vq dSr5)r�r�rwr{)r�r*r+�r$rRr1r2� flatten_spansDs � �z!Text.split.<locals>.flatten_spansc3s�|] }|j�kr|VqdSr5rZr�)r$r1r2r|Js��) r[rrarqr�r�rwr rDr��pop)r0r$rr#rur'r1r&r2r9's   �  �z Text.split�offsetscs�t|�}|s t|��g�S|j�t��}dg|�|�}tt||dd���}|j�|j�|j�t �t�����fdd�|D��}|j sE|Sdd�|j D�}t|�}t } |j D]�\} } �d} |} | | d} ||\}}| |kru|d} n | |kr~|d} nn| | d}qf| |kr�|}n'|}} |} ||\}}| |kr�|d} n | |kr�|d} nn| | d}q�t ||d�D]&}||\}}td| |�}t| |||�}||kr�||| ||���q�qV|S) z�Divide text in to a number of lines at given offsets. Args: offsets (Iterable[int]): Offsets used to divide text. Returns: Lines: New RichText instances between offsets. rrNc3s,�|]\}}��||����d�VqdS))r,rIrJNr1)rkr*r+�r�rIrJr,rRr1r2r|js�� � �zText.divide.<locals>.<genexpr>cSsg|]}|jj�qSr1)rMrbr�r1r1r2rnvszText.divide.<locals>.<listcomp>r)r�rrar[rSr�r,rIrJr(rM�_linesr)�ranger�r8)r0r)Z_offsetsr�Zdivide_offsetsZ line_ranges� new_linesZ _line_appendsZ line_countrmZ span_startZspan_endZ lower_boundZ upper_boundZ start_line_noZ line_startZline_endZ end_line_no�line_noZ new_startZnew_endr1r*r2rqSsl �      �     � ��z Text.divider�amountcsXt|j�|�t���fdd�|jD�|jdd�<|jd| �g|_|j|8_dS)z7Remove a number of characters from the end of the text.cr r1r rzr r1r2rn�r z#Text.right_crop.<locals>.<listcomp>N)rSr[r)rMrHrN)r0r/r1r r2r=�s � zText.right_cropr�c Cs�|p|jpt}|p |jp t}t||jd�p|dk}t�} |jdd�D]I} d| vr-| �|�|r5t| g�} nt t | �||dkd�} | � | �} | D]} | � |�qG|rZ| j||||d�| D] } | j ||d �q\| �| �q"| S) aCWord wrap the text. Args: console (Console): Console instance. width (int): Number of cells available per line. justify (str, optional): Justify method: "default", "left", "center", "full", "right". Defaults to "default". overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None. tab_size (int, optional): Default tab size. Defaults to 8. no_wrap (bool, optional): Disable wrapping, Defaults to False. Returns: Lines: Number of lines. FrT�r#rr&)r&r�)rJ)rIr%rJr'rrKrr9rrrFrqr�rr?) r0r�rrIrJrLrKZ wrap_justifyZ wrap_overflowrur�r-r)r1r1r2r��s*    � z Text.wrapcCs0t�}|j}|��D] }|�|�||�q |S)z�Fit the text in to given width by chopping in to lines. Args: width (int): Maximum characters in a line. Returns: Lines: Lines container. )rrbr9r�)r0rrurbr�r1r1r2�fit�s     zText.fitcCsVdd�tjd|jtjd�D�}zttdd�|D��pd}W|Sty*d}Y|Sw)zrAuto-detect indentation of code. Returns: int: Number of spaces used to indent code. cSsh|] }t|�d���qS�r)rSr�r%r1r1r2� <setcomp>�s ��z*Text.detect_indentation.<locals>.<setcomp>� ^( *)(.*)$r�cSsg|]}|ds|�qS)rr1)rk�indentr1r1r2rnsz+Text.detect_indentation.<locals>.<listcomp>r)r�r�r[� MULTILINErrrr)r0Z _indentations� indentationr1r1r2�detect_indentation�s�� ��zText.detect_indentationu│z dim green)rr,� indent_sizecCs8|dur|��n|}|��}|��|�d|d��}t�d�}g}|j} d} |jdd�D]U} |�| j�} | r=| � d�sB| d7} q.| � d�} t t | �|�\}}||�d|��}|| jt |�d�| _| � |dt |��| r|� t||d �g| �d} | | �q.| r�|� td |d �g| �|�d ��|�}|S) u�Adds indent guide lines to text. Args: indent_size (Optional[int]): Size of indentation, or None to auto detect. Defaults to None. character (str, optional): Character to use for indentation. Defaults to "│". style (Union[Style, str], optional): Style of indent guides. Returns: Text: New text with indentation guides. Nr]rr4rTr0rr�rOrP)r8rarr�r�rbr9r�r[r��divmodrSr�r?r(r�r�)r0r9rr,Z _indent_sizerRZ indent_lineZ re_indentr-Zadd_lineZ blank_linesr�r�r5Z full_indentsZremaining_spaceZ new_indentr�r1r1r2�with_indent_guidess4    zText.with_indent_guides)rOrO)rO)r-r()rNr5)r-N)r])rRr(r-r()rRr(r-N)rPr2)Sr@rArBrC� __slots__rFrrr rGrDr r)rUrWr6r\r3rre�objectrgrhrirv�propertyrr�� classmethodrr�r�rr�rr r�r[�setterrQr�rar�r�r�r�r�r�r�r�GetStyleCallabler�r r�r�r�r�rr�rr�r�r�rrr�rrr�rrrbrr!r"rr9rqr=r�r1r8r;r1r1r1r2r(vs\ ��� ����� � � ��� ����� � �!�� ����� � �%������ ��� ������ � �-  � ��� �� ��� �� ��� �$ ����� �-�� �� �  �� ��� � ;'.���� �  � �� � ,� �  ����� �,O������� �0�������__main__)r a� Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ZLorem�boldZipsum�italiczjustify='left'�redr�zjustify='center'�greenr)r,rIzjustify='right'�blue�rightzjustify='full'�magenta�full)Cr�� functoolsrr�mathr�operatorr�typingrrrr r r r r rrrZ_looprZ_pickr�_wraprrrr>rr� containersr�controlrr�r�jupyterr�measurer�segmentrr,rrr�r r!r"r#r%rEr'r�r�rF�TextTyperAr)r(r@� rich.consolerRr��rule�printr1r1r1r2�<module>sr   4              G K �     �
Memory