
    AHjg                    F   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
ZddlmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z' g dZ(ddZ) G d d      Z*ejV                  jX                   G d d             Z-y)z
This module contains the `Strip` class and related objects.

A `Strip` contains the result of rendering a widget.
See [Line API](/guide/widgets#line-api) for how to use Strips.
    )annotations)	lru_cache)AnyIterableIteratorSequenceN)cell_lenset_cell_size)ColorSystem)ConsoleConsoleOptionsRenderResult)Measurement)Segment)Style	StyleType)index_to_cell_positionline_pad)	FIFOCache)Color)AlignHorizontalAlignVertical)
LineFilter)12345678921515253c           
     p    t         }t        | D cg c]  \  }}}|r
 ||       c}}}      S c c}}}w )zGet the line length (total length of all segments).

    Args:
        segments: Iterable of segments.

    Returns:
        Length of line in cells.
    )r	   sum)segments	_cell_lentext_controls        E/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/strip.pyget_line_lengthr/      s2     IPP$4D!W	$PQQPs   1
1c                  B    e Zd ZdZdddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 d	dZy)
StripRenderablez7A renderable which renders a list of strips into lines.Nc                     || _         || _        y N)_strips_width)selfstripswidths      r.   __init__zStripRenderable.__init__.   s        c              #  v   K   t        j                         }| j                  D ]  }|E d {    |  y 7 wr3   )r   liner4   )r6   consoleoptionsnew_linestrips        r.   __rich_console__z StripRenderable.__rich_console__2   s7      <<>\\ 	EN	s   )979c                    | j                   t        d | j                  D              }n| j                   }t        ||      S )Nc              3  4   K   | ]  }|j                     y wr3   )cell_length).0r@   s     r.   	<genexpr>z3StripRenderable.__rich_measure__.<locals>.<genexpr>>   s     De))D   )r5   maxr4   r   )r6   r=   r>   r8   s       r.   __rich_measure__z StripRenderable.__rich_measure__:   s8     ;;Dt||DDEKKE5%((r:   r3   )r7   list[Strip]r8   
int | NonereturnNone)r=   r   r>   r   rL   r   )r=   z	'Console'r>   z'ConsoleOptions'rL   r   )__name__
__module____qualname____doc__r9   rA   rI    r:   r.   r1   r1   +   sD    A)7	) )+;)	)r:   r1   c                     e Zd ZdZg dZ	 d*	 	 	 	 	 d+dZd,dZed-d       Zed.d       Z	e
d*d/d       Ze
	 d*	 	 	 	 	 d0d	       Ze
	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1d
       Zd2dZed3d       Ze
d4d       Zd5dZd6dZd7dZd7dZd3dZd8dZd9dZed3d       Zd*d:dZd*d:dZd;dZd;dZd<dZd=dZd>dZd*d?dZ d@dZ!dAdZ"dBd Z#dCd!Z$e
 e%d"#      dDd$              Z&e
dEd%       Z'dFd&Z(dGd'Z)dHd(Z*dId)Z+y)JStripa(  Represents a 'strip' (horizontal line) of a Textual Widget.

    A Strip is like an immutable list of Segments. The immutability allows for effective caching.

    Args:
        segments: An iterable of segments.
        cell_length: The cell length if known, or None to calculate on demand.
    )	_segments_cell_length_divide_cache_crop_cache_style_cache_filter_cache_render_cache_line_length_cache_crop_extend_cache_offsets_cache	_link_ids_cell_countNc                <   t        |      | _        || _        t        d      | _        t        d      | _        t        d      | _        t        d      | _        t        d      | _        t        d      | _	        t        d      | _
        d | _        d | _        d | _        y )N      )listrU   rV   r   rW   rX   rY   rZ   r\   r]   r^   r[   r_   r`   )r6   r)   rD   s      r.   r9   zStrip.__init__^   s     h'FOPQl>Gm5>r]IRST aL 	 aL 	 BK1)-*.'+r:   c              #  d   K   	 | j                    | j                   y # t        $ r Y y w xY wwr3   )rU   rD   AttributeErrorr6   s    r.   __rich_repr__zStrip.__rich_repr__t   s2     	.. """ 		s   0! 0	-0-0c                F    dj                  d | j                  D              S )zSegment text. c              3  4   K   | ]  }|j                     y wr3   )r+   rE   segments     r.   rF   zStrip.text.<locals>.<genexpr>~   s     Bw||BrG   )joinrU   rg   s    r.   r+   z
Strip.text{   s     wwB4>>BBBr:   c                    | j                   2| j                  D ch c]  \  }}}|
|j                   c}}| _         | j                   S c c}}w )z$A set of the link ids in this Strip.)r_   rU   _link_id)r6   r,   styles      r.   link_idszStrip.link_ids   sI     >>!26..#.1eQEDUDN ~~s
   AAc                ~    t        |t              rt        j                  |      n|} | t	        d|z  |      g|      S )zCreate a blank strip.

        Args:
            cell_length: Desired cell length.
            style: Style of blank.

        Returns:
            New strip.
         )
isinstancestrr   parser   )clsrD   rq   segment_styles       r.   blankzStrip.blank   s9     /9.DE*%GC+-}=>LLr:   c                :    |D cg c]  } | ||       c}S c c}w )a  Convert lines (lists of segments) to a list of Strips.

        Args:
            lines: List of lines, where a line is a list of segments.
            cell_length: Cell length of lines (must be same) or None if not known.

        Returns:
            List of strips.
        rR   )rx   linesrD   r)   s       r.   
from_lineszStrip.from_lines   s      <AAxHk*AAAs   c           	   #  Z   K   |sy|D cg c]  }|j                    }}t        |      }	t        |      }
d fd}dx}}|@t        d||
z
        }|dk(  r|}n|dk(  r|dz  }||z
  }n|dk(  r|}|r ||      E d{    |dk(  rM|D ]G  }|j                   k(  r| t        t	        |j
                  d|j                   z
               I n|d	k(  rbt        d|	z
        dz  }|D ]J  }|j                   k(  r| t        t	        |j
                  ||j                   z
  |z
               L nN|d
k(  rI|D ]D  }|j                   k(  r|   t	        |j
                  |j                   z
  d             F |r ||      E d{    yyc c}w 7 %7 w)a  Align a list of strips on both axis.

        Args:
            strips: A list of strips, such as from a render.
            style: The Rich style of additional space.
            width: Width of container.
            height: Height of container.
            horizontal: Horizontal alignment method.
            vertical: Vertical alignment method.

        Returns:
            An iterable of strips, with additional padding.

        Nc              3  d   K    t        dz        g      }t        |       D ]  }|  yw)zCreate blank lines.

            Args:
                count: Desired number of blank lines.

            Returns:
                An iterable of blank lines.
            rt   N)r   range)countrz   r,   rx   rq   r8   s      r.   blank_linesz Strip.align.<locals>.blank_lines   s;      ue45u=E5\ s   -0r   topmiddle   bottomleftcenterright)r   intrL   Iterable[Strip])rD   rH   lenrT   r   rU   )rx   r7   rq   r8   height
horizontalverticalr@   line_lengthsshape_widthshape_heightr   top_blank_linesbottom_blank_linesvertical_excess_space
left_spaces   ` ``            r.   alignzStrip.align   s    0 7=>e))>>,'<(	 010,$'6L+@$A!5 %:"X%"71"<%:_%L"X%"7&777 $$-K !UU=N=N5NPUV 	 8#Q 349J $$-K !OO&!E$5$55
B!	  	 7" $$-K %%:K:K2KQPUV 	 "#5666 G ?< 8L 7s/   F+F!A!F+F&DF+F)F+)F+c                .    t        | j                  |      S )a?  Given a character index, return the cell position of that character.
        This is the sum of the cell lengths of all the characters *before* the character
        at `index`.

        Args:
            index: The index to convert.

        Returns:
            The cell position of the character at `index`.
        )r   rU   r6   indexs     r.   r   zStrip.index_to_cell_position  s     &dnne<<r:   c                f    | j                   t        | j                        | _         | j                   S )z7Get the number of cells required to render this object.)rV   r/   rU   rg   s    r.   rD   zStrip.cell_length  s-     $ / ?D   r:   c                H   |D cg c]  }||	 }}|D cg c]  }|j                   D ]  }|  }}}d}t        |D cg c]  }|j                  du  c}      rd}n't        |D cg c]  }|j                  xs d c}      } | ||      S c c}w c c}}w c c}w c c}w )zJoin a number of strips into one.

        Args:
            strips: An iterable of Strips.

        Returns:
            A new combined strip.
        Nr   )rU   anyrV   r(   )rx   r7   r@   join_stripsrm   r)   rD   s          r.   rn   z
Strip.join  s     +1FE4EuFF)4TEOOTGTGTT"&Du""d*DEKKP5u116Q6PQK8[)) GTD Qs   BBB B(Bc                0    t         j                  | |g      S r3   )rT   rn   )r6   others     r.   __add__zStrip.__add__.  s    zz4-((r:   c                    | j                     S r3   )rU   rg   s    r.   __bool__zStrip.__bool__1  s    ~~%%%r:   c                ,    t        | j                        S r3   )iterrU   rg   s    r.   __iter__zStrip.__iter__4  s    DNN##r:   c                ,    t        | j                        S r3   )reversedrU   rg   s    r.   __reversed__zStrip.__reversed__7  s    ''r:   c                ,    t        | j                        S r3   )r   rU   rg   s    r.   __len__zStrip.__len__:  s    4>>""r:   c                X    t        |t              xr | j                  |j                  k(  S r3   )ru   rT   rU   )r6   r@   s     r.   __eq__zStrip.__eq__=  s!    %'OT^^u-NOr:   c                    t        |t              rt        ||dz         }| j                  |j                  |j
                  | j                        S |j
                        S )N   )ru   r   slicecropstartstop
cell_countr   s     r.   __getitem__zStrip.__getitem__@  sT    eS!%+EyyKKEJJ,>
 	
DIJJ
 	
r:   c                t    | j                   !t        d | j                  D              | _         | j                   S )zNumber of cells in the stripc              3  F   K   | ]  }t        |j                          y wr3   )r   r+   rl   s     r.   rF   z#Strip.cell_count.<locals>.<genexpr>K  s     "S3w||#4"Ss   !)r`   r(   rU   rg   s    r.   r   zStrip.cell_countG  s3     #""SDNN"SSDr:   c                    | j                   |k  r8|| j                   z
  }| j                  t        d|z  |      gz   }t        ||      S | S )zExtend the cell length if it is less than the given value.

        Args:
            cell_length: Required minimum cell length.
            style: Style for padding if the cell length is extended.

        Returns:
            A new Strip.
        rt   )rD   rU   r   rT   )r6   rD   rq   missing_spacer)   s        r.   extend_cell_lengthzStrip.extend_cell_lengthN  sQ     k)'$*:*::M~~}1De)L(MMH;//Kr:   c                   | j                   |k(  r| S ||f}| j                  j                  |      }||S | j                  }| j                   }t        }||k  r | |d||z
  z  |      gz   }t        ||      }	nr||kD  rkg }|j                  }
d}|D ]I  }|j                   }||z   |k  r |
|       ||z  }%|\  }}}t        |||z
        } |
 |||              n t        ||      }	n| }	|	| j                  |<   |	S )a   Adjust the cell length, possibly truncating or extending.

        Args:
            cell_length: New desired cell length.
            style: Style when extending, or `None`.

        Returns:
            A new strip with the supplied cell length.
        rt   r   )rD   r\   getrU   r   rT   appendr
   )r6   rD   rq   	cache_keycached_stripr<   current_cell_length_Segmentr?   r@   r   line_lengthrm   segment_lengthr+   ry   r,   s                    r.   adjust_cell_lengthzStrip.adjust_cell_length_  sE    {*K %(	..229=# ~~"..,.A ABEJ H (K0E ;.H__FK 	!(!4!4/+=7O>1K-4*D-({[/HID8D-89	 (K0E E-2	*r:   c                l    t        t        j                  | j                        | j                        }|S )zhSimplify the segments (join segments with same style).

        Returns:
            New strip.
        )rT   r   simplifyrU   rV   )r6   r<   s     r.   r   zStrip.simplify  s0     T^^,
 r:   c                |    dd}t        | j                  D cg c]
  } ||       c}| j                        S c c}w )zPRemove all meta from segments.

        Returns:
            New strip.
        c                ^    | \  }}}|| S |j                         }d|_        t        |||      S )zBuild a Segment with no meta.

            Args:
                segment: Segment.

            Returns:
                Segment, sans meta.
            N)copy_metar   )rm   r+   rq   r-   s       r.   remove_meta_from_segmentz4Strip.discard_meta.<locals>.remove_meta_from_segment  s;     $+ D%}JJLEEK400r:   )rm   r   rL   r   )rT   rU   rV   )r6   r   rm   s      r.   discard_metazStrip.discard_meta  s:    	1  >BnnM7%g.M
 	
Ms   9c                    | j                   j                  ||f      }|At        |j                  | j                  |      | j
                        }|| j                   ||f<   |S )zApply a filter to all segments in the strip.

        Args:
            filter: A line filter object.

        Returns:
            A new Strip.
        )rZ   r   rT   applyrU   rV   )r6   filter
backgroundr   s       r.   apply_filterzStrip.apply_filter  sg     ))--vz.BC T^^Z8$:K:KL 8DD
34r:   c           
        t         }|| j                  vr| S | j                  D cg c]6  \  }}} |||r$|j                  s|j                  |k(  r	|||z   ndn||      8 }}}}t        || j                        S c c}}}w )zApply a style to Segments with the given link_id.

        Args:
            link_id: A link id.
            link_style: Style to apply.

        Returns:
            New strip (or same Strip if no changes).
        N)r   rr   rU   _nullrp   rT   rV   )r6   link_id
link_styler   r+   rq   r-   r)   s           r.   style_linkszStrip.style_links  s     $--'K )-
 
 %eW  ekkenn6O ,1+<UZ'$
 
 Xt0011
s   ;A<c                    |||f}| j                   j                  |      }||S | j                  ||      j                  ||      }|| j                   |<   |S )a  Crop between two points, extending the length if required.

        Args:
            start: Start offset of crop.
            end: End offset of crop.
            style: Style of additional padding.

        Returns:
            New cropped Strip.
        )r]   r   r   r   )r6   r   endrq   r   cached_resultr@   s          r.   crop_extendzStrip.crop_extend  sf     C'	//33I>$  ''U388D-2	*r:   c                t   t        d|      }|| j                  nt        | j                  |      }|dk(  r|| j                  k(  r| S ||k  rt        g d      S ||f}| j                  j                  |      }||S t        }d}g }|j                  }t        | j                        }	d}
|| j                  k\  rt        g d      }n|	D ]7  }
| ||
j                        z   }||kD  r|
j                  ||z
        d   }
 n|}9 || j                  k\  r5|
r ||
       |j                  |	       t        || j                  |z
        }ne|}|
R| ||
j                        z   }||k  r	 ||
       n ||
j                  ||z
        d          n|}t        |	d      }
|
Rt        |||z
        }|| j                  |<   |S )zCrop a strip between two cell positions.

        Args:
            start: The start cell position (inclusive).
            end: The end cell position (exclusive).

        Returns:
            A new Strip.
        r   Nr   )rH   rD   minrT   rX   r   r	   r   r   rU   r+   split_cellsextendnext)r6   r   r   r   cachedr*   posoutput_segmentsadd_segmentiter_segmentsrm   r@   end_poss                r.   r   z
Strip.crop  s    Au"%+d3t7G7G3MA:#!1!11K%<Q<CL	!!%%i0M	)+%,,T^^,"&D$$$"aLE( 	',, 77U?%11%#+>qAG d&&&(&&}5ot/?/?%/GH)!Igll$;;G}#G,#G$7$7c	$B1$EF!C"=$7G ) osU{;&+#r:   c                   d}| j                   }|D cg c]
  }||k  s	| }}t        |      }| j                  j                  |      }||S ||gk(  r| g}nWg }|j                  }t        t        j                  | j                  |      |      D ]  \  }	} |t        |	||z
               |} || j                  |<   |S c c}w )zDivide the strip into multiple smaller strips by cutting at given (cell) indices.

        Args:
            cuts: An iterable of cell positions as ints.

        Returns:
            A new list of strips.
        r   )
rD   tuplerW   r   r   zipr   dividerU   rT   )
r6   cutsr   rD   cutr   r   r7   	add_stripr)   s
             r.   r   zStrip.divide8  s     &&#:sk'9::$K	##''	2M K= VFFI!$W^^DNND%I4!P #%#)45 )/9%% ;s
   
C C c                    | j                   j                  |      }||S t        t        j                  | j
                  |      | j                        }|| j                   |<   |S )zApply a style to the Strip.

        Args:
            style: A Rich style.

        Returns:
            A new strip.
        )rY   r   rT   r   apply_stylerU   rD   )r6   rq   r   styled_strips       r.   r   zStrip.apply_styleX  sc     ""&&u-M68H8H
 $0% r:   c                N    t        j                  |      }| j                  |      S )zApply meta to all segments.

        Args:
            meta: A dict of meta information.

        Returns:
            A new strip.

        )r   	from_metar   )r6   meta
meta_styles      r.   
apply_metazStrip.apply_metaj  s#     __T*

++r:   c           
         | j                   }t        }|D cg c];  \  }}}|r+ |||j                  |nd|j                  v r||z   n||      n ||      = }}}}t	        || j
                        S c c}}}w )Nz@click)rU   r   r   r   rT   rV   )r6   r   r)   r   r+   rq   r-   s          r.   _apply_link_stylezStrip._apply_link_stylew  s    >> )1
 
 %eW   !;;. 4<

4Jej0PU d^
$
 
  Xt0011!
s   A A3i @  )maxsizec                   |j                   |j                  z  x}r8t        }t        |j	                               D cg c]  }|d|z  z  r||    }}ng }|j
                  x}.|j                  |j                  |      j                                |j                  x}/|j                  |j                  |      j                  d             dj                  |      x}	|_        |	S c c}w )zRender ANSI codes for a give style.

        Args:
            style: A Rich style.
            color_system: Color system enumeration.

        Returns:
            A string of ANSI escape sequences to render the style.
        r   F;)_attributes_set_attributes
SGR_STYLESr   
bit_length_colorr   	downgradeget_ansi_codes_bgcolorrn   _ansi)
rx   rq   color_system
attributes
_style_map
bit_offsetsgrcolorbgcoloransis
             r.   render_ansizStrip.render_ansi  s     **U-B-BBB:B#J #(
(=(=(?"@j1 :&C  C\\!E.JJu|4CCEF~~%G2JJw((6EEeLM XXc]*u{s   C)c                    |j                   x}| j                  ||      }|r	d| d| dn|}|j                  r d|j                   d|j                   d| d}|S )zRender a Rich style and text.

        Args:
            style: Style to render.
            text: Content string.
            color_system: Color system enumeration.

        Returns:
            Text with ANSI escape sequences.
        z[mz[0mz]8;id=r   z\z]8;;\)r  r  _linkrp   )rx   rq   r+   r  r
  outputs         r.   render_stylezStrip.render_style  so     KKD(??5,7D155avW-4;;U^^,Aekk]&W  r:   c                   | j                   m|j                  xs t        j                  }| j                  }dj                  | j                  D cg c]  \  }}}||n
 ||||       c}}}      | _         | j                   S c c}}}w )zRender the strip into terminal sequences.

        Args:
            console: Console instance.

        Returns:
            Rendered sequences.
        rj   )r  )r[   _color_systemr   	TRUECOLORr  rn   rU   )r6   r=   r  renderr+   rq   r,   s          r.   r  zStrip.render  s     %"00IK4I4IL&&F!# +/..  'eQ != #E4lKL	"D !!!s   Bc                0   || j                   k7  r| j                  ||      }n| }|s|s|S |j                  j                         }|r|j	                  dt        d|z  |             |r|j                  t        d|z  |             t        |||z   |z         S )aY  Crop the strip to `cell_length`, and add optional padding.

        Args:
            cell_length: Cell length of strip prior to padding.
            left: Additional padding on the left.
            right: Additional padding on the right.
            style: Style of any padding.

        Returns:
            Cropped and padded strip.
        r   rt   )rD   r   rU   r   insertr   r   rT   )r6   rD   r   r   rq   r@   r)   s          r.   crop_padzStrip.crop_pad  s     $***++K?EEL??'')OOAwsTz59:OOGC%K78X{T1E9::r:   c           	     L   |dk(  rR| j                   |k(  r| S t        t        | j                  d|| j                   z
  t	        j
                               |      S |dk(  rqt        d|| j                   z
        dz  }| j                   |k(  r| S t        t        | j                  ||| j                   z
  |z
  t	        j
                               |      S |dk(  rR| j                   |k(  r| S t        t        | j                  || j                   z
  dt	        j
                               |      S y )Nr   r   r   r   r   )rD   rT   r   rU   r   nullrH   )r6   r8   r   r   s       r.   
text_alignzStrip.text_align  s   F?5(T^^Q8H8H0H%**,W  hQ(8(8 89Q>J5(" 0 00:=

	   g5(T^^UT5E5E-Eq%**,W 	 r:   c                r   ||f}| j                   j                  |      x}|S | j                  }g }|D ][  }|\  }}	}
t        j                  d||fi      }|j                  t        ||	r|	|z   n|             |t        |j                        z  }] t        || j                        }|| j                   |<   |S )zApply offsets used in text selection.

        Args:
            x: Offset on X axis (column).
            y: Offset on Y axis (row).

        Returns:
            New strip.
        offset)r^   r   rU   r   r   r   r   r   r+   rT   rV   )r6   xyr   r   r)   strip_segmentsrm   r+   rq   r,   offset_styler@   s                r.   apply_offsetszStrip.apply_offsets  s     F	 //33I>>LK>>(* 	#G$ND% ??Hq!f+=>L!!eel2N W\\""A	# nd&7&78).I&r:   r3   )r)   Iterable[Segment]rD   rK   rL   rM   )rL   zrich.repr.Result)rL   rv   )rL   zset[str])rD   r   rq   zStyleType | NonerL   rT   )r|   zlist[list[Segment]]rD   rK   rL   rJ   )r7   rJ   rq   r   r8   r   r   rK   r   r   r   r   rL   r   )r   r   rL   r   )rL   r   )r7   zIterable[Strip | None]rL   rT   )r   rT   rL   rT   )rL   bool)rL   zIterator[Segment])r@   objectrL   r#  )r   zint | slicerL   rT   )rD   r   rq   Style | NonerL   rT   )rL   rT   )r   r   r   r   rL   rT   )r   rv   r   r   rL   rT   )r   r   r   r   rq   r%  rL   rT   )r   r   r   rK   rL   rT   )r   zIterable[int]rL   zSequence[Strip])rq   r   rL   rT   )r   zdict[str, Any]rL   rT   )r   r   rL   rT   )rq   r   r  r   rL   rv   )rq   r   r+   rv   r  r   rL   rv   )r=   r   rL   rv   )
rD   r   r   r   r   r   rq   r   rL   rT   )r8   r   r   r   rL   rT   )r  r   r  r   rL   rT   ),rN   rO   rP   rQ   	__slots__r9   rh   propertyr+   rr   classmethodrz   r}   r   r   rD   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r!  rR   r:   r.   rT   rT   D   s   I  FJ,),8B,	,, C C   M M CGB'B6@B	B B ]7]7 ]7 	]7
 ]7 $]7  ]7 
]7 ]7~= ! ! * *$)&$(#P
    "5n

8"28&8t@$,2* u  6  ("0;2 Dr:   rT   )r)   r"  rL   r   ).rQ   
__future__r   	functoolsr   typingr   r   r   r   	rich.reprrich
rich.cellsr	   r
   
rich.colorr   rich.consoler   r   r   rich.measurer   rich.segmentr   
rich.styler   r   textual._segment_toolsr   r   textual.cacher   textual.colorr   textual.css.typesr   r   textual.filterr   r   r/   r1   reprautorT   rR   r:   r.   <module>r;     sx    #  4 4  . " > > $   ' C #  < %R

R) )2 d d dr:   