
    ^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y)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!y )!_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     W/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/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OrK   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<   y)	_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FDItE4KGTFDM4"d")-J&-)-J&-rK   rN   c                V   | j                   xs | j                  xs | j                  xs | j                  xsr | j                  xsd | j
                  xsV | j                  xsH | j                  xs: | j                  xs, | j                  xs | j                  duxs | 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\   )states    rL   _sgr_state_is_activer`   n   s     JJ L%)) Lu|| Lu L{{L#//L38==LDILLL""L&+&<&<L t+L 05/?/?t/KMrK   c                z   t        |       sy| 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 cg c]
  \  }}|s	| }}}| j                  1|j                  dj                  d | j                  D                     | j                  1|j                  dj                  d | j                  D                     ddj                  |       dS c c}}w )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21;c              3  2   K   | ]  }t        |        y wNstr.0ps     rL   	<genexpr>z)_sgr_state_to_sequence.<locals>.<genexpr>        @!s1v@   c              3  2   K   | ]  }t        |        y wro   rp   rr   s     rL   ru   z)_sgr_state_to_sequence.<locals>.<genexpr>   rv   rw   z[m)r`   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r[   appendjoinr\   )r_   
bool_attrsactivecodeparamss        rL   _sgr_state_to_sequencer   {   s%     & 
SEIIs+ellC-@	#c 2U5F5F4L	u||S1E4G4G3M			&	J (2<|vtVd<F< #chh@u/?/?@@A#chh@u/?/?@@A388F#$A&& =s   
D7!D7c                n   t         j                  |       }|sg S |j                  d      }|sdgS g }|j                  d      D ]j  }d|v rF|j                  d      D cg c]  }|rt	        |      nd }}|j                  t        |             M|j                  |rt	        |      nd       l |S c c}w )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   :)_SGR_PATTERNmatchgroupsplitintrz   tuple)sequencer   
params_strresultparamrt   partss          rL   _parse_sgr_paramsr      s     x(E	QJs
*,F!!#& 6%< 27S1ABAqSVa'BEBMM%,'MM#e*156 M	 Cs   B2c                   	 t        |       }t        |t              ry|dk(  r*t        |       }t        |t              ryt        |      d|fS |dk(  rbt        |       t        |       t        |       }}}t        |t              s t        |t              st        |t              ryt        |      d|||fS 	 y# t        $ r Y yw xY 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   basemodenrgbs          rL   _parse_extended_colorr      s    F|dE"19VA!U#Iq!$$196lDL$v,!qA!U#z!U';z!U?SIq!Q**	    s(   B6  B6 B6 AB6 $B6 6	CCc                	   t        |      }t        |      }|D ]  }t        |t              rnt	        |      dk\  r)|d   t
        j                  k(  r| j                  |      } n6t	        |      dk\  r(|d   t
        j                  k(  r| j                  |      } |t
        j                  k(  rt        } |t
        j                  k(  r| j                  d      } |t
        j                  k(  r| j                  d      } |t
        j                  k(  r| j                  d      } |t
        j                  k(  r| j                  d	      } 6|t
        j                  k(  r| j                  d
      } ]|t
        j                   k(  r| j                  d      } |t
        j"                  k(  r| j                  d      } |t
        j$                  k(  r| j                  d      } |t
        j&                  k(  r| j                  d      } |t
        j(                  k(  r| j                  d      }  |t
        j*                  k(  r| j                  dd      } H|t
        j,                  k(  r| j                  d      } o|t
        j.                  k(  r| j                  dd      } |t
        j0                  k(  r| j                  dd      } |t
        j2                  k(  r| j                  d      } |t
        j4                  k(  r| j                  d      } |t
        j6                  k(  r| j                  d      } 4t
        j8                  |cxk  rt
        j:                  k  s,n t
        j<                  |cxk  rt
        j>                  k  rn n| j                  |f      } t
        j@                  |cxk  rt
        jB                  k  s,n t
        jD                  |cxk  rt
        jF                  k  rn n| j                  |f      } |t
        jH                  k(  r| j                  d      } +|t
        jJ                  k(  r| j                  d      } R|t
        j                  k(  r2tM        |t
        j                        x}s| j                  |      } |t
        j                  k(  stM        |t
        j                        x}s| j                  |      }  | 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   rt   colors         rL   _sgr_state_updater      s    $H-K+F A9a1v{qtt'7'77!4Q11)9)9!9!4

?&E$))^NNN-E$((]NNtN,E$++NN$N/E$.. NNTN2E$**_NNN.E$"""NNtN4E$,,NN4N0E$++NN$N/E$$$$NNN6E$'''NNDN9E$###NN5N9E$//!NN%N0E$$$$NNUUNKE$.. NNENBE$"""NN5N1E$//!NN%N0E$(((NNN7Emmq1DMM1%%Bd.B.BBNNqdN3Emmq1DMM1%%Bd.B.BBNNqdN3E$//!NNdN3E$//!NNdN3E$"""-fd6F6FGGuG%8$"""-fd6F6FGGuG%8CA9D LrK   c                <   t        d | D              r| st        |       S g }t        }| D ]o  }t        |      }t        j                  |      D ]  }t        ||j                               } |r||z   n|}t        |      r	|t        z   }|j                  |       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              3  F   K   | ]  }t         j                  |        y wro   )_SGR_QUICK_CHECKsearch)rs   lines     rL   ru   z propagate_sgr.<locals>.<genexpr>=  s     ?&&t,?s   !)anylistr   r   r   finditerr   r   r`   
_SGR_RESETrz   )linesr   r_   r   prefixr   output_lines          rL   propagate_sgrr   #  s    4 ???uE{FE #'. "**40 	<E%eU[[];E	< (.ftm4&%
2Kk"# MrK   )r_   rN   returnrO   )r_   rN   r   rq   )r   rq   r   zlist[int | tuple[int, ...]])r   zIterator[int | tuple[int, ...]]r   r   r   rZ   )r_   rN   r   rq   r   rN   )r   zSequence[str]r   z	list[str])r+   
__future__r   reenumr   typingr   r   r   r   r
   compiler   r   r   rN   r   r`   r   r   r   r   r   rJ   rK   rL   <module>r      s    # 
  6 6&7 &Z rzz-. 2::/0  
.
 .B [ 
M'8<+36:Nb/rK   