HEX
Server: LiteSpeed
System: Linux srv146.niagahoster.com 4.18.0-553.30.1.lve.el8.x86_64 #1 SMP Tue Dec 3 01:21:19 UTC 2024 x86_64
User: kodi1989 (1633)
PHP: 8.1.34
Disabled: symlink,shell_exec,exec,popen,system,dl,passthru,escapeshellarg,escapeshellcmd,show_source,pcntl_exec
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/rich/__pycache__/text.cpython-37.pyc
B

��fĸ�@sBUddlZddlmZmZddlmZddlmZddlm	Z	m
Z
mZmZm
Z
mZmZmZmZmZddlmZddlmZdd	lmZdd
lmZddlmZmZddlmZdd
l m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+m,Z,e	�rddl-m.Z.m/Z/m0Z0m1Z1dZ2de3d<dZ4de3d<e�5d�Z6ee7dfZ8ee7gee,fZ9Gdd�de�Z:Gdd�de%�Z;e<dk�r>dd l=m.Z.e;d!�Z>e>�?d"gd#�e>�?d$gd%�e.�Z-e-�@d&�e-jAe>d'd(�e-�A�e-�@d)�e-jAe>d*d+d,�e-�A�e-�@d-�e-jAe>d.d/d,�e-�A�e-�@d0�e-jAe>d1d2d,�e-�A�dS)3�N)�partial�reduce)�gcd)�
itemgetter)
�
TYPE_CHECKING�Any�Callable�Dict�Iterable�List�
NamedTuple�Optional�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<ed�dd�Z	e
d�dd	�Zeede
dfd
�dd�Zedd
�d
d�Zedd
�dd�Zedd�dd�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�r/�:/opt/alt/python37/lib/python3.7/site-packages/rich/text.py�__repr__8sz
Span.__repr__cCs|j|jkS)N)r*r))r.r/r/r0�__bool__;sz
Span.__bool__)�offsetr,cCsV||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)r.r3r)r*r+Zspan1Zspan2r/r/r0�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()r.r3r)r*r+r/r/r0�moveKs	
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(r4)r.r3r)r*r+r/r/r0�
right_cropWs	
zSpan.right_crop)�cellsr,cCs&|r|\}}}t||||�S|SdS)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.
        N)r()r.r8r)r*r+r/r/r0�extendes	
zSpan.extendN)�__name__�
__module__�__qualname__�__doc__�int�__annotations__r�strrr1�boolr2rr
r5r6r7r9r/r/r/r0r(.s

r(c
@s�eZdZdZdddddddd	d
g	Zd�dddd
ddd�eeeefededee	eee
eeedd�	dd�Z
e
d�dd�Ze	d�dd�Zed�dd�Zed�dd�Zedd�dd�Zee	d�d d!�Zee	d�d"d#�Zee
efdd$�d%d&�Zee
d�d'd(��Zeed�d)d*��Zedd+dddd
d,�eeeefe	eeedededd-�d.d/��Zeddddd
d0d1�eeeefededee	eee
dd2�d3d4��Zed�ddd5�ee ededdd6�d7d8��Z!eddddd
d0dd9�eede"ee ffeeefededee	ee
ee#eefdd:�	d;d<��Z$eed�d=d>��Z%e%j&edd?�d@d>��Z%eeed�dAdB��Z'e'j&eeddC�dDdB��Z'd�eddE�dFdG�Z(dd�dHdI�Z)d�eeefe
ee
ddK�dLdM�Z*d�eeefe
ee
ddK�dNdO�Z+d�e#eefe
ee
ddP�dQdR�Z,d�ee#eefeddS�dTdU�Z-eddV�dWdX�Z.dYe
edZ�d[d\�Z/e
dd]�d^d_�Z0d�dd`�eeee1e fee
da�dbdc�Z2d+dd�e3eeeefe	e
de�dfdg�Z4dd�dhdi�Z5e
ddj�dkdl�Z6e
ddm�dndo�Z7dYdpe3e8dq�drds�Z9dYdpe:dq�dtdu�Z;d�dYee3dvdw�dxdy�Z<e3dddz�d{d|�Z=d�ee
dd}�d~d�Z>dd�d��e
ede	dd��d�d��Z?dd�d�d��Z@d�e
edd��d�d��ZAd�e
edd��d�d��ZBd�e
edd��d�d��ZCd�eDe
edd��d�d��ZEd�edefeeed�fdd��d�d��ZFddd��d�d��ZGe3e"eee fdd��d�d��ZHddd��d�d��ZId�d�d�d��ee	e	eJd��d�d��ZKe3e
eJd��d�d��ZLd�e
dd��d�d��ZMddd0dd��dYe
edede
ee	eJd��d�d��ZNe
eJd��d�d��ZOe
d�d�d��ZPd�d�d�d��ee
ee dd��d�d��ZQdS)�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�
)rCrDrEr*rF�spansr!r")	�textr+rCrDrEr*rFrKr,c
CsLt|�}	|	g|_||_||_||_||_||_||_|p:g|_t	|	�|_
dS)N)rrBr+rCrDrEr*rFrG�lenrH)
r.rLr+rCrDrEr*rFrK�sanitized_textr/r/r0�__init__�s
z
Text.__init__)r,cCs|jS)N)rH)r.r/r/r0�__len__�szText.__len__cCs
t|j�S)N)rArH)r.r/r/r0r2�sz
Text.__bool__cCs|jS)N)�plain)r.r/r/r0�__str__�szText.__str__cCsd|j�d|j�d�S)Nz<text � �>)rQrG)r.r/r/r0r1�sz
Text.__repr__)�otherr,cCs(t|ttf�r$|��}|�|�|StS)N)�
isinstancer@r'�copy�append�NotImplemented)r.rU�resultr/r/r0�__add__�s

zText.__add__cCs&t|t�stS|j|jko$|j|jkS)N)rVr'rYrQrG)r.rUr/r/r0�__eq__�s
zText.__eq__cCs.t|t�r||jkSt|t�r*|j|jkSdS)NF)rVr@rQr')r.rUr/r/r0�__contains__�s



zText.__contains__)�slicer,csftdd��fdd�}t|t�r&||�S|�t�j��\}}}|dkrZ��||g�}|dStd��dS)Nr')r3r,cs.t�t�j���fdd��jD�dd�}|S)Ncs6g|].\}}}|�kr"|krnq�dd|��qS)rrr/)�.0r)r*r+)�_Spanr3r/r0�
<listcomp>�sz9Text.__getitem__.<locals>.get_text_at.<locals>.<listcomp>rI)rKr*)r(r'rQrG)r3rL)r.)r`r3r0�get_text_at�sz%Text.__getitem__.<locals>.get_text_atrz%slices with step!=1 are not supported)r>rV�indicesrMrQ�divide�	TypeError)r.r^rbr)�stop�step�linesr/)r.r0�__getitem__�s
zText.__getitem__cCs
t|j�S)z5Get the number of cells required to render this text.)rrQ)r.r/r/r0r�sz
Text.cell_lencCs�ddlm}g}|j}dd|jffdd�|jD�dd�|jD�t|�d|jff�}|jtdd�d	�d}|j}xV|D]N\}}}	||kr�|||||���|}|	rt||r�d
|	�d�n
d|	�d��qtWd
�	|�}
|
S)z~Get console markup to render this Text.

        Returns:
            str: A string potentially creating markup tags.
        r)�escaperFcss|]}|jd|jfVqdS)FN)r)r+)r_�spanr/r/r0�	<genexpr>�szText.markup.<locals>.<genexpr>css|]}|jd|jfVqdS)TN)r*r+)r_rkr/r/r0rl�sT)�keyz[/�]�[rI)
�markuprjrQr+rGrM�sortrrX�join)r.rj�outputrQZmarkup_spans�positionrXr3�closingr+rpr/r/r0rp�s$$
zText.markupT)r+�emoji�
emoji_variantrCrDr*)rLr+rvrwrCrDr*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)rvrw)rprxrCrDr*)
�clsrLr+rvrwrCrDr*rxZ
rendered_textr/r/r0�from_markupszText.from_markup�)r+rCrDrEr*rF)rLr+rCrDrEr*rFr,c	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)�AnsiDecoderrJ)rCrDrEr*rFr+css|]
}|VqdS)Nr/)r_�liner/r/r0rlGsz!Text.from_ansi.<locals>.<genexpr>)�ansir|r'rr�decode)ryrLr+rCrDrEr*rFr|�joiner�decoderrZr/r/r0�	from_ansi$szText.from_ansi)rCrD)rLr+rCrDr,cCs||||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.
        )rCrD)�stylize)ryrLr+rCrDZstyled_textr/r/r0�styledJs
zText.styled)r+rCrDrEr*rF�meta)	�partsr+rCrDrEr*rFr�r,c
Gs^|||||||d�}	|	j}
t}x,|D]$}t||tf�r@|
|�q$|
|�q$W|rZ|	�|�|	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+rCrDrEr*rF)rXr'rVr@�
apply_meta)
ryr+rCrDrEr*rFr�r�rLrX�_Text�partr/r/r0�assemblecs 


z
Text.assemblecCs0t|j�dkr&d�|j�g|jdd�<|jdS)z Get the text as a single string.rrINr)rMrBrr)r.r/r/r0rQ�sz
Text.plain)�new_textr,cCsH||jkrDt|�}|g|jdd�<|j}t|�|_||jkrD|��dS)zSet the text to a new value.N)rQrrBrHrM�_trim_spans)r.r�rNZ
old_lengthr/r/r0rQ�s


cCs|jS)z.Get a reference to the internal list of spans.)rG)r.r/r/r0rK�sz
Text.spans)rKr,cCs|dd�|_dS)z
Set spans.N)rG)r.rKr/r/r0rK�s)rQr,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+rCrDrEr*rF)r'r+rCrDrEr*rF)r.rQ�	copy_selfr/r/r0�
blank_copy�s
zText.blank_copyc	Cs8t|j|j|j|j|j|j|jd�}|j|jdd�<|S)zReturn a copy of this instance.)r+rCrDrEr*rFN)	r'rQr+rCrDrEr*rFrG)r.r�r/r/r0rW�s
z	Text.copyr)r+r)r*r,cCsj|rft|�}|dkr||}|dkr(|}|dkr8||}||ksH||krLdS|j�t|t||�|��dS)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)rMrGrXr(r4)r.r+r)r*�lengthr/r/r0r��s
zText.stylizecCsl|rht|�}|dkr||}|dkr(|}|dkr8||}||ksH||krLdS|j�dt|t||�|��dS)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)rMrG�insertr(r4)r.r+r)r*r�r/r/r0�stylize_before�s
zText.stylize_before)r�r)r*r,cCst�|�}|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�)r.r�r)r*r+r/r/r0r��s
zText.apply_meta)r��handlersr,cKs<|dkrin|}|�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)�@r/)r_rm�valuer/r/r0�
<dictcomp>szText.on.<locals>.<dictcomp>)�update�itemsr�rr�)r.r�r�r/r/r0�on
szText.on)�suffixr,cCs|j�|�r|�t|��dS)zaRemove a suffix if it exists.

        Args:
            suffix (str): Suffix to remove.
        N)rQ�endswithr7rM)r.r�r/r/r0�
remove_suffixszText.remove_suffixr)�consoler3r,cCsj|dkrt|�|}|j}||j���}x<|jD]2\}}}||krN|kr0nq0|||dd�7}q0W|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.
        rrI)r#)rM�	get_styler+rWrG)r.r�r3r�r+r)r*Z
span_styler/r/r0�get_style_at_offset'szText.get_style_at_offset)�spacesr,csr�dkrdS|j}d�}|r`t|����fdd�|D�|jdd�<|j�|�|j�7_n|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.
        rNrScs$g|]}|j�kr|���n|�qSr/)r*r9)r_rk)�
end_offsetr�r/r0raHsz%Text.extend_style.<locals>.<listcomp>)rKrMrGrBrXrHrQ)r.r�rKZ
new_spacesr/)r�r�r0�extend_style;szText.extend_style)�style_prefix)�re_highlightr+r�r,cCs�d}|jj}t}|j}x�t�||�D]�}|j}	|rx|	�\}
}t|�rT|||
|��n|}|dk	rx||
krx|||
||��|d7}xH|���	�D]8}
|	|
�\}
}|
dkr�||
kr�|||
||�|
����q�Wq$W|S)aHighlight text with a regular expression, where group names are
        translated to styles.

        Args:
            re_highlight (str): A regular expression.
            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���)
rGrXr(rQ�re�finditerrk�callable�	groupdict�keys)r.r�r+r��count�append_spanr`rQ�matchZget_spanr)r*Zmatch_style�namer/r/r0�highlight_regexPs"
 zText.highlight_regex)�case_sensitive)�wordsr+r�r,cCstd�dd�|D��}|jj}d}t}xJtj||j|r8dntjd�D]*}|�d�\}	}
|||	|
|��|d7}qBW|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�|�VqdS)N)r�rj)r_�wordr/r/r0rl�sz'Text.highlight_words.<locals>.<genexpr>r)�flagsr)	rrrGrXr(r�r�rQ�
IGNORECASErk)r.r�r+r�Zre_wordsZadd_spanr�r`r�r)r*r/r/r0�highlight_wordsvszText.highlight_wordscCs|j��|_dS)z"Strip whitespace from end of text.N)rQ�rstrip)r.r/r/r0r��szText.rstrip)�sizer,cCsNt|�}||krJ||}t�|j�}|dk	rJt|�d��}|�t||��dS)z�Remove whitespace beyond a certain width at the end of the text.

        Args:
            size (int): The desired size of the text.
        Nr)rM�_re_whitespace�searchrQ�groupr7r4)r.r��text_lengthZexcessZwhitespace_matchZwhitespace_countr/r/r0�
rstrip_end�szText.rstrip_end)�
new_lengthr,cCs:t|�}||kr6||kr(|�||�n|�||�dS)z<Set new length of the text, clipping or padding is required.N)rM�	pad_rightr7)r.r�r�r/r/r0�
set_length�s
zText.set_lengthr )r��optionsr,c
cs�|jdkr|jn|j}|jp$|jp$t}|jp4|jp4t}|j||j|||pJdt|j|jd�d�}t	d��
|�}|j||jd�EdHdS)Nr{F)rCrDrFrErJ)r*)
rFrCr$rDr&�wrap�	max_widthrrEr'rrrxr*)r.r�r�rFrCrDrhZ	all_linesr/r/r0�__rich_console__�szText.__rich_console__cCsT|j}|��}|r$tdd�|D��nd}|��}|rFtdd�|D��n|}t||�S)Ncss|]}t|�VqdS)N)r)r_r}r/r/r0rl�sz(Text.__rich_measure__.<locals>.<genexpr>rcss|]}t|�VqdS)N)r)r_r�r/r/r0rl�s)rQ�
splitlines�maxr5r)r.r�r�rLrhZmax_text_widthr�Zmin_text_widthr/r/r0�__rich_measure__�szText.__rich_measure__r)r�r*r,c#s\t}|j}|js,t|�V|r(||�VdSt|jt��d��tt|jd��}�fdd�|D���|j	��d<ddd	�|D�d
d	�|D�t
|�ddff�}|jtdd�d�g��j
}�j}i��j�tj�td
������fdd�}	x`t||dd��D]J\\}
}}\}
}}|�r||�n||�|
|
kr�|||
|
�|	��Vq�W|�rX||�VdS)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.
        N)r#rcsi|]\}}�|j�|�qSr/)r+)r_�indexrk)r�r/r0r��szText.render.<locals>.<dictcomp>r�rFrcss|]\}}|jd|fVqdS)FN)r))r_r�rkr/r/r0rl�szText.render.<locals>.<genexpr>css|]\}}|jd|fVqdS)TN)r*)r_r�rkr/r/r0rl�sT)rm)r,csBt�fdd�t��D��}�|�}|dk	r.|S�|�}|�|<|S)z#Construct current style from stack.c3s|]}�|VqdS)Nr/)r_Z	_style_id)�	style_mapr/r0rl�sz9Text.render.<locals>.get_current_style.<locals>.<genexpr>N)�tuple�sorted)ZstylesZcached_styleZ
current_style)�combine�stack�style_cache�style_cache_getr�r/r0�get_current_style�sz&Text.render.<locals>.get_current_style)r�)rrQrGrr�r�null�list�	enumerater+rMrqrrX�remove�getr��zip)r.r�r*Z_SegmentrLZenumerated_spansrKZstack_appendZ	stack_popr�r3ZleavingZstyle_idZnext_offset�_r/)r�r�r�r�r�r�r0rx�s>



(
zText.render)rhr,cs����}tdd���fdd�}|jj}|jj}|jj}d�t�x^|�D]T}||j�|jrv|���t|�|j��|��fdd�|jD���t|�7�qHW�|_	|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<�jr.x0t��D]\}}|V|s�VqWn
�EdHdS)N)rQr)�lastr})rhr.r/r0�	iter_textszText.join.<locals>.iter_textrc3s(|] \}}}��|�||�VqdS)Nr/)r_r)r*r+)r`r3r/r0rl(szText.join.<locals>.<genexpr>)
r�r
rBr9rGrXr(r+rMrH)r.rhr�r�Zextend_textr�Zextend_spansrLr/)r`rhr3r.r0rrs"
	
z	Text.join)rFr,cCs"d|jkrdS|dkr|j}|dkr(d}g}|j}x�|jddd�D]�}d|jkrZ||�qBd}|jddd�}xx|D]p}|j�d�r�|jddd�d	|jd<||j7}||}|r�||}	|�|	�||	7}n
||j7}||�qrWqBWtd
��	|�}
|
jg|_t
|j�|_|
j|jdd�<dS)ztConverts tabs to spaces.

        Args:
            tab_size (int, optional): Size of tabs. Defaults to 8.

        �	Nr{rJT)�include_separatorrr�rSrI)
rQrFrXr5r�rBrr�r'rrrMrHrG)r.rFr�rXr}Z
cell_positionr�r�Z
tab_remainderr�rZr/r/r0�expand_tabs/s8








zText.expand_tabsF)rD�pad)r�rDr�r,cCs�|p|jpt}|dkr�t|j�}||krV|dkrHt|j|d�d|_nt|j|�|_|r�||kr�||}|j�d|��g|_t|j�|_dS)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.
        �ignoreZellipsisru…rSN)rDr&rrQrrBrMrH)r.r�rDr�Z	_overflowr�r�r/r/r0�truncateYs
z
Text.truncatecs2t|j��t���fdd�|jD�|jdd�<dS)z=Remove or modify any spans that are over the end of the text.cs<g|]4}|j�kr|j�kr |n�|jt�|j�|j��qSr/)r)r*r4r+)r_rk)r`�
max_offsetr/r0raysz$Text._trim_spans.<locals>.<listcomp>N)rMrQr(rG)r.r/)r`r�r0r�ts
zText._trim_spansrS)r��	characterr,cs\t|�dkstd���rX|�}|�|j�|��|_t���fdd�|jD�|jdd�<dS)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.
        rz&Character must be a string of length 1cs&g|]\}}}�|�|�|��qSr/r/)r_r)r*r+)r`r�r/r0ra�szText.pad.<locals>.<listcomp>N)rM�AssertionErrorrQr(rG)r.r�r�Zpad_charactersr/)r`r�r0r��szText.padcsTt|�dkstd���rP|��|j��|_t���fdd�|jD�|jdd�<dS)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 " ".
        rz&Character must be a string of length 1cs&g|]\}}}�|�|�|��qSr/r/)r_r)r*r+)r`r�r/r0ra�sz!Text.pad_left.<locals>.<listcomp>N)rMr�rQr(rG)r.r�r�r/)r`r�r0�pad_left�sz
Text.pad_leftcCs0t|�dkstd��|r,|j�||��|_dS)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 " ".
        rz&Character must be a string of length 1N)rMr�rQ)r.r�r�r/r/r0r��szText.pad_right)�align�widthr�r,cCsp|�|�|t|j�}|rl|dkr2|�||�n:|dkr`|d}|�||�|�|||�n|�||�dS)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)r�rrQr�r�)r.r�r�r�Zexcess_spacer�r/r/r0r��s
z
Text.alignr)rLr+r,cst|ttf�std��t|�r�t|t�rxt|�}|j�|�t|�}t|��|rh|j�t	||�|��|j
�7_
n�t|t�r�t	�|dk	r�td��|j
�|jr�|j����t|�|j��|j�|j
�|j���fdd�|jD��|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 instancec3s(|] \}}}�|�|�|�VqdS)Nr/)r_r)r*r+)r`r�r/r0rl�szText.append.<locals>.<genexpr>)rVr@r'rerMrrBrXrGr(rH�
ValueErrorr+rQr9)r.rLr+rNr3r/)r`r�r0rX�s4


zText.append)rLr,cspt�|j�|jr.|j����t|�|j��|j�|j�|j���fdd�|jD��|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.
        c3s(|] \}}}�|�|�|�VqdS)Nr/)r_r)r*r+)r`r�r/r0rl�sz#Text.append_text.<locals>.<genexpr>)	r(rHr+rGrXrMrBrQr9)r.rLr/)r`r�r0�append_text�s
zText.append_text)�tokensr,cCsh|jj}|jj}t}t|�}x@|D]8\}}||�|rN||||t|�|��|t|�7}q"W||_|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.
        )rBrXrGr(rMrH)r.r�r�r�r`r3�contentr+r/r/r0�
append_tokensszText.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)rGr9)r.rLr/r/r0�copy_stylesszText.copy_styles)r��allow_blank)�	separatorr�r�r,cs��std��|j���kr(t|��g�S|rP|�dd�t�t�����D��}n6tt	d���fdd�}t�fdd�|�|��D��}|s���
��r�|��|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|]}|��VqdS)N)r*)r_r�r/r/r0rl<szText.split.<locals>.<genexpr>)r,c3s8x2t�t�����D]}|��\}}|V|VqWdS)N)r�r�rjrk)r�r)r*)r�rLr/r0�
flatten_spans@sz!Text.split.<locals>.flatten_spansc3s|]}|j�kr|VqdS)N)rQ)r_r})r�r/r0rlGs)r�rQrrWrdr�r�rjr
r>r��pop)r.r�r�r�rhr�r/)r�rLr0r5#s z
Text.split)�offsetsr,cs�t|�}|st|��g�S|j�t��}d	||f�}tt||dd���}|j�|j�|j�t	�t�����fdd�|D��}|j
s�|Sdd�|jD�}t|�}t}	�x:|j
D�].\}
}�d}|}
||
d}xD||\}}|
|kr�|d}
n|
|k�r|d}nP||
d}q�W||k�r$|}nV|}}|}
xH||\}}||k�rR|d}
n||k�rf|d}nP||
d}�q2Wxbt
||d�D]P}||\}}td|
|�}t||||�}||k�r�|||	||����q�Wq�W|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+rCrDNr/)r_r)r*)r�rCrDr+rLr/r0rlgszText.divide.<locals>.<genexpr>cSsg|]}|jj�qSr/)rGrX)r_r}r/r/r0rarszText.divide.<locals>.<listcomp>r�)r)r�rrWrQrMr�r+rCrDr'rG�_linesr(�ranger�r4)r.r�Z_offsetsr�Zdivide_offsetsZline_ranges�	new_linesZ
_line_appends�
line_countr`Z
span_startZspan_endZlower_boundZupper_boundZ
start_line_noZ
line_startZline_endZend_line_no�line_noZ	new_startZnew_endr/)r�rCrDr+rLr0rdOsd	









zText.divider)�amountr,csXt|j�|�t���fdd�|jD�|jdd�<|jd|�g|_|j|8_dS)z7Remove a number of characters from the end of the text.cs<g|]4}|j�kr|j�kr |n�|jt�|j�|j��qSr/)r)r*r4r+)r_rk)r`r�r/r0ra�sz#Text.right_crop.<locals>.<listcomp>N)rMrQr(rGrBrH)r.r�r/)r`r�r0r7�szText.right_crop)rCrDrFrE)r�r�rCrDrFrEr,c
Cs�|p|jpt}|p|jpt}t||jd�p0|dk}t�}	x�|jdd�D]�}
d|
kr\|
�|�|rlt|
g�}n t	t
|
�||dkd�}|
�|�}x|D]}
|
�|�q�W|r�|j||||d�x|D]}
|
j
||d	�q�W|	�|�qFW|	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.
        Fr�T)r�r�r%)r%)rCrD)rD)rCr$rDr&rrErr5r�rr@rdr�r�r9)
r.r�r�rCrDrFrEZwrap_justifyZ
wrap_overflowrhr}r�r�r/r/r0r��s(



z	Text.wrap)r�r,cCs4t�}|j}x"|��D]}|�|�||�qW|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.
        )rrXr5r�)r.r�rhrXr}r/r/r0�fit�s	
zText.fitcCsXdd�tjd|jtjd�D�}yttdd�|D��p6d}Wntk
rRd}YnX|S)zrAuto-detect indentation of code.

        Returns:
            int: Number of spaces used to indent code.
        cSsh|]}t|�d���qS)r)rMr�)r_r�r/r/r0�	<setcomp>�sz*Text.detect_indentation.<locals>.<setcomp>z
^( *)(.*)$)r�cSsg|]}|ds|�qS)r�r/)r_�indentr/r/r0ra�sz+Text.detect_indentation.<locals>.<listcomp>r)r�r�rQ�	MULTILINErrre)r.Z
_indentations�indentationr/r/r0�detect_indentation�s
zText.detect_indentationu│z	dim green)r�r+)�indent_sizer�r+r,cCs@|dkr|��n|}|��}|��|�d|d��}t�d�}g}|j}	d}
x�|jdd�D]�}|�|j�}|r||�	d�s�|
d7}
q^|�	d�}
t
t|
�|�\}}||�d|��}||jt|�d�|_|�|dt|��|
�r|�
t||d	�g|
�d}
|	|�q^W|
�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.
        NrSrz
^( *)(.*)$rT)r�r�)r+rIrJ)rrWr�r��compilerXr5r�rQr��divmodrMr�r9r'r�rr)r.rr�r+Z_indent_sizerLZindent_lineZ	re_indentr�Zadd_lineZblank_linesr}r�r�Zfull_indentsZremaining_spaceZ
new_indentr�r/r/r0�with_indent_guidess4

zText.with_indent_guides)rIrI)rI)rI)rN)rN)rN)N)N)rI)N)rS)rS)rS)rS)N)rJ)r)N)Rr:r;r<r=�	__slots__r@rrr
rAr>rr(rOrPr2rRr1rr[�objectr\r]r^ri�propertyrrp�classmethodrrzr�rr�rr	r�rQ�setterrKr�rWr�r�r�r�r�r�r��GetStyleCallabler�r
r�r�r�r�rr�rr�rxrrr�r�r�r�r�r�rr�rXr�r�r�rr5rdr7r�r�rrr/r/r/r0r'us6(,H#

 	%

;'.*
&O'�__main__)ra�
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.
ZLoremZboldZipsumZitaliczjustify='left'Zred)r+zjustify='center'Zgreenr�)r+rCzjustify='right'Zblue�rightzjustify='full'Zmagenta�full)Br��	functoolsrr�mathr�operatorr�typingrrrr	r
rrr
rrZ_looprZ_pickr�_wraprr�rr8rrZ
containersr�controlrrvrZjupyterrZmeasurer�segmentrr+rrr�rr r!r"r$r?r&rr�r@ZTextTyper
r(r'r:Zrich.consolerLr�Zrule�printr/r/r/r0�<module>sl0

GH