o
    aĎi0                     @  s   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 deZed	Zed
ZdZG dd de	Ze 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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                   @  s   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 rH   rH   M/home/jeff/fluffinator/venv/lib/python3.10/site-packages/wcwidth/sgr_state.pyr      s@    r   z\x1b\[([\d;:]*)mz\x1b\[[\d;:]*mz[0mc                   @  s   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)   rL   __annotations__rM   rN   rO   rP   rQ   rR   rS   rT   rU   rW   rX   rH   rH   rH   rI   rJ   J   s   
 rJ   statereturnrK   c                 C  sP   | j p'| jp'| jp'| jp'| jp'| 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)rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rW   rX   )rZ   rH   rH   rI   _sgr_state_is_activen   s"   r\   strc                 C  s   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 |D }| jdurJ|ddd | jD  | jdur]|d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                 S  s   g | ]\}}|r|qS rH   rH   ).0activecoderH   rH   rI   
<listcomp>   s    z*_sgr_state_to_sequence.<locals>.<listcomp>N;c                 s      | ]}t |V  qd S Nr]   ri   prH   rH   rI   	<genexpr>       z)_sgr_state_to_sequence.<locals>.<genexpr>c                 s  rn   ro   rp   rq   rH   rH   rI   rs      rt   z[m)r\   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rW   appendjoinrX   )rZ   
bool_attrsparamsrH   rH   rI   _sgr_state_to_sequence{   s   

rz   sequencelist[int | tuple[int, ...]]c                 C  s   t | }|s	g S |d}|sdgS g }|dD ]#}d|v r2dd |dD }|t| q||r:t|nd q|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   rm   :c                 S  s   g | ]
}|r
t |nd qS )r   )intrq   rH   rH   rI   rl      s    z%_parse_sgr_params.<locals>.<listcomp>)_SGR_PATTERNmatchgroupsplitrv   tupler~   )r{   r   
params_strresultparampartsrH   rH   rI   _parse_sgr_params   s   

r   ry   Iterator[int | tuple[int, ...]]baser~   rV   c                 C  s   zUt | }t|trW dS |dkr%t | }t|trW dS t|d|fW S |dkrSt | t | t | }}}t|tsFt|tsFt|trIW dS t|d|||fW S W dS  ty_   Y dS w )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)ry   r   modenrgbrH   rH   rI   _parse_extended_color   s(   


r   c                 C  sR  t |}t|}|D ]}t|tr:t|dkr&|d tjkr&| j|d} nt|dkr9|d tjkr9| j|d} q
|tj	krBt
} q
|tjkrN| jdd} q
|tjkrZ| jdd} q
|tjkrf| jdd} q
|tjkrr| jdd	} q
|tjkr~| jdd
} q
|tjkr| jdd} q
|tjkr| jdd} q
|tjkr| jdd} q
|tjkr| jdd} q
|tjkr| jdd} q
|tjkr| jddd} q
|tjkr| jdd} q
|tjkr| jddd} q
|tjkr| jddd} q
|tjkr| jdd} q
|tjkr| jdd} q
|tjkr| jdd} q
tj|  kr!tjks2n tj|  kr0tjkr:n n| j|fd} q
tj |  krHtj!ksYn tj"|  krWtj#kran n| j|fd} q
|tj$krn| jdd} q
|tj%kr{| jdd} q
|tjkrt&|tj }r| j|d} q
|tjkrt&|tj }r| j|d} q
| 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   )rW   )rX   T)rL   )rM   )rN   )rO   )rP   )rQ   )rR   )rS   )rT   )rU   F)rL   rM   )rO   rU   )rP   rQ   N)'r   iterr   r   lenr   r>   _replacerB   r*   _SGR_STATE_DEFAULTr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   rD   rE   r@   rA   rF   rG   r?   rC   r   )rZ   r{   params_listry   rr   colorrH   rH   rI   _sgr_state_update   s   


















  r   linesSequence[str]	list[str]c                 C  s   t dd | D r| st| S g }t}| D ]*}t|}t|D ]	}t|| }q |r0|| n|}t|r:|t	 }|
| q|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                 s  s    | ]}t |V  qd S ro   )_SGR_QUICK_CHECKsearch)ri   linerH   rH   rI   rs   =  s    z propagate_sgr.<locals>.<genexpr>)anylistr   rz   r   finditerr   r   r\   
_SGR_RESETrv   )r   r   rZ   r   prefixr   output_linerH   rH   rI   propagate_sgr#  s   r   )rZ   rJ   r[   rK   )rZ   rJ   r[   r]   )r{   r]   r[   r|   )ry   r   r   r~   r[   rV   )rZ   rJ   r{   r]   r[   rJ   )r   r   r[   r   )r)   
__future__r   reenumr   typingr   r   r   r   r   compiler   r   r   rJ   r   r\   rz   r   r   r   r   rH   rH   rH   rI   <module>   s&    
-
!




Q