Zi\ &dZddlmZddlZddlZddlZddlmZmZddl m Z ddl m Z ddl mZdd lmZdd lmZerdd lmZmZGd d eZejdZd0dZd1dZd2dZGddejZd3ddddddddd d4d/ZdS)5z 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_CHECKING NamedTuple)width)iter_sequences)iter_graphemes) propagate_sgr)ZERO_WIDTH_PATTERN)AnyLiteralc2eZdZUdZded<ded<ded<dS)_HyperlinkStatez>State for tracking an open OSC 8 hyperlink across line breaks.strurlparams terminatorN)__name__ __module__ __qualname____doc____annotations__`/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/wcwidth/textwrap.pyrrs1HH HHHKKKOOOOOrrz*\x1b]8;([^;]*);([^\x07\x1b]*)(\x07|\x1b\\)seqrreturn_HyperlinkState | Nonect|x}rKt|d|d|dSdS)z0Parse OSC 8 open sequence, return state or None.rrrrN)_HYPERLINK_OPEN_REmatchrgroup)rms r_parse_hyperlink_openr''sW  % %c * **Y1771::aggajjQWWUVZZXXXX 4rrrrcd|d||S)zGenerate OSC 8 open sequence.z]8;;rr"s r_make_hyperlink_openr*.s /V / /c /: / //rc d|S)zGenerate OSC 8 close sequence.z]8;;r)rs r_make_hyperlink_closer,3s "j " ""rceZdZdZd(ddddd)fdZed*dZd+dZd,dZd,dZ d-dZ d.dZ d/dZ d0d"Z d1d$Zd2d&Zd+d'ZxZS)3SequenceTextWrappera 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. Fparser control_codestabsizeambiguous_widthrintr3$Literal['parse', 'strict', 'ignore']r4r5kwargsr rNonec htjdd|i|||_||_||_dS)a 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 kwargs: Additional arguments passed to :class:`textwrap.TextWrapper`. rNr)super__init__r3r4r5)selfrr3r4r5r8 __class__s rr<zSequenceTextWrapper.__init__LsD //u////* .rrc*tjdS)z7Generate unique hyperlink id as 8-character hex string.)secrets token_hexrrr_next_hyperlink_idz&SequenceTextWrapper._next_hyperlink_id_s ###rtextcFt||j|j|jS)z,Measure text width accounting for sequences.r2)_widthr3r4r5r=rDs rrFzSequenceTextWrapper._widthds*d$*>> 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. rIrF rz]8;;\z]8;;z]) risspacerK startswithlentextwrap TextWrapper_split enumerate)r=rDchar_end stripped_text original_posprev_was_hyperlink_closerOrPcharis_hyperlink_closestripped_chunksrN stripped_pos num_chunksidxchunk chunk_len start_origend_origs rr^zSequenceTextWrapper._splitysX0!  #( -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$|jr d|d|j}nd|}|t)|j z}d|jvrRt |j|j|j }t |j||j }|||d}t-|j||j }nd}d}|j r|}|||zd }|r|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. TNrrXrIzid=:rF)listreversedinitial_indentsubsequent_indentrrFr*rrrrQdrop_whitespacestriprSrKpop_handle_long_wordrL_track_hyperlink_staterCr,replacerrstrip)r=rnlines is_first_linehyperlink_statecurrent_hyperlink_id current_line current_widthindent line_widthopen_seqstripped sequencesrj chunk_width stripped_last line_content new_stateold_opennew_opens r _wrap_chunksz SequenceTextWrapper._wrap_chunkss I 26+/hv&&''c &&(LM-:UT((t?UFdkk&&9&99J*/#')?A[]]%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$* &!ww|44 !77 oVV (+3 I$4443<3C00&-U!Ud&=&=&?&? T T)BR T T104U9P9P9R9R3T3T0#/2G H\2]2]#]LI$444#7%M9+;Y=Q$S$S#7%M+?AU$W$W'3';';HhPQ'R'R '6! ';Y=Q'S'SOO'+O+/('9#/#6#6#8#8L Vl2333 % Gc &J rstaterct|D]9\}}|r2t|}| |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. NrW)rr'rrZ)r=rDrrOrP parsed_links rryz*SequenceTextWrapper._track_hyperlink_stateXsj .d33 ! !OGV !3G<< *{*'EE''(JKK! E rreversed_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-VrNT) break_long_wordsbreak_on_hyphensrQr[rfindany_map_stripped_pos_to_original_find_break_position_find_first_grapheme_endrKrw) r=rrrr space_leftrjbreak_at_hyphen hyphen_endr hyphen_pos actual_ends rrxz%SequenceTextWrapper._handle_long_wordks 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 3rrgcd}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)rr[)r=rDrg stripped_idx original_idxrOrPs rrz1SequenceTextWrapper._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)r[r r$endnextr rF) r=rDrri width_so_farrdr$graphemegrapheme_widths rrz(SequenceTextWrapper._find_break_positions CIIoo9Dv~~*0s;;))++CN4s;;;<s@rr.r.8sR&/GN !()////////&$$$\$<<<< \\\\|B&/3/3/3/3b&:////////rr.r/r0r1rIT)r3r4r5rsrtrrr rDrr6r3r7r4r5rsrtrboolrr rTc t||||||||} | |} | rt| } | S)u6 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 ambiguous_width: Width to use for East Asian Ambiguous (A) characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts. :param initial_indent: String prepended to first line. :param subsequent_indent: String prepended to subsequent lines. :param break_long_words: If True, break words longer than width. :param break_on_hyphens: If True, allow breaking at hyphens. :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). Example:: >>> from wcwidth import wrap >>> wrap('hello world', 5) ['hello', 'world'] >>> wrap('中文字符', 4) # CJK characters (2 cells each) ['中文', '字符'] )rr3r4r5rsrtrr)r.wrap_propagate_sgr) rDrr3r4r5rsrtrrr wrapperr|s rrrs^P"#'%+))   G LL  E&u%% Lr)rrrr)rrrrrrrr)rrrrr)rDrrr6r3r7r4r6r5r6rsrrtrrrrrr rrrT)r __future__rrerAr\typingrrwcwidthrrFrrr sgr_stater rescape_sequencesr r r rcompiler#r'r*r,r]r.rrrrrs #""""" ,,,,,,,,%$$$$$######$$$$$$666666000000$########j RZ MNN0000 #### V/V/V/V/V/(.V/V/V/r W?F !!"$"&"&#WWWWWWWWr