
    AHj"                    b   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZ dd
lmZ  ed      dd       Z G d de      ZddZ	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZ ej6                  d      Zej:                  f	 	 	 	 	 	 	 ddZy)z6
Tools for processing Segments, or lists of Segments.
    )annotationsN)	lru_cache)IterableSegment)Style)cell_len)AlignHorizontalAlignVertical)Sizei    c                     t        d| z  |      S )zMake a blank segment.

    Args:
        width: Width of blank.
        style: Style of blank.

    Returns:
        A single segment
     r   )widthstyles     N/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_segment_tools.py
make_blankr      s     3;&&    c                      e Zd Zy)NoCellPositionForIndexN)__name__
__module____qualname__ r   r   r   r   !   s    r   r   c                4   | st         |dk(  ryd}d}d}d}d}t        |       }	 ||k  r=t        |      }|j                  }t	        |      }t        |      }||z  }||z  }||k  r=||z
  }	||	z
  }
||z
  }|t        |d|
       z   S # t        $ r t         w xY w)a  Given a character index, return the cell position of that character within
    an Iterable of Segments. This is the sum of the cell lengths of all the characters
    *before* the character at `index`.

    Args:
        segments: The segments to find the cell position within.
        index: The index to convert into a cell position.

    Returns:
        The cell position of the character at `index`.

    Raises:
        NoCellPositionForIndex: If the supplied index doesn't fall within the given segments.
    r    N)r   iternexttextlenr	   StopIteration)segmentsindexcell_position_endsegment_lengthsegment_end_indexsegment_cell_lengthr   iter_segmentssegmentsegment_index_startindex_within_segmentsegment_cell_starts               r   index_to_cell_positionr,   %   s     $$zNDNM	%%'=)G<<D YN"*4.!44/  %' ,n< #66*-@@.C/C)D EEE  %$$%s   AB Bc                   t         }d}g }|j                  }t        |       }d}	|D ]7  }	| ||	j                        z   }
|
|kD  r|	j	                  ||z
        d   }	 n|
}9 g S ||k\  r|	r ||	       |j                  |       |S |}|	T| ||	j                        z   }
|
|k  r	 ||	       n  ||	j	                  ||z
        d          	 |S |
}t        |d      }	|	T|S )a$  Crops a list of segments between two cell offsets.

    Args:
        segments: A list of Segments for a line.
        start: Start offset (cells)
        end: End offset (cells, exclusive)
        total: Total cell length of segments.
    Returns:
        A new shorter list of segments
    r   N   )r	   appendr   r   split_cellsextendr   )r!   startendtotal	_cell_lenposoutput_segmentsadd_segmentr'   r(   end_poss              r   	line_cropr:   S   s   " I
C%'O!((KNM"G  	',,//U?))%#+6q9G 	
e| }-
C

	',,//S= ++C#I6q9:  }d+ 
 r   c                N   | j                         } | r<|r:| d   j                  d      \  }}|j                  r|| d<   n| j                  d       | rT|rR| d   }|j                  t	        |j                        dz
        \  }}|j                  r|| d<   | S | j                          | S )zOptionally remove a cell from the start and / or end of a list of segments.

    Args:
        segments: A line (list of Segments)
        start: Remove cell from start.
        end: Remove cell from end.

    Returns:
        A new list of segments.
    r   r.   )copyr0   r   popr   )r!   r2   r3   _first_segmentlast_segments         r   	line_trimrB      s     }}HE#A;2215='HQKLLOC|&223|7H7H3IA3MNa'HRL O LLNOr   c                    |r|rt        ||      g| t        ||      S |rt        ||      g| S |rg | t        ||      S t        |       S )a#  Adds padding to the left and / or right of a list of segments.

    Args:
        segments: A line of segments.
        pad_left: Cells to pad on the left.
        pad_right: Cells to pad on the right.
        style: Style of padded cells.

    Returns:
        A new line with padding.
    )r   list)r!   pad_left	pad_rightr   s       r   line_padrG      s     Ix'

 y%(
 	

 
x'

 	
 


y%(
 	
 >r   c              #    K   | sy|\  }t         j                  }| D cg c]
  } ||       }}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(  r2t	        ||       D ]"  \  }}|k(  r| t        |d|z
         $ n|d	k(  rGt        d|	z
        dz  }t	        ||       D ]%  \  }}|k(  r| t        |||z
  |z
         ' n6|d
k(  r1t	        ||       D ]"  \  }}|k(  r| t        ||z
  d       $ |r ||      E d{    yyc c}w 7 7 w)zAlign lines.

    Args:
        lines: A list of lines.
        style: Background style.
        size: Size of container.
        horizontal: Horizontal alignment.
        vertical: Vertical alignment.

    Returns:
        Aligned lines.
    Nc                &    t              gg| z  S )zCreate blank lines.

        Args:
            count: Desired number of blank lines.

        Returns:
            A list of blank lines.
        )r   )countr   r   s    r   blank_linesz align_lines.<locals>.blank_lines   s     E5)*+e33r   r   topmiddle   bottomleftcenterright)rJ   intreturnlist[list[Segment]])r   get_line_lengthmaxr   ziprG   )linesr   size
horizontalverticalheightrV   lineline_lengthsshape_widthshape_heightrK   top_blank_linesbottom_blank_linesvertical_excess_spacecell_length
left_spacer   s    `               @r   align_linesrg      s    & ME6--O6;<dOD)<L<l#K|$L	4 ,-,O(6L#8952	X	/142_D	X	////V!$\5!9 	DKe#
tQ(;UCC		D 
x	EK/0A5
!$\5!9 	Ke#
*ek&9J&F 		 
w	!$\5!9 	DK#
tU[%8!UCC		D 1222 k =6 	06 	3s/   E)E AE)E%C
E)E'E)'E)z	(\s+|\S+)c              #     K   t         }| D ]^  }d|j                  vr| |\  }}} ||      D ]8  }	|	s|	j                         r ||t        |	      z  |       . ||	|       : ` yw)zReplace run of spaces with another character + style.

    Args:
        segments: Segments to process.
        character: Character to replace spaces.
        hatch_style: Style of replacement characters.

    Yields:
        Segments.
    r   N)r   r   isspacer   )
r!   	characterhatch_style_split_Segmentr(   r   r   r?   tokens
             r   apply_hatchro     sz       H 
5gll"M$ND% 5}}&y3u:'={KK&ue445
5s
   2A-8A-)r   r   rT   r   )r!   Iterable[Segment]r"   rS   rT   rS   )
r!   list[Segment]r2   rS   r3   rS   r4   rS   rT   rq   )r!   rq   r2   boolr3   rr   rT   rq   )
r!   rp   rE   rS   rF   rS   r   r   rT   rq   )rY   rU   r   r   rZ   r   r[   r
   r\   r   rT   zIterable[list[Segment]])r!   rp   rj   strrk   r   rT   rp   )__doc__
__future__r   re	functoolsr   typingr   rich.segmentr   
rich.styler   textual._cellsr	   textual.css.typesr
   r   textual.geometryr   r   	Exceptionr   r,   r:   rB   rG   rg   compile
_re_spacessplitro   r   r   r   <module>r      s?   # 	      # < ! 8
' 
'	Y 	+F\22$'2.12:=22j8+.;>GLBM3M3M3 M3  	M3
 M3 M3` RZZ%
 	555 5
 5r   