
    Zi0                       d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 erddlm
Z
  G d de          Z ej        d	          Z ej        d
          ZdZ G d de	          Z e            Zd"dZd#dZd$dZd%dZd&dZd'd!ZdS )(as  
SGR (Select Graphic Rendition) state tracking for terminal escape sequences.

This module provides functions for tracking and propagating terminal styling (bold, italic, colors,
etc.) via public API propagate_sgr(), and its dependent functions, cut() and wrap(). It only has
attributes necessary to perform its functions, eg 'RED' and 'BLUE' attributes are not defined.
    )annotationsN)IntEnum)TYPE_CHECKINGIterator
NamedTuple)Sequencec                      e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d S )!_SGRz
    SGR (Select Graphic Rendition) parameter codes.

    References:
    - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
    - https://github.com/tehmaze/ansi/tree/master/ansi/colour
    r                           	                              %   &   '   (   /   0   1   Z   a   d   k   N)"__name__
__module____qualname____doc__RESETBOLDDIMITALIC	UNDERLINEBLINKRAPID_BLINKINVERSEHIDDENSTRIKETHROUGHDOUBLE_UNDERLINEBOLD_DIM_OFF
ITALIC_OFFUNDERLINE_OFF	BLINK_OFFINVERSE_OFF
HIDDEN_OFFSTRIKETHROUGH_OFFFG_BLACKFG_WHITEFG_EXTENDED
FG_DEFAULTBG_BLACKBG_WHITEBG_EXTENDED
BG_DEFAULTFG_BRIGHT_BLACKFG_BRIGHT_WHITEBG_BRIGHT_BLACKBG_BRIGHT_WHITE     a/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/wcwidth/sgr_state.pyr
   r
      s          ED
CFIEKGFMLJMIKJHHKJHHKJOOOOOOrK   r
   z\x1b\[([\d;:]*)mz\x1b\[[\d;:]*mz[0mc                      e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dZ	ded<   dZ
ded	<   dZded
<   dZded<   dZded<   dZded<   dZded<   dZded<   dS )	_SGRStatea  
    Track active SGR terminal attributes by category (immutable).

    :param bold: Bold attribute (SGR 1).
    :param dim: Dim/faint attribute (SGR 2).
    :param italic: Italic attribute (SGR 3).
    :param underline: Underline attribute (SGR 4).
    :param blink: Slow blink attribute (SGR 5).
    :param rapid_blink: Rapid blink attribute (SGR 6).
    :param inverse: Inverse/reverse attribute (SGR 7).
    :param hidden: Hidden/invisible attribute (SGR 8).
    :param strikethrough: Strikethrough attribute (SGR 9).
    :param double_underline: Double underline attribute (SGR 21).
    :param foreground: Foreground color as tuple of SGR params, or None for default.
    :param background: Background color as tuple of SGR params, or None for default.
    Fboolbolddimitalic	underlineblinkrapid_blinkinversehiddenstrikethroughdouble_underlineNtuple[int, ...] | None
foreground
background)r(   r)   r*   r+   rP   __annotations__rQ   rR   rS   rT   rU   rV   rW   rX   rY   r[   r\   rJ   rK   rL   rN   rN   J   s          " DCFIEKGFM""""")-J----)-J------rK   rN   statereturnrO   c                    | j         pP| j        pI| j        pB| j        p;| j        p4| j        p-| j        p&| j        p| j        p| j	        p| j
        dup| j        duS )z
    Return True if any attributes are set.

    :param state: The SGR state to check.
    :returns: True if any attribute differs from default.
    N)rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r[   r\   )r^   s    rL   _sgr_state_is_activera   n   s     J L%) Lu| Lu L{L#/L38=LDILL"L&+&<L t+L 05/?t/KMrK   strc                
   t          |           sdS | j        df| j        df| j        df| j        df| j        df| j        df| j        df| j        d	f| j	        d
f| j
        dfg
}d |D             }| j        7|                    d                    d | j        D                                  | j        7|                    d                    d | j        D                                  dd                    |           dS )z
    Generate minimal SGR sequence to restore this state from reset.

    :param state: The SGR state to convert.
    :returns: SGR escape sequence string, or empty string if no attributes set.
     12345678921c                    g | ]	\  }}||
S rJ   rJ   ).0activecodes      rL   
<listcomp>z*_sgr_state_to_sequence.<locals>.<listcomp>   s!    <<<|vtV<d<<<rK   N;c              3  4   K   | ]}t          |          V  d S Nrb   rp   ps     rL   	<genexpr>z)_sgr_state_to_sequence.<locals>.<genexpr>   (      @@!s1vv@@@@@@rK   c              3  4   K   | ]}t          |          V  d S rv   rw   rx   s     rL   rz   z)_sgr_state_to_sequence.<locals>.<genexpr>   r{   rK   z[m)ra   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r[   appendjoinr\   )r^   
bool_attrsparamss      rL   _sgr_state_to_sequencer   {   s)     && r 
SEIs+elC-@	#c 2U5F4L	u|S1E4G3M		&	J =<z<<<F #chh@@u/?@@@@@AAA#chh@@u/?@@@@@AAA&388F##&&&&rK   sequencelist[int | tuple[int, ...]]c                   t                               |           }|sg S |                    d          }|sdgS g }|                    d          D ]n}d|v rBd |                    d          D             }|                    t          |                     H|                    |rt          |          nd           o|S )a  
    Parse SGR sequence and return list of parameter values.

    Handles compound sequences like ``\x1b[1;31;4m`` -> [1, 31, 4].
    Empty params (e.g., ``\x1b[m``) are treated as [0] (reset).
    Colon-separated extended colors like ``\x1b[38:2::255:0:0m`` are returned
    as tuples: [(38, 2, 255, 0, 0)].

    :param sequence: SGR escape sequence string.
    :returns: List of integer parameters or tuples for colon-separated colors.
    r   r   rt   :c                4    g | ]}|rt          |          nd S )r   )intrx   s     rL   rs   z%_parse_sgr_params.<locals>.<listcomp>   s'    BBBAq'SVVVaBBBrK   )_SGR_PATTERNmatchgroupsplitr~   tupler   )r   r   
params_strresultparampartss         rL   _parse_sgr_paramsr      s     x((E 	QJ s
*,F!!#&& 6 6%<< CBS1A1ABBBEMM%,,''''MM4#e***15555MrK   r   Iterator[int | tuple[int, ...]]baser   rZ   c                   	 t          |           }t          |t                    rdS |dk    r8t          |           }t          |t                    rdS t          |          d|fS |dk    rt          |           t          |           t          |           }}}t          |t                    s*t          |t                    st          |t                    rdS t          |          d|||fS n# t          $ r Y nw xY wdS )a9  
    Parse extended color (256-color or RGB) from parameter iterator.

    :param params: Iterator of remaining SGR parameters (semicolon-separated format).
    :param base: Base code (38 for foreground, 48 for background).
    :returns: Color tuple like (38, 5, N) or (38, 2, R, G, B), or None if malformed.
    Nr   r   )next
isinstancer   r   StopIteration)r   r   modenrgbs          rL   _parse_extended_colorr      s   F||dE"" 	4199VA!U## tIIq!$$1996llDLL$v,,!qA!U## z!U';'; z!U?S?S tIIq!Q**	 
    4s)   $C/ *C/ C/ &A2C/ C/ /
C<;C<c                (
   t          |          }t          |          }|D ]}t          |t                    rt	          |          dk    r-|d         t
          j        k    r|                     |          } n?t	          |          dk    r,|d         t
          j        k    r|                     |          } |t
          j	        k    rt          } |t
          j        k    r|                     d          } |t
          j        k    r|                     d          } |t
          j        k    r|                     d          } &|t
          j        k    r|                     d	          } N|t
          j        k    r|                     d
          } v|t
          j        k    r|                     d          } |t
          j        k    r|                     d          } |t
          j        k    r|                     d          } |t
          j        k    r|                     d          } |t
          j        k    r|                     d          } >|t
          j        k    r|                     dd          } g|t
          j        k    r|                     d          } |t
          j        k    r|                     dd          } |t
          j        k    r|                     dd          } |t
          j        k    r|                     d          } 	|t
          j        k    r|                     d          } 1|t
          j        k    r|                     d          } Yt
          j        |cxk    rt
          j        k    s&n t
          j        |cxk    rt
          j        k    rn n|                     |f          } t
          j         |cxk    rt
          j!        k    s&n t
          j"        |cxk    rt
          j#        k    rn n|                     |f          } |t
          j$        k    r|                     d          } A|t
          j%        k    r|                     d          } i|t
          j        k    r4tM          |t
          j                  x}r|                     |          } |t
          j        k    r2tM          |t
          j                  x}r|                     |          } | S )z
    Parse SGR sequence and return new state with updates applied.

    :param state: Current SGR state.
    :param sequence: SGR escape sequence string.
    :returns: New SGRState with updates applied.
    r   r   )r[   )r\   T)rP   )rQ   )rR   )rS   )rT   )rU   )rV   )rW   )rX   )rY   F)rP   rQ   )rS   rY   )rT   rU   N)'r   iterr   r   lenr
   r@   _replacerD   r,   _SGR_STATE_DEFAULTr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   rF   rG   rB   rC   rH   rI   rA   rE   r   )r^   r   params_listr   ry   colors         rL   _sgr_state_updater      s    $H--K+F A9 A9a 	1vv{{qtt'777!44Q11)9!9!9!44
??&EE$)^^NNN--EE$(]]NNtN,,EE$+NN$N//EE$.  NNTN22EE$*__NNN..EE$"""NNtN44EE$,NN4N00EE$+NN$N//EE$$$$NNN66EE$'''NNDN99EE$###NN5N99EE$/!!NN%N00EE$$$$NNUUNKKEE$.  NNENBBEE$"""NN5N11EE$/!!NN%N00EE$(((NNN77EEmq1111DM1111%BBBBd.BBBBBBNNqdN33EEmq1111DM1111%BBBBd.BBBBBBNNqdN33EE$/!!NNdN33EE$/!!NNdN33EE$"""-fd6FGGGu 9%88$"""-fd6FGGGu 9%88LrK   linesSequence[str]	list[str]c                   t          d | D                       r| st          |           S g }t          }| D ]}t          |          }t                              |          D ]$}t          ||                                          }%|r||z   n|}t          |          r
|t          z   }|
                    |           |S )a(  
    Propagate SGR codes across wrapped lines.

    When text with SGR styling is wrapped across multiple lines, each line
    needs to be self-contained for proper display. This function:

    - Ends each line with ``\x1b[0m`` if styles are active (prevents bleeding)
    - Starts each subsequent line with the active style restored

    :param lines: List of text lines, possibly containing SGR sequences.
    :returns: List of lines with SGR codes propagated.

    Example::

        >>> propagate_sgr(['\x1b[31mhello', 'world\x1b[0m'])
        ['\x1b[31mhello\x1b[0m', '\x1b[31mworld\x1b[0m']

    This is useful in cases of making special editors and viewers, and is used for the
    default modes (propagate_sgr=True) of :func:`wcwidth.width` and :func:`wcwidth.clip`.

    When wrapping and clipping text containing SGR sequences, maybe a previous line enabled the BLUE
    color--if we are viewing *only* the line following, we would want the carry over the BLUE color,
    and all lines with sequences should end with terminating reset (``\x1b[0m``).
    c              3  J   K   | ]}t                               |          V  d S rv   )_SGR_QUICK_CHECKsearch)rp   lines     rL   rz   z propagate_sgr.<locals>.<genexpr>=  s1      ??&&t,,??????rK   )anylistr   r   r   finditerr   r   ra   
_SGR_RESETr~   )r   r   r^   r   prefixr   output_lines          rL   propagate_sgrr   #  s    4 ??????? u E{{FE # #'.. "**400 	< 	<E%eU[[]];;EE (.7ftmm4&& 	3%
2Kk""""MrK   )r^   rN   r_   rO   )r^   rN   r_   rb   )r   rb   r_   r   )r   r   r   r   r_   rZ   )r^   rN   r   rb   r_   rN   )r   r   r_   r   )r+   
__future__r   reenumr   typingr   r   r   r   r
   compiler   r   r   rN   r   ra   r   r   r   r   r   rJ   rK   rL   <module>r      s    # " " " " " 
			       6 6 6 6 6 6 6 6 6 6  & & & & &7 & & &Z rz-.. 2:/00  
. . . . .
 . . .B Y[[ 
M 
M 
M 
M' ' ' '8   <   :N N N Nb/ / / / / /rK   