
    AHjb                        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  ej                  d      Zdd	Z	 	 d	 	 	 	 	 	 	 	 	 	 	 dd
Zy)    )annotationsN)Iterable)get_character_cell_size)cell_len)	loop_last)get_tab_widthsz
\S+\s*|\s+c              #     K   d}t         j                  | |      x}E|j                         \  }}|j                  d      }|||f t         j                  | |      x}Dyyw)aD  Yields each "chunk" from the text as a tuple containing (start_index, end_index, chunk_content).
    A "chunk" in this context refers to a word and any whitespace around it.

    Args:
        text: The text to split into chunks.

    Returns:
        Yields tuples containing the start, end and content for each chunk.
    r   N)re_chunkmatchspangroup)textendchunk_matchstartchunks        E/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/_wrap.pychunksr      sj      C"..s33;
@ %%'
s!!!$S% #..s33;
@s   AA#!A#c                   t        ||      }|r|}nt        | |      }g }|j                  }d}t        }	d}
d}g }|j                  }t        |      D ]O  \  }\  }}t        |      t        t        |            z   }|g|z  } ||       ||z  }|s?|j                  |       Q t        |       D ]D  \  }}} |	|      }||   }||   }||z
  }||z  }||z
  }||k\  }|r||z  }6||kD  r|rt        }g g}|j                  }|d   j                  }d} |D ]M  }!|!dk(  r||
   d   }"|
dz  }
n ||!      }"| |"z   |kD  r ||!g       |d   j                  }|"} A ||!       | |"z  } O |D #cg c]  }#dj                  |#       }$}#t        |$      D ]B  \  }}#|r ||       |r#||t        |#      z      ||   z
  }% |	|#      |%z   }5|t        |#      z  }D %|r ||       |}3|s7|s; ||       |}G |S c c}#w )ac  Given a string of text, and a width (measured in cells), return a list
    of codepoint indices which the string should be split at in order for it to fit
    within the given width.

    Args:
        text: The text to examine.
        width: The available cell width.
        tab_size: The tab stop width.
        fold: If True, words longer than `width` will be folded onto a new line.
        precomputed_tab_sections: The output of `get_tab_widths` can be passed here directly,
            to prevent us from having to recompute the value.

    Returns:
        A list of indices to break the line at.
    r   	    )minr   appendr   extendr   lenintboolr   r   join)&r   widthtab_sizefoldprecomputed_tab_sectionstab_sectionsbreak_positionsr   cell_offset	_cell_lentab_section_indexcumulative_widthcumulative_widthsrecord_widthslasttab_section	tab_widthsection_codepoint_lengthwidthsr   r   r   chunk_widthtab_width_before_starttab_width_before_endchunk_tab_widthremaining_space
chunk_fits_get_character_cell_sizelinesappend_new_lineappend_to_last_linetotal_width	character
cell_widthlinefolded_wordline_tab_widthss&                                         r   compute_wrap_offsetsrB       s   , 8U#H/%dH5!#O##FKI#%%,,M*3L*A 7&&{I#&{#3c$y/6J#J "#&>>fI%$$%567 $D\ B*sE&!25!905.1GG&+-$3
;&K U"/F,.0TE&+llO*/)*:*:'"#K%* 6	$,)56G)H)KJ-2-)A))LJ&3e;+YK827)2B2B/*4K/	:':5K6  >C"CT2774="CK"C&/&< /
d "5M !2%#d)2C D"3E":!; , +4D/O*KK!SY.E/$ u"-K u)EB*H 9 #Ds   'G8)r   strreturnzIterable[tuple[int, int, str]])TN)r   rC   r!   r   r"   r   r#   r   r$   zlist[tuple[str, int]] | NonerD   z	list[int])
__future__r   retypingr   
rich.cellsr   textual._cellsr   textual._loopr   textual.expand_tabsr   compiler
   r   rB        r   <module>rO      s{    " 	  . # # .2::m$ * =As
ss s 	s
 ;s srN   