Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
textwrap.cpython-313.pyc213 linesDownload Raw Back to __pycache__
1�

2��jFo��&�SrSSKJr SSKrSSKrSSKJrJr SSKJ	r3 SSKJr SSK
Jr SS	KJr SS4KJrJr \(aSSKJrJr "SS
\R.5rSSSSSSSSSSSSSSSS.SSjjjrg)z�5Sequence-aware text wrapping functions.6 7This module provides functions for wrapping text that may contain terminal escape sequences, with8proper handling of Unicode grapheme clusters and character display widths.9�)�annotationsN)�
TYPE_CHECKING�Optional�)�width)�iter_graphemes)�HyperlinkParams)�
propagate_sgr)�ZERO_WIDTH_PATTERN�iter_sequences)�Any�Literalc��^�\rSrSrSrSSSSS.SU4Sjjjjr\SSj5rSS	jrSS10jr	SSjr11SSjrSS
jrSSjr
SSjrSSjrSSjrSSjrSSjrSrU=r$) �SequenceTextWrapper�a�12Sequence-aware text wrapper extending :class:`textwrap.TextWrapper`.13 14This wrapper properly handles terminal escape sequences and Unicode grapheme clusters when15calculating text width for wrapping.16 17This implementation is based on the SequenceTextWrapper from the 'blessed' library, with18contributions from Avram Lubkin and grayjk.19 20The key difference from the blessed implementation is the addition of grapheme cluster support21via :func:`~.iter_graphemes`, providing width calculation for ZWJ emoji sequences, VS-16 emojis22and variations, regional indicator flags, and combining characters.23 24OSC 8 hyperlinks are handled specially: when a hyperlink must span multiple lines, each line25receives complete open/close sequences with a shared ``id`` parameter, ensuring terminals26treat the fragments as a single hyperlink for hover underlining. If the original hyperlink27already has an ``id`` parameter, it is preserved; otherwise, one is generated.28�parse�r��
control_codes�tabsize�ambiguous_widthc�N>�[TU]"SSU0UD6 X lX0lX@lg)ag29Initialize the wrapper.30 31:param width: Maximum line width in display cells.32:param control_codes: How to handle control sequences (see :func:`~.width`).33:param tabsize: Tab stop width for tab expansion.34:param ambiguous_width: Width to use for East Asian Ambiguous (A) characters.35:param kwargs: Additional arguments passed to :class:`textwrap.TextWrapper`.36rN�)�super�__init__rrr)�selfrrrr�kwargs�	__class__s      ��VD:\code\apps\devtools\python\user_packages\Python313\site-packages\wcwidth/textwrap.pyr�SequenceTextWrapper.__init__/s+���	���/�u�/��/�*����.��c�.�[R"S5$)z7Generate unique hyperlink id as 8-character hex string.�)�secrets�	token_hexrr!r�_next_hyperlink_id�&SequenceTextWrapper._next_hyperlink_idBs��� � ��#�#r!c�T�[XRURURS9$)z,Measure text width accounting for sequences.r)�
wcwidth_widthrrr�r�texts  r�_width�SequenceTextWrapper._widthGs(���T�1C�1C�T�\�\�-1�-A�-A�C�	Cr!c��/n[U5Hup4U(aMURU5 M! SRU5$)z'Strip all terminal sequences from text.��r�append�join�rr+�result�segment�is_seqs     r�_strip_sequences�$SequenceTextWrapper._strip_sequencesLs9����-�d�3�O�G��6��
�
�g�&� 4��w�w�v��r!c��/n[U5Hup4U(dMURU5 M! SRU5$)z*Extract only terminal sequences from text.r/r0r3s     r�_extract_sequences�&SequenceTextWrapper._extract_sequencesTs9����-�d�3�O�G��v��
�
�g�&� 4��w�w�v��r!c�b�/nSnSnSn[U5H�upgU(dcU(a5U(a.USR5(dUS-
nURU5 UHnUS-
nURU5 X8-
nM SnMoURS5n	URS5(a<U(a5USR5(dU	(dUS-
nURU5 U[	U5-
nU	nM� URU5 [37RRX5n38U39(d40U(aU/$/nSn[	U415n
[U425HVup�[	U5nUS:XaSOX,S-43nX�S-44:XaUSnO45X,U-S-46nUU:waURUUU5 UU-
nMX U$)	ae47Sequence-aware variant of :meth:`textwrap.TextWrapper._split`.48 49This method ensures that terminal escape sequences don't interfere with the text splitting50logic, particularly for hyphen-based word breaking. It builds a position mapping from51stripped text to original text, calls the parent's _split on stripped text, then maps chunks52back.53 54OSC hyperlink sequences are treated as word boundaries::55 56    >>> wrap('foo \x1b]8;;https://example.com\x07link\x1b]8;;\x07 bar', 6)57    ['foo', '\x1b]8;;https://example.com\x07link\x1b]8;;\x07', 'bar']58 59Both BEL (``\x07``) and ST (``\x1b\\``) terminators are supported.60r/rF� r�z]8;;\z]8;;z]�����)	r�isspacer1�61startswith�len�textwrap�TextWrapper�_split�	enumerate)rr+�char_end�
stripped_text�original_pos�prev_was_hyperlink_closer5r6�char�is_hyperlink_close�stripped_chunksr4�stripped_pos�62num_chunks�idx�chunk�	chunk_len�63start_orig�end_origs                   rrE�SequenceTextWrapper._split\s���0!���
���#(� �-�d�3�O�G��+����64�@R�@R�@T�@T�!�S�(�M��O�O�L�1�#�D� �A�%�L��O�O�L�1�!�)�M�$�,1�(�%,�%7�%7�8Z�%[�"�65�&�&�w�/�/�M�%�b�)�1�1�3�3�-�%��,�
� ����5���G��,��+=�(�3 4�8	����%�#�.�.�5�5�d�J���4��6�M�������)�66�#�O�4�J�C��E�67�I�+�a�/��X�Q�>N�5O�J��1�n�$�#�B�<��#�9�$<�q�$@�A���X�%��
�
�d�:�h�7�8��I�%�L�#5�&�
r!c�v
�U(d/$URb}URS:�a
URnOURnURU5URURR55-UR:�a[S5e/nSnSnSn[[U55nU(Ga/nSnU(aUROURnURURU5-68n	UbA[URURURS9R5n69X�S-US'URUS5nUR (aRU(aKU(aDUR#5(d/UR%US5nUS	U(aU(a70X�S-US'U(aLUSn
URU
5nX�-U	::a$UR'UR)55 X�-
nOO	U(aMLU(aqURUS5U	:�aYUR+XX�5 URSR-U55nU(a US(dUS	U(aUS(dMU(aURUS5OSnUR (aiU(abU(a[UR#5(dFUR%US5nX�RUS5-nUS	U(aU(aUSU-US'U(Ga�U(+=(dO UR =(a< [/U5S:H=(a' URUS5R#5(+nURb*[/U5S-UR:dU(Ga�X�::Ga�SR-U5nUR1UU5nUGb3UcbS	UR;a
URnOEUR(a!S	UR35S71UR3nOS	UR353nU[URSS9R55-
nS	UR;aw[URURURS9R5n[URUURS9R5nUR7UUS5n[URUUR5nOSnSnUR (aUR95nUR'UU-5 SnGOfURUR5nU(a�URUS5nUR#5(ayUU-U	::apSR-U5nUR1UU5nUb%U[URSS9R55-
nUR'UU-UR-5 U$X�RUS5-nUS	U(aM�U(aKUR;US5nURU5U-UR::aUUR-US'U$UR'X RR5-5 U$U(aGMU$)
a72Wrap chunks into lines using sequence-aware width.73 74Override TextWrapper._wrap_chunks to use _width instead of len. Follows stdlib's algorithm:75greedily fill lines, handle long words.  Also handle OSC hyperlink processing. When76hyperlinks span multiple lines, each line gets complete open/close sequences with matching77id parameters for hover underlining continuity per OSC 8 spec.78Nrz#placeholder too large for max widthTr)�url�params�79terminatorr?r/zid=�:)rYrWF)�	max_lines�subsequent_indent�initial_indentr,�placeholder�lstripr�80ValueError�list�reversedr	rWrXrY�	make_openr7�drop_whitespace�stripr:r1�pop�_handle_long_wordr2rB�_track_hyperlink_stater&�81make_close�replace�rstrip�_rstrip_visible)r�chunks�indent�lines�
is_first_line�hyperlink_state�current_hyperlink_id�current_line�
current_width�82line_width�open_seq�stripped�	sequencesrQ�chunk_width�
stripped_last�no_more_content�line_content�	new_state�old_open�new_open�
placeholder_w�	last_text�	prev_lines                        r�_wrap_chunks� SequenceTextWrapper._wrap_chunks�sB����I��>�>�%��~�~��!��/�/���,�,�����F�#��k�k�$�"2�"2�"9�"9�";�<�=��j�j�!�!�!F�G�G����
�59��.2���h�v�&�'���&(�L��M�-:�T�(�(�t�?U�?U�F����d�k�k�&�&9�9�J��*�*�'�+�+�*�1�1�.�9�9���)�+�	�83&�r�84�2��r�85�86�,�,�V�B�Z�8�H��#�#��(�8�>�>�CS�CS� �3�3�F�2�J�?�	��2�J���!*�B�Z�!7�F�2�J���r�87��"�k�k�%�0�� �.�*�<� �'�'��88�89��5�!�0�M���&��$�+�+�f�R�j�1�J�>��&�&��-��!%���B�G�G�L�,A� B�
��V�B�Z��r�90��V�B�Z�Z�HT�D�1�1�,�r�2B�C�Y[�M��$�$��!�-�*=�*=�*?�*?� �3�3�L��4D�E�	����\�"�-=�!>�>�
� ��$���'3�B�'7�)�'C�L��$���J�A��(�(�A���K�1�$�A��-�-�f�Q�i�8�>�>�@�@�	 ��N�N�*��E�91�Q�����7�'�)�7�#%�7�7�<�#8�L�!%� ;� ;�L�/� Z�I�!�,�/�7�$�	�(8�(8�8�7@�7G�7G� 4�!*�!1�!1�'*�$�*A�*A�*C�)D�A�i�FV�FV�EW�$X�!5�:=�T�=T�=T�=V�<W�7X� 4�$��'0�';�';��)E�EO�Z�\�R��92!�	�(8�(8�8�'6�$-�M�M�'0�'7�'7�+4�+?�+?�(�(�i�k�	%�93(7�$-�M�M�';�+4�+?�+?�(�(�i�k�	%�94,8�+?�+?��(�TU�+V�L�+:�%�M�M�+?��AU�AU�+W��+/��/3�,��+�+�'3�':�':�'<���L�L��,�!6�7�$)�M�%)�K�K��0@�0@�$A�M�&�$(�$9�$9�,�r�:J�$K�	�%�O�O�-�-�$1�M�$A�Z�$O�+-�7�7�<�+@�L�(,�(C�(C� ,�o�)?�I�(�4� ,��/8�/C�/C��1M�MW�Z�\�!Z��!�L�L��,�)>��AQ�AQ�)Q�R�!����&���\�"�5E�)F�F�
�(��,�'�,�!�(,�(<�(<�U�2�Y�(G�I� $���I� 6�� F�'+�z�z�!2�,5��8H�8H�,H��b�	� %������V�.>�.>�.E�.E�.G�%G�H����s�f�r�r!c���[U5HVup4U(dM[R"U5nUbUR(aUnM<UR	S5(dMTSnMX U$)z�95Track hyperlink state through text.96 97:param text: Text to scan for hyperlink sequences.98:param state: Current state or None if outside hyperlink.99:returns: Updated state after processing text.100Nr>)rr	rrWrA)rr+�stater5r6�parsed_links      rrh�*SequenceTextWrapper._track_hyperlink_stateysY�� .�d�3�O�G��v�-�3�3�G�<���*�{���'�E��'�'�(J�K�K� �E�
 4��r!c�>�US:aSnOXC-101nUSnUR(a�SnSnUR(aiURU5n	[U	5U:�aIU	R	SSU5n102U103S:�a0[SU	SU10455(aUR
XjS-5nSnU(aUnO/URXe5nUS:XaU(dURU5nURUSU5 XkSUS'gU(d URUR55 gg)	z�105Sequence-aware :meth:`textwrap.TextWrapper._handle_long_word`.106 107This method ensures that word boundaries are not broken mid-sequence, and respects grapheme108cluster boundaries when breaking long words.109rr?Fr�-c3�*# �UH	oS:gv� M g7f)r�Nr)�.0�cs  r�	<genexpr>�8SequenceTextWrapper._handle_long_word.<locals>.<genexpr>�s���-V�@U�1�3�h�@U�s�NT)�break_long_words�break_on_hyphensr7rB�rfind�any�_map_stripped_pos_to_original�_find_break_position�_find_first_grapheme_endr1rf)r�reversed_chunks�cur_line�cur_lenr�110space_leftrQ�break_at_hyphen�111hyphen_endrw�112hyphen_pos�113actual_ends            rrg�%SequenceTextWrapper._handle_long_word�s���1�9��J���J���#��� � �#�O��J��$�$��0�0��7���x�=�:�-�!)����Q�114�!C�J�!�A�~�#�-V���*�@U�-V�*V�*V�%)�%G�%G��\]�~�%^�115�*.���'�116�!�6�6�u�I�117�118��?�8�!%�!>�!>�u�!E�J��O�O�E�+�:�.�/�"'��"4�O�B����O�O�O�/�/�1�2�r!c���SnSn[U5HSupVU(aU[U5-
nMU[U5-U:�a	XBU-119-s $U[U5-
nU[U5-
nMU U$)z?Map a position in stripped text back to original text position.r)rrB)rr+rN�stripped_idx�original_idxr5r6s       rr��1SequenceTextWrapper._map_stripped_pos_to_original�sq������-�d�3�O�G����G��,����G��,�|�;�#�l�'B�C�C���G��,����G��,�� 4��r!c�>�SnSnU[U5:a�XnUS:Xa/[R"X5nU(aUR5nMH[	[XS95nUR
U5nXH-U:�aU$XH-
nU[U5-
nU[U5:aM�U$)z;Find string index in text that fits within max_width cells.r�)�start)rBr�match�end�nextrr,)	rr+�	max_widthrP�width_so_farrKr��grapheme�grapheme_widths	         rr��(SequenceTextWrapper._find_break_position�s��������C��I�o��9�D��v�~�*�0�0��;����)�)�+�C���N�4�;�<�H�!�[�[��2�N��,�y�8��120��*�L��3�x�=� �C�%�C��I�o�,�121r!c�<�[[[U555$)z,Find the end position of the first grapheme.)rBr�rr*s  rr��,SequenceTextWrapper._find_first_grapheme_end�s���4��t�,�-�.�.r!c��[[U55nSn[U5H*unupVU(aMUR5(dM(UnM, US:Xag/n[U5H`unupVXC:aUR	U5 M XC:Xa!UR	UR55 MFU(dMOUR	U5 Mb SRU5$)zAStrip trailing visible whitespace, preserving trailing sequences.r?r/)rarrFrkr1r2)rr+�segments�last_vis�ir5r6r4s        rrl�#SequenceTextWrapper._rstrip_visible�s�����t�,�-����$-�h�$7� �A� ���6�g�n�n�.�.���%8��r�>����$-�h�$7� �A� ���|��
�
�g�&����
�
�g�n�n�.�/����
�
�g�&�
%8��w�w�v��r!)rrr��F)r�intr�$Literal['parse', 'strict', 'ignore']rr�rr�rr
�return�None)r��str)r+r�r�r�)r+r�r�r�)r+r�r��	list[str])rmr�r�r�)r+r�r��Optional[HyperlinkParams]r�r�)122r�r�r�r�r�r�rr�r�r�)r+r�rNr�r�r�)r+r�r�r�r�r�)�__name__�123__module__�__qualname__�__firstlineno__�__doc__r�staticmethodr&r,r7r:rEr�rhrgr�r�r�rl�__static_attributes__�
__classcell__)rs@rrrs�����&/�GN� !�()�/� D�/��/�#&�/��	/�$(�	/�/�&�$��$�C�124��\�|}�~���,��1J��&/3�$-�/3�8;�/3�!$�/3�)-�/3�b�&�:/��r!rrrTr/Fz [...])rr�expand_tabs�replace_whitespacerr]r\�fix_sentence_endingsr�r�rdr[r^r125c�x�[UUUUUUUUU	U126UUU
US9nURU5nU(a[U5nU$)u127Wrap text to fit within given width, returning a list of wrapped lines.128 129Like :func:`textwrap.wrap`, but measures width in display cells rather than130characters, correctly handling wide characters, combining marks, and terminal131escape sequences.132 133:param text: Text to wrap, may contain terminal sequences.134:param width: Maximum line width in display cells.135:param control_codes: How to handle terminal sequences (see :func:`~.width`).136:param tabsize: Tab stop width for tab expansion.137:param expand_tabs: If True (default), tab characters are expanded138    to spaces using ``tabsize``.139:param replace_whitespace: If True (default), each whitespace character140    is replaced with a single space after tab expansion. When False,141    control whitespace like ``\n`` has zero display width (unlike142    :func:`textwrap.wrap` which counts ``len()``), so wrap points143    may differ from stdlib for non-space whitespace characters.144:param ambiguous_width: Width to use for East Asian Ambiguous (A)145    characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.146:param initial_indent: String prepended to first line.147:param subsequent_indent: String prepended to subsequent lines.148:param fix_sentence_endings: If True, ensure sentences are always149    separated by exactly two spaces.150:param break_long_words: If True, break words longer than width.151:param break_on_hyphens: If True, allow breaking at hyphens.152:param drop_whitespace: If True (default), whitespace at the beginning153    and end of each line (after wrapping but before indenting) is dropped.154    Set to False to preserve whitespace.155:param max_lines: If set, output contains at most this many lines, with156    ``placeholder`` appended to the last line if the text was truncated.157:param placeholder: String appended to the last line when text is158    truncated by ``max_lines``. Default is ``' [...]'``.159:param propagate_sgr: If True (default), SGR (terminal styling) sequences160    are propagated across wrapped lines. Each line ends with a reset161    sequence and the next line begins with the active style restored.162:returns: List of wrapped lines without trailing newlines.163 164SGR (terminal styling) sequences are propagated across wrapped lines165by default. Each line ends with a reset sequence and the next line166begins with the active style restored::167 168    >>> wrap('\x1b[1;34mHello world\x1b[0m', width=6)169    ['\x1b[1;34mHello\x1b[0m', '\x1b[1;34mworld\x1b[0m']170 171Set ``propagate_sgr=False`` to disable this behavior.172 173Like :func:`textwrap.wrap`, newlines in the input text are treated as174whitespace and collapsed. To preserve paragraph breaks, wrap each175paragraph separately::176 177    >>> text = 'First line.\nSecond line.'178    >>> wrap(text, 40)  # newline collapsed to space179    ['First line. Second line.']180    >>> [line for para in text.split('\n')181    ...  for line in (wrap(para, 40) if para else [''])]182    ['First line.', 'Second line.']183 184.. seealso::185 186   :func:`textwrap.wrap`, :class:`textwrap.TextWrapper`187       Standard library text wrapping (character-based).188 189   :class:`.SequenceTextWrapper`190       Class interface for advanced wrapping options.191 192.. versionadded:: 0.3.0193 194.. versionchanged:: 0.5.0195   Added ``propagate_sgr`` parameter (default True).196 197.. versionchanged:: 0.6.0198   Added ``expand_tabs``, ``replace_whitespace``, ``fix_sentence_endings``,199   ``drop_whitespace``, ``max_lines``, and ``placeholder`` parameters.200 201Example::202 203    >>> from wcwidth import wrap204    >>> wrap('hello world', 5)205    ['hello', 'world']206    >>> wrap('中文字符', 4)  # CJK characters (2 cells each)207    ['中文', '字符']208)rrrr�r�rr]r\r�r�r�rdr[r^)r�wrap�_propagate_sgr)r+rrrr�r�rr]r\r�r�r�rdr[r^r209�wrapperros                  rr�r�s[��F"��#���-�'�%�+�1�)�)�'����G� 
�L�L���E���u�%���Lr!r�)"r+r�rr�rr�rr�r��boolr�r�rr�r]r�r\r�r�r�r�r�r�r�rdr�r[z
Optional[int]r^r�r210r�r�r�)r��211__future__rr$rC�typingrrr,rr)r�r�	hyperlinkr	�	sgr_stater212r��escape_sequencesrrr
rrDrr�rr!r�<module>r�s���#���*�+�$�&�6�@��#�g�(�.�.�g�Tx�?F��!�$(� !�!�"$�&+�"&�"&�!%�$(�$�#�x�<�x��x��x�"�	x�213�x��
x� �x� $�x� �x� �x��x�"�x��x��x�)2�xr!
codekingpro/portable-devtools · Team Ai