Z-jesdZddlmZddlZddlZddlmZmZddlm Z ddl m Z ddl mZdd lmZdd lmZmZerdd lmZmZGd d ejZd0ddddddddddddddddd1d/ZdS)2z Sequence-aware text wrapping functions. This module provides functions for wrapping text that may contain terminal escape sequences, with proper handling of Unicode grapheme clusters and character display widths. ) annotationsN) TYPE_CHECKINGOptional)width)iter_graphemes)HyperlinkParams) propagate_sgr)ZERO_WIDTH_PATTERNiter_sequences)AnyLiteralceZdZdZd,dddddd-fdZed.dZd/dZd0dZd0dZ d1dZ d2dZ d3d!Z d4d%Z d5d'Zd6d)Zd/d*Zd0d+ZxZS)7SequenceTextWrappera Sequence-aware text wrapper extending :class:`textwrap.TextWrapper`. This wrapper properly handles terminal escape sequences and Unicode grapheme clusters when calculating text width for wrapping. This implementation is based on the SequenceTextWrapper from the 'blessed' library, with contributions from Avram Lubkin and grayjk. The key difference from the blessed implementation is the addition of grapheme cluster support via :func:`~.iter_graphemes`, providing width calculation for ZWJ emoji sequences, VS-16 emojis and variations, regional indicator flags, and combining characters. OSC 8 hyperlinks are handled specially: when a hyperlink must span multiple lines, each line receives complete open/close sequences with a shared ``id`` parameter, ensuring terminals treat the fragments as a single hyperlink for hover underlining. If the original hyperlink already has an ``id`` parameter, it is preserved; otherwise, one is generated. FparserF control_codestabsizeambiguous_width term_programrintr$Literal['parse', 'strict', 'ignore']rrr bool | strkwargsr returnNonec vtjdd|i|||_||_||_||_dS)aG Initialize the wrapper. :param width: Maximum line width in display cells. :param control_codes: How to handle control sequences (see :func:`~.width`). :param tabsize: Tab stop width for tab expansion. :param ambiguous_width: Width to use for East Asian Ambiguous (A) characters. :param term_program: Terminal software identifier for table correction. ``False`` (default) disables override lookup. ``True`` reads the ``TERM_PROGRAM`` or ``TERM`` environment variable for auto-detection. Accepts a canonical terminal name matching :func:`list_term_programs`, such as from XTVERSION_, ENQ_, or ``TERM_PROGRAM``. .. versionadded:: 0.8.0 :param kwargs: Additional arguments passed to :class:`textwrap.TextWrapper`. rN)super__init__rrrr)selfrrrrrr __class__s h/home/jenkins/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/wcwidth/textwrap.pyr"zSequenceTextWrapper.__init__/sL, //u////* .(strc*tjdS)z7Generate unique hyperlink id as 8-character hex string.)secrets token_hexr r&r%_next_hyperlink_idz&SequenceTextWrapper._next_hyperlink_idKs ###r&textcRt||j|j|j|jS)z,Measure text width accounting for sequences.r) wcwidth_widthrrrrr#r-s r%_widthzSequenceTextWrapper._widthPs0T1CT\-1-A*.*;=== =r&cg}t|D]\}}|s||d|S)z'Strip all terminal sequences from text.r appendjoinr#r-resultsegmentis_seqs r%_strip_sequencesz$SequenceTextWrapper._strip_sequencesVsM-d33 ' 'OGV ' g&&&wwvr&cg}t|D]\}}|r||d|S)z*Extract only terminal sequences from text.r3r4r7s r%_extract_sequencesz&SequenceTextWrapper._extract_sequences^sM-d33 ' 'OGV ' g&&&wwvr& list[str]cg}d}d}d}t|D]\}}|s_|r6|r4|ds|dz }|||D]!}|dz }||||z }"d}f|d} |dr8|r6|ds| s|dz }|||t |z }| }||t j||} | s|r|gSg} d} t | } t| D]o\}}t |}| dkrdn || dz }|| dz kr |d}n|| |zdz }||kr| |||| |z } p| S) a Sequence-aware variant of :meth:`textwrap.TextWrapper._split`. This method ensures that terminal escape sequences don't interfere with the text splitting logic, particularly for hyphen-based word breaking. It builds a position mapping from stripped text to original text, calls the parent's _split on stripped text, then maps chunks back. OSC hyperlink sequences are treated as word boundaries:: >>> wrap('foo \x1b]8;;https://example.com\x07link\x1b]8;;\x07 bar', 6) ['foo', '\x1b]8;;https://example.com\x07link\x1b]8;;\x07', 'bar'] Both BEL (``\x07``) and ST (``\x1b\\``) terminators are supported. r3rF rz]8;;\z]8;;z]) r isspacer5 startswithlentextwrap TextWrapper_split enumerate)r#r-char_end stripped_text original_posprev_was_hyperlink_closer9r:charis_hyperlink_closestripped_chunksr8 stripped_pos num_chunksidxchunk chunk_len start_origend_origs r%rHzSequenceTextWrapper._splitfsX0!  #( -d33 > >OGV >+22 @R@R@T@T2!S(MOOL111#**D A%LOOL111!T)MM+0((%,%7%78Z%[%[" &&w//6M6%b)11336-6%,   555G , +=((  %%%#.55dMJJ 4 6M )) #O44 & &JCE I+a//XlQ>N5OJj1n$$#B<#L9$|j r d |d |j }nd |}|t|j d z }d |j vrxt|j |j |j }t|j ||j }|||d}t|j ||j }nd}d}|jr|}|||zd }nl||j}|r||d}|r||z| kryd|}|||}|+|t|j d z }|||z|jzn|||dz}|d=||rJ||d}|||z|jkr||jz|d<n3|||jzn||S) a Wrap chunks into lines using sequence-aware width. Override TextWrapper._wrap_chunks to use _width instead of len. Follows stdlib's algorithm: greedily fill lines, handle long words. Also handle OSC hyperlink processing. When hyperlinks span multiple lines, each line gets complete open/close sequences with matching id parameters for hover underlining continuity per OSC 8 spec. Nrz#placeholder too large for max widthTr)urlparams terminatorrBr3zid=:)r\rZF) max_linessubsequent_indentinitial_indentr1 placeholderlstripr ValueErrorlistreversedr rZr[r\ make_openr;drop_whitespacestripr=r5pop_handle_long_wordr6rE_track_hyperlink_stater, make_closereplacerstrip_rstrip_visible)r#rXindentlines is_first_linehyperlink_statecurrent_hyperlink_id current_line current_width line_widthopen_seqstripped sequencesrT chunk_width stripped_lastno_more_content line_content new_stateold_opennew_open placeholder_w last_text prev_lines r% _wrap_chunksz SequenceTextWrapper._wrap_chunkss I > %~!!/, F##kk$"2"9"9";";<<=j!!!!FGGG 59.2hv&&''W &(LM-:UT((t?UFdkk&&9&99J**'+*1.9)++  &r 2r ,,VBZ88H# 8 8( 88>>CSCS 8 33F2J?? 2J88!*VBZ!7F2J r "kk%00  ;.*<< '' 555![0MM  #$++fRj11J>>&&L-!% BGGL,A,A B B #VBZ#r #VBZ# HT[D11,r2BCCCY[M$ D D! D*7*=*=*?*? D 33L4DEE \"-=!>!>>  $DD'3B'7)'CL$[  JA(AKK1$A--fQi88>>@@@  N*E Q77'8)Z77#%77<#8#8L!% ; ;L/ Z ZI!,/7$ (8887@7G 4 4!*!1Y%Y$*A*A*C*C$X$XiFV$X$X!5 48YT=T=T=V=V7X7X 4$'0';)E)E)EEOZ\\R ! (888'6$-M'0'7+4+?((((ikk % (7$-M';+4+?((((ikk % ,8+?+?(TU+V+VL+:%M+?AU+W+W+//3,+='3':':'<'< LL,!6777$)MM%)KK0@$A$AM&I$($9$9,r:J$K$K %OO-- "$1M$AZ$O$O+-77<+@+@L(,(C(C ,o)?)?I(4 ,/8/C1M1M1MMWZ\\!Z !LL,)>AQ)QRRR!%\"5E)F)FF (,'I!&(,(<(.E.E.G.G%GHHHoW r r&stateOptional[HyperlinkParams]ct|D]>\}}|r7tj|}| |jr|}'|drd}?|S)z Track hyperlink state through text. :param text: Text to scan for hyperlink sequences. :param state: Current state or None if outside hyperlink. :returns: Updated state after processing text. NrA)r r rrZrD)r#r-rr9r: parsed_links r%rkz*SequenceTextWrapper._track_hyperlink_statesm .d33 ! !OGV !-3G<< *{*'EE''(JKK! E r&reversed_chunkscur_linecur_lencv|dkrd}n||z }|d}|jrd}d}|jr||} t| |krY| dd|} | dkrz8SequenceTextWrapper._handle_long_word..s&-V-V1a3h-V-V-V-V-V-Vr&NT) break_long_wordsbreak_on_hyphensr;rErfindany_map_stripped_pos_to_original_find_break_position_find_first_grapheme_endr5ri) r#rrrr space_leftrTbreak_at_hyphen hyphen_endry hyphen_pos actual_ends r%rjz%SequenceTextWrapper._handle_long_words 199JJJ#   3#OJ$ /0077x==:--!)Q !C!CJ!A~~#-V-V*@U-V-V-V*V*V~%)%G%Gz\]~%^%^ *. F' !66ujII ??8?!%!>!>u!E!EJ OOE+:+. / / /"' "4OB    3 OOO//11 2 2 2 2 2 3 3r&rQcd}d}t|D]^\}}|r|t|z }|t|z|kr |||z zcS|t|z }|t|z }_|S)z?Map a position in stripped text back to original text position.r)r rE)r#r-rQ stripped_idx original_idxr9r:s r%rz1SequenceTextWrapper._map_stripped_pos_to_originals  -d33 - -OGV -G , G ,|;;#|l'BCCCCG , G , r& max_widthcxd}d}|t|kr||}|dkr,tj||}|r|}Mt t ||}||}||z|kr|S||z }|t|z }|t|k|S)z;Find string index in text that fits within max_width cells.r)start)rEr matchendnextrr1) r#r-rrS width_so_farrNrgraphemegrapheme_widths r%rz(SequenceTextWrapper._find_break_positions CIIoo9Dv~~*0s;;))++CN4s;;;<>2$-h$7$7 ' ' A 8|| g&&&&h gnn..//// ' g&&&wwvr&r)rrrrrrrrrrrr rr)rr')r-r'rr)r-r'rr')r-r'rr>)rXr>rr>)r-r'rrrr) rr>rr>rrrrrr)r-r'rQrrr)r-r'rrrr)__name__ __module__ __qualname____doc__r" staticmethodr,r1r;r=rHrrkrjrrrro __classcell__)r$s@r%rrsi&)GN !(),1 ))))))))8$$$\$==== \\\\|}}}}~&/3/3/3/3b&:////r&rrrrTFr3z [...])rr expand_tabsreplace_whitespacerrr`r_fix_sentence_endingsrrrgr^rar r-r'rrrrrrboolrrrrr`r_rrrrgr^ Optional[int]rar rr>ct||||||||| | | | | ||}||}|rt|}|S)u Wrap text to fit within given width, returning a list of wrapped lines. Like :func:`textwrap.wrap`, but measures width in display cells rather than characters, correctly handling wide characters, combining marks, and terminal escape sequences. :param text: Text to wrap, may contain terminal sequences. :param width: Maximum line width in display cells. :param control_codes: How to handle terminal sequences (see :func:`~.width`). :param tabsize: Tab stop width for tab expansion. :param expand_tabs: If True (default), tab characters are expanded to spaces using ``tabsize``. :param replace_whitespace: If True (default), each whitespace character is replaced with a single space after tab expansion. When False, control whitespace like ``\n`` has zero display width (unlike :func:`textwrap.wrap` which counts ``len()``), so wrap points may differ from stdlib for non-space whitespace characters. :param ambiguous_width: Width to use for East Asian Ambiguous (A) characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts. :param term_program: Terminal software identifier for table correction. ``False`` (default) disables override lookup. ``True`` reads the ``TERM_PROGRAM`` or ``TERM`` environment variable for auto-detection. Accepts a canonical terminal name matching :func:`list_term_programs`, such as from XTVERSION_, ENQ_, or ``TERM_PROGRAM``. .. versionadded:: 0.8.0 :param initial_indent: String prepended to first line. :param subsequent_indent: String prepended to subsequent lines. :param fix_sentence_endings: If True, ensure sentences are always separated by exactly two spaces. :param break_long_words: If True, break words longer than width. :param break_on_hyphens: If True, allow breaking at hyphens. :param drop_whitespace: If True (default), whitespace at the beginning and end of each line (after wrapping but before indenting) is dropped. Set to False to preserve whitespace. :param max_lines: If set, output contains at most this many lines, with ``placeholder`` appended to the last line if the text was truncated. :param placeholder: String appended to the last line when text is truncated by ``max_lines``. Default is ``' [...]'``. :param propagate_sgr: If True (default), SGR (terminal styling) sequences are propagated across wrapped lines. Each line ends with a reset sequence and the next line begins with the active style restored. :returns: List of wrapped lines without trailing newlines. SGR (terminal styling) sequences are propagated across wrapped lines by default. Each line ends with a reset sequence and the next line begins with the active style restored:: >>> wrap('\x1b[1;34mHello world\x1b[0m', width=6) ['\x1b[1;34mHello\x1b[0m', '\x1b[1;34mworld\x1b[0m'] Set ``propagate_sgr=False`` to disable this behavior. Like :func:`textwrap.wrap`, newlines in the input text are treated as whitespace and collapsed. To preserve paragraph breaks, wrap each paragraph separately:: >>> text = 'First line.\nSecond line.' >>> wrap(text, 40) # newline collapsed to space ['First line. Second line.'] >>> [line for para in text.split('\n') ... for line in (wrap(para, 40) if para else [''])] ['First line.', 'Second line.'] .. seealso:: :func:`textwrap.wrap`, :class:`textwrap.TextWrapper` Standard library text wrapping (character-based). :class:`.SequenceTextWrapper` Class interface for advanced wrapping options. .. versionadded:: 0.3.0 .. versionchanged:: 0.5.0 Added ``propagate_sgr`` parameter (default True). .. versionchanged:: 0.6.0 Added ``expand_tabs``, ``replace_whitespace``, ``fix_sentence_endings``, ``drop_whitespace``, ``max_lines``, and ``placeholder`` parameters. Example:: >>> from wcwidth import wrap >>> wrap('hello world', 5) ['hello', 'world'] >>> wrap('中文字符', 4) # CJK characters (2 cells each) ['中文', '字符'] )rrrrrrrr`r_rrrrgr^ra)rwrap_propagate_sgr)r-rrrrrrrr`r_rrrrgr^rar wrapperrqs r%rrssV"#-'!%+1))'G" LL  E&u%% Lr&r)$r-r'rrrrrrrrrrrrrrr`r'r_r'rrrrrrrgrr^rrar'r rrr>)r __future__rr*rFtypingrrr1rr/rr hyperlinkr sgr_stater rescape_sequencesr r r rrGrrr r&r%rsj#"""""********+*****$$$$$$&&&&&&666666@@@@@@@@$########qqqqq(.qqqhA?F!$( !$)!"$&+"&"&!%$($#AAAAAAAAr&