o
    aĎiEq                     @  s  d Z ddlmZ ddlZddlZddlZddlmZ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 erDddlmZmZ G dd deZedZd=ddZd>ddZd?ddZG dd dejZd@dd d!d!dd"d"d#d!d!d!dd$d!d%dAd;d<ZdS )Bz
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)AnyLiteralc                   @  s*   e Zd ZU dZded< ded< ded< dS )_HyperlinkStatez>State for tracking an open OSC 8 hyperlink across line breaks.strurlparams
terminatorN)__name__
__module____qualname____doc____annotations__ r   r   L/home/jeff/fluffinator/venv/lib/python3.10/site-packages/wcwidth/textwrap.pyr      s
   
 r   z*\x1b]8;([^;]*);([^\x07\x1b]*)(\x07|\x1b\\)seqr   return_HyperlinkState | Nonec                 C  s2   t |  }rt|d|d|ddS dS )z0Parse OSC 8 open sequence, return state or None.   r      r   r   r   N)_HYPERLINK_OPEN_REmatchr   group)r   mr   r   r   _parse_hyperlink_open'   s    r#   r   r   r   c                 C  s   d| d|  | S )zGenerate OSC 8 open sequence.z]8;;r   r   r   r   r   _make_hyperlink_open.   s   r%   c                 C  s
   d|  S )zGenerate OSC 8 close sequence.z]8;;r   )r   r   r   r   _make_hyperlink_close3   s   
r&   c                      s   e Zd ZdZd6ddddd7 fddZed8ddZd9ddZd:ddZd:ddZ	d;ddZ
d<d!d"Zd=d%d&Zd>d*d+Zd?d-d.Zd@d0d1Zd9d2d3Zd:d4d5Z  ZS )ASequenceTextWrappera  
    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.
    F   parse   r   control_codestabsizeambiguous_widthr   intr,   $Literal['parse', 'strict', 'ignore']r-   r.   kwargsr   r   Nonec                  s,   t  jdd|i| || _|| _|| _dS )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`.
        r   Nr   )super__init__r,   r-   r.   )selfr   r,   r-   r.   r1   	__class__r   r   r4   L   s   
zSequenceTextWrapper.__init__r   c                   C  s
   t dS )z7Generate unique hyperlink id as 8-character hex string.   )secrets	token_hexr   r   r   r   _next_hyperlink_id_   s   
z&SequenceTextWrapper._next_hyperlink_idtextc                 C  s   t || j| j| jdS )z,Measure text width accounting for sequences.r+   )_widthr,   r-   r.   r5   r<   r   r   r   r=   d   s   zSequenceTextWrapper._widthc                 C  s.   g }t |D ]\}}|s|| qd|S )z'Strip all terminal sequences from text. r   appendjoinr5   r<   resultsegmentis_seqr   r   r   _strip_sequencesi      

z$SequenceTextWrapper._strip_sequencesc                 C  s.   g }t |D ]\}}|r|| qd|S )z*Extract only terminal sequences from text.r?   r@   rC   r   r   r   _extract_sequencesq   rH   z&SequenceTextWrapper._extract_sequences	list[str]c                 C  st  g }d}d}d}t |D ]S\}}|s:|r%|r%|d  s%|d7 }|| |D ]}|d7 }|| ||7 }q'd}q|d}	|drW|rW|d  sW|	sW|d7 }|| |t|7 }|	}q|| tj| |}
|
ss|rs|gS g }d}t|
}t|
D ]8\}}t|}|dkrdn||d  }||d kr|d }n||| d  }||kr||||  ||7 }q|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.
        r?   r   F r   z]8;;\z]8;;z])	r   isspacerA   
startswithlentextwrapTextWrapper_split	enumerate)r5   r<   char_endstripped_textoriginal_posprev_was_hyperlink_closerE   rF   charis_hyperlink_closestripped_chunksrD   stripped_pos
num_chunksidxchunk	chunk_len
start_origend_origr   r   r   rS   y   sP   








zSequenceTextWrapper._splitchunksc                 C  s|  |sg S | j dur)| j dkr| j}n| j}| || | j  | jkr)tdg }d}d}d}tt	|}|r<g }d}|rC| jn| j}| j| | }	|durct
|j|j|j}
|
|d  |d< | |d }| jr|r|r| s| |d }|d= |r|r||d  |d< |r|d }| |}|| |	kr||  ||7 }nn|s|r| |d |	kr| ||||	 | d|}|r|d s|d= |r|d r|r| |d nd}| jr|r|r| s| |d }|| |d 8 }|d= |r|r|d | |d< |r:| p+| jo+t|dko+| |d   }| j du sDt|d | j k sD|r||	krd|}| ||}|dur|du rzd|jv rc|j}n|jrsd|   d	|j }nd|   }|t|j7 }d|jvrt
|j|j|j}t
|j||j}|||d}t|j||j}nd}d}| jr| }|||  d
}n|| | j}|r| |d }| r|| |	krd|}| ||}|dur|t|j7 }||| | j  	 	 |S || |d 8 }|d= |s|r-| |d }| || | jkr-|| j |d< 	 |S ||| j   	 |S |s:|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.
        Nr   z#placeholder too large for max widthTr   rM   r?   zid=:F)	max_linessubsequent_indentinitial_indentr=   placeholderlstripr   
ValueErrorlistreversedr%   r   r   r   rG   drop_whitespacestriprI   rA   pop_handle_long_wordrB   rP   _track_hyperlink_stater;   r&   replacer   rstrip_rstrip_visible)r5   rc   indentlinesis_first_linehyperlink_statecurrent_hyperlink_idcurrent_linecurrent_width
line_widthopen_seqstripped	sequencesr_   chunk_widthstripped_lastno_more_contentline_content	new_stateold_opennew_openplaceholder_w	last_text	prev_liner   r   r   _wrap_chunks   s  













  z SequenceTextWrapper._wrap_chunksstater   c                 C  sD   t |D ]\}}|rt|}|dur|jr|}q|drd}q|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.
        NrL   )r   r#   r   rO   )r5   r<   r   rE   rF   parsed_linkr   r   r   rq     s   

z*SequenceTextWrapper._track_hyperlink_statereversed_chunkscur_linecur_lenc                 C  s   |dk rd}n|| }|d }| j rod}d}| jrF| |}	t|	|krF|	dd|}
|
dkrFtdd |	d|
 D rF| ||
d }d	}|rK|}n| ||}|dkr\|s\| |}|	|d|  ||d |d< dS |sz|	|
  dS dS )
z
        Sequence-aware :meth:`textwrap.TextWrapper._handle_long_word`.

        This method ensures that word boundaries are not broken mid-sequence, and respects grapheme
        cluster boundaries when breaking long words.
        r   rM   Fr   -c                 s  s    | ]}|d kV  qdS )r   Nr   ).0cr   r   r   	<genexpr>  s    z8SequenceTextWrapper._handle_long_word.<locals>.<genexpr>NT)break_long_wordsbreak_on_hyphensrG   rP   rfindany_map_stripped_pos_to_original_find_break_position_find_first_grapheme_endrA   ro   )r5   r   r   r   r   
space_leftr_   break_at_hyphen
hyphen_endr~   
hyphen_pos
actual_endr   r   r   rp     s0   	
"
z%SequenceTextWrapper._handle_long_wordr\   c                 C  sh   d}d}t |D ])\}}|r|t|7 }q|t| |kr%|||    S |t|7 }|t|7 }q|S )z?Map a position in stripped text back to original text position.r   )r   rP   )r5   r<   r\   stripped_idxoriginal_idxrE   rF   r   r   r   r     s   z1SequenceTextWrapper._map_stripped_pos_to_original	max_widthc           	      C  s   d}d}|t |k rD|| }|dkrt||}|r| }qtt||d}| |}|| |kr4|S ||7 }|t |7 }|t |k s
|S )z;Find string index in text that fits within max_width cells.r   )start)rP   r
   r    endnextr   r=   )	r5   r<   r   r^   width_so_farrY   r    graphemegrapheme_widthr   r   r   r     s"   
z(SequenceTextWrapper._find_break_positionc                 C  s   t tt|S )z,Find the end position of the first grapheme.)rP   r   r   r>   r   r   r   r      s   z,SequenceTextWrapper._find_first_grapheme_endc                 C  s   t t|}d}t|D ]\}\}}|s| r|}q|dkr!dS g }t|D ]#\}\}}||k r7|| q'||krC||  q'|rJ|| q'd|S )zAStrip trailing visible whitespace, preserving trailing sequences.rM   r?   )rk   r   rT   rs   rA   rB   )r5   r<   segmentslast_visirE   rF   rD   r   r   r   rt     s$   

z#SequenceTextWrapper._rstrip_visibler(   )r   r/   r,   r0   r-   r/   r.   r/   r1   r   r   r2   )r   r   )r<   r   r   r/   )r<   r   r   r   )r<   r   r   rJ   )rc   rJ   r   rJ   )r<   r   r   r   r   r   )
r   rJ   r   rJ   r   r/   r   r/   r   r2   )r<   r   r\   r/   r   r/   )r<   r   r   r/   r   r/   )r   r   r   r   r4   staticmethodr;   r=   rG   rI   rS   r   rq   rp   r   r   r   rt   __classcell__r   r   r6   r   r'   8   s*    




^ 
6

1

r'   r(   r)   r*   Tr?   Fz [...])r,   r-   expand_tabsreplace_whitespacer.   rg   rf   fix_sentence_endingsr   r   rm   re   rh   r	   r<   r   r/   r,   r0   r-   r   boolr   r.   rg   rf   r   r   r   rm   re   
int | Nonerh   r	   rJ   c                C  s>   t |||||||||	|
||||d}|| }|rt|}|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 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)
        ['中文', '字符']
    )r   r,   r-   r   r   r.   rg   rf   r   r   r   rm   re   rh   )r'   wrap_propagate_sgr)r<   r   r,   r-   r   r   r.   rg   rf   r   r   r   rm   re   rh   r	   wrapperrv   r   r   r   r     s(   c
r   )r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   )"r<   r   r   r/   r,   r0   r-   r/   r   r   r   r   r.   r/   rg   r   rf   r   r   r   r   r   r   r   rm   r   re   r   rh   r   r	   r   r   rJ   )r   
__future__r   rer9   rQ   typingr   r   wcwidthr   r=   r   r   r   	sgr_stater	   r   escape_sequencesr
   r   r   r   compiler   r#   r%   r&   rR   r'   r   r   r   r   r   <module>   sL    
	


   c