o
    Œõ±j3  ã                   @  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d0d#d$„Zd1d%d&„Zd2d*d+„ZdS )3as  
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   ú„/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/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   K   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_activeo   s"   ÿÿÿÿþþýýr\   Úcolorútuple[int, ...]Ústrc                 C  s@   t | ƒdkr| d dkrd dd„ | D ƒ¡S d dd„ | D ƒ¡S )	aÃ  
    Join color parameters, preserving the ITU T.416 colon form where it was used.

    ``38:2:<colour space id>:R:G:B`` carries a colour space element that the legacy
    ``38;2;R;G;B`` form has no slot for, so joining it with ``;`` would shift R, G and B
    one position and leave a trailing parameter.

    :param color: Color parameters as parsed by :func:`_parse_sgr_params`.
    :returns: Parameter string for embedding in an SGR sequence.
    r   r	   r
   ú:c                 s  ó   � | ]}t |ƒV  qd S ©N©r_   ©Ú.0ÚprH   rH   rI   Ú	<genexpr>ˆ   ó   € z'_color_params_to_str.<locals>.<genexpr>ú;c                 s  ra   rb   rc   rd   rH   rH   rI   rg   ‰   rh   )ÚlenÚjoin)r]   rH   rH   rI   Ú_color_params_to_str|   s   rl   c                 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rD| t| jƒ¡ | jdurQ| t| jƒ¡ 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   )re   ÚactiveÚcoderH   rH   rI   Ú
<listcomp>�   s    z*_sgr_state_to_sequence.<locals>.<listcomp>Nz[ri   Úm)r\   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rW   Úappendrl   rX   rk   )rZ   Ú
bool_attrsÚparamsrH   rH   rI   Ú_sgr_state_to_sequenceŒ   s   ü

r   Ú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   ri   r`   c                 S  s   g | ]
}|r
t |ƒnd ‘qS )r   )Úintrd   rH   rH   rI   rz   ¿   s    z%_parse_sgr_params.<locals>.<listcomp>)Ú_SGR_PATTERNÚmatchÚgroupÚsplitr|   Útupler‚   )r€   r„   Ú
params_strÚresultÚparamÚpartsrH   rH   rI   Ú_parse_sgr_params¨   s   

rŒ   r~   ú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)r~   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�r‘t&|tjƒ }�r�| j|d�} q
|tjk�r¦t&|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‡   rj   r   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_listr~   rf   r]   rH   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.wrap` 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 rb   )Ú_SGR_QUICK_CHECKÚsearch)re   ÚlinerH   rH   rI   rg   N  s   € z propagate_sgr.<locals>.<genexpr>)ÚanyÚlistrš   r   rƒ   Úfinditerrœ   r…   r\   Ú
_SGR_RESETr|   )r�   r‰   rZ   r¢   Úprefixr„   Úoutput_linerH   rH   rI   Úpropagate_sgr4  s   r©   )rZ   rJ   r[   rK   )r]   r^   r[   r_   )rZ   rJ   r[   r_   )r€   r_   r[   r�   )r~   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\   rl   r   rŒ   r—   rœ   r©   rH   rH   rH   rI   Ú<module>   s(    
-
!





Q