o
    MEgg\                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZmZmZ dd	lmZmZ eed
f ZG dd dZG dd
 d
Ze ZG dd dZdS )    N)	lru_cache)randint)time)AnyDictIterableListOptionalTypeUnion   )errors)ColorColorParseErrorColorSystem	blend_rgb)DEFAULT_TERMINAL_THEMETerminalThemeStylec                   @   sF   e Zd ZdZdgZdeddfddZdd	d
ed	 dee	 fddZ
dS )_Bitz.A descriptor to get/set a style attribute bit.bitbit_noreturnNc                 C   s   d|> | _ d S )Nr   )r   )selfr    r   d/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/rich/style.py__init__      z_Bit.__init__objr   objtypec                 C   s    |j | j@ r|j| j@ dkS d S )Nr   )_set_attributesr   _attributes)r   r   r   r   r   r   __get__   s   z_Bit.__get__)__name__
__module____qualname____doc__	__slots__intr   r
   r	   boolr"   r   r   r   r   r      s
    "r   c                "   @   s  e Zd ZU dZee ed< ee ed< eed< eed< eed< eed< g dZ	d	d
ddddddddddddZ
ddddddddddddddddddeeeef  deeeef  dee dee dee dee dee d ee d!ee d"ee d#ee d$ee d%ee d&ee d'ee d(ee f d)d*Zed|d,d-Ze	d}dee dee d+d fd.d/Zed0Zed1Zed2Zed3Zed4Zed5Zed6Zed7Zed8Zed9Zed:Zed;Zed<Zed+efd=d>Z d+efd?d@Z!d+efdAdBZ"dCe#d+efdDdEZ$ee%dFdGdHed+efdIdJZ&edKee' d+e'fdLdMZ(d+efdNdOZ)dPe*d+efdQdRZ+d+efdSdTZ,ed+ee fdUdVZ-ed+ee fdWdXZ.ed+ee fdYdZZ/ed+efd[d\Z0ed|d]d^Z1ed|d_d`Z2ee%dadGdbed+d fdcddZ3e%dFdGd~deee4 d+efdfdgZ5edhe6d  d+d fdidjZ7eddkdlZ8d|dmdnZ9d~d(ee d+d fdodpZ:	qde#j;drdsdtedCee# dued+efdvdwZ<d~dtee d+dfdxdyZ=dHed  d+d fdzd{Z>dS )r   a  A terminal style.

    A terminal style consists of a color (`color`), a background color (`bgcolor`), and a number of attributes, such
    as bold, italic etc. The attributes have 3 states: they can either be on
    (``True``), off (``False``), or not set (``None``).

    Args:
        color (Union[Color, str], optional): Color of terminal text. Defaults to None.
        bgcolor (Union[Color, str], optional): Color of terminal background. Defaults to None.
        bold (bool, optional): Enable bold text. Defaults to None.
        dim (bool, optional): Enable dim text. Defaults to None.
        italic (bool, optional): Enable italic text. Defaults to None.
        underline (bool, optional): Enable underlined text. Defaults to None.
        blink (bool, optional): Enabled blinking text. Defaults to None.
        blink2 (bool, optional): Enable fast blinking text. Defaults to None.
        reverse (bool, optional): Enabled reverse text. Defaults to None.
        conceal (bool, optional): Enable concealed text. Defaults to None.
        strike (bool, optional): Enable strikethrough text. Defaults to None.
        underline2 (bool, optional): Enable doubly underlined text. Defaults to None.
        frame (bool, optional): Enable framed text. Defaults to None.
        encircle (bool, optional): Enable encircled text. Defaults to None.
        overline (bool, optional): Enable overlined text. Defaults to None.
        link (str, link): Link URL. Defaults to None.

    _color_bgcolorr!   r    _hash_null)
r*   r+   r!   r    _link_link_id_ansi_style_definitionr,   r-   12345678921515253)r   r                        	   
         N)colorbgcolorbolddimitalic	underlineblinkblink2reverseconcealstrike
underline2frameencircleoverlinelinkrJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   c                C   s  d | _ d | _dtttf dtfdd}|d u rd n||| _|d u r$d n||| _t|d u|d uo2d|d uo7d|d uo<d|d uoAd|d uoFd	|	d uoKd
|
d uoPd|d uoUd|d uoZd|d uo_d|d uodd|d uoidf| _| jrt|rudpvd|rzdp{d|rdpd|rdpd|rdpd|rd	pd|	rd
pd|
rdpd|rdpd|rdpd|rdpd|rdpd|rdpdfnd| _	|| _
|rt  dtdd nd| _t| j| j| j	| j|f| _| jp|p|p| | _d S )NrJ   r   c                 S   s   t | tr| S t| S N)
isinstancer   parse)rJ   r   r   r   _make_colort   s   z#Style.__init__.<locals>._make_colorr?   rA   rE          @                     r   r   -?B  )r0   r1   r   r   strr*   r+   sumr    r!   r.   r   r   r/   hashr,   r-   )r   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r]   r   r   r   r   ]   sf   











#












 	zStyle.__init__r   c                 C   s   t S )zCCreate an 'null' style, equivalent to Style(), but more performant.)
NULL_STYLE)clsr   r   r   null   s   z
Style.nullc                 C   s^   |  t}d|_d|_||_||_d|_d|_d|_d|_	t
||dddf|_|p*| |_|S )a  Create a new style with colors and no attributes.

        Returns:
            color (Optional[Color]): A (foreground) color, or None for no color. Defaults to None.
            bgcolor (Optional[Color]): A (background) color, or None for no color. Defaults to None.
        Nr   ri   )__new__r   r0   r1   r*   r+   r    r!   r.   r/   rl   r,   r-   )rn   rJ   rK   styler   r   r   
from_color   s&   

	zStyle.from_colorr   r   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   c                 C      | j S )z+Get a link id, used in ansi code for links.)r/   r   r   r   r   link_id      zStyle.link_idc                 C   s  | j du rg }|j}| j}|d@ rE|d@ r|| jrdnd |d@ r+|| jr(dnd |d	@ r8|| jr5d
nd |d@ rE|| jrBdnd |d@ r|d@ rV|| jrSdnd |d@ rc|| jr`dnd |d@ rp|| j	rmdnd |d@ r}|| j
rzdnd |d@ r|| jrdnd |d@ r|d @ r|| jrd!nd" |d#@ r|| jrd$nd% |d&@ r|| jrd'nd( |d)@ r|| jrd*nd+ | jdur|| jj | jdur|d, || jj | jr|d- || j d.|pd/| _ | j S )0z-Re-generate style definition from attributes.N   r   rL   znot boldr?   rM   znot dimrA   rN   z
not italicrE   rO   znot underline  r^   rP   z	not blinkr_   rQ   z
not blink2r`   rR   znot reversera   rS   znot concealrb   rT   z
not strike   rc   rU   znot underline2rd   rV   z	not framere   rW   znot encirclerf   rX   znot overlineonrY    none)r1   appendr    rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   r*   namer+   r.   join)r   
attributesr}   bitsr   r   r   __str__   sV   



zStyle.__str__c                 C   s   | j  S )z;A Style is false if it has no attributes, colors, or links.)r-   rt   r   r   r   __bool__  s   zStyle.__bool__color_systemc                 C   s0  | j du rg }|j}| j}| j| j@ }|rm|d@ r||d  |d@ r)||d  |d@ r3||d  |d@ r=||d  |d@ rUtdd	D ]}|d|> @ rT|||  qF|d
@ rmtd	dD ]}|d|> @ rl|||  q^| jdur}|| j|	  | j
dur|| j
|j	dd d|| _ | j S )zGenerate ANSI codes for this style.

        Args:
            color_system (ColorSystem): Color system.

        Returns:
            str: String containing codes.
        Nr   r   r?   rA   rE   r@   rx   rF   ry      F
foreground;)r0   r}   
_style_mapr!   r    ranger*   extend	downgradeget_ansi_codesr+   r   )r   r   sgrr}   r   r   r   r   r   r   _make_ansi_codes  sD   
	

zStyle._make_ansi_codesrd   )maxsizerq   c                 C   s2   zt | |W S  tjy   |   Y S w )zNormalize a style definition so that styles with the same effect have the same string
        representation.

        Args:
            style (str): A style definition.

        Returns:
            str: Normal form of style definition.
        )rj   r\   r   StyleSyntaxErrorstriplower)rn   rq   r   r   r   	normalize@  s
   zStyle.normalizevaluesc                 G   s"   |D ]
}|dur|  S qt d)zPick first non-None style.Nz$expected at least one non-None style)
ValueError)rn   r   valuer   r   r   
pick_firstQ  s
   zStyle.pick_firstc                 C   s   d|  dS )z9Render a named style differently from an anonymous style.zStyle.parse("z")r   rt   r   r   r   __repr__Y  s   zStyle.__repr__otherc                 C   sJ   t |tstS | j|jko$| j|jko$| j|jko$| j|jko$| j|jkS rZ   )r[   r   NotImplementedr*   r+   r    r!   r.   )r   r   r   r   r   __eq__]  s   




zStyle.__eq__c                 C   rs   rZ   )r,   rt   r   r   r   __hash__h  s   zStyle.__hash__c                 C   rs   )z.The foreground color or None if it is not set.)r*   rt   r   r   r   rJ   k  rv   zStyle.colorc                 C   rs   )z.The background color or None if it is not set.)r+   rt   r   r   r   rK   p  rv   zStyle.bgcolorc                 C   rs   )zLink text, if set.)r.   rt   r   r   r   rY   u  rv   z
Style.linkc                 C   s   | j du p| j jS )z6Check if the style specified a transparent background.N)rK   
is_defaultrt   r   r   r   transparent_backgroundz  s   zStyle.transparent_backgroundc                 C   s   t | jdS )zA Style with background only.)rK   )r   rK   rt   r   r   r   background_style  s   zStyle.background_stylec                 C   sx   | j rtS | t}d|_d|_d|_d|_| j|_| j	|_	| j
|_
| j
r0t  dtdd nd|_| j|_d|_ |S )z+Get a copy of the style with color removed.Nrg   r   rh   ri   Fr-   rm   rp   r   r0   r1   r*   r+   r!   r    r.   r   r   r/   r,   r   rq   r   r   r   without_color  s   
"zStyle.without_colorrf   style_definitionc                 C   s  |  dks|s|  S i ddddddddddddddd	dd
d
dddddddddddddddddddddd}d}d}i }d}t| }|D ]}| }	|	dkrt|d}	|	sotdz	t	|	du  W n t
y }
 ztd|	d|
 dd}
~
ww |	}qY|	dkrt|d}	||	}|du rtd|	d||< qY|	d krt|d}	|	std!|	}qY|	|v rd"|||	 < qYzt	|	 W n t
y }
 ztd|	d#|
 dd}
~
ww |	}qYtd%|||d$|}|S )&a  Parse a style definition.

        Args:
            style_definition (str): A string containing a style.

        Raises:
            errors.StyleSyntaxError: If the style definition syntax is invalid.

        Returns:
            `Style`: A Style instance.
        r|   rM   drL   brN   irO   urP   rQ   rR   rrS   crT   srU   rV   rW   rX   )uurV   rW   rX   oNrz   ri   zcolor expected after 'on'zunable to parse z as background color; notz,expected style attribute after 'not', found FrY   zURL expected after 'link'Tz as color; )rJ   rK   rY   r   )r   ro   itersplitr   nextr   r   r   r\   r   getr   )rn   r   style_attributesrJ   rK   r   rY   wordsoriginal_wordworderror	attributerq   r   r   r   r\     s   	







zStyle.parsethemec                 C   s  |pt }g }|j}| j}| j}| jr||}}| jr0|du r!|jn||}t	t
||jd}|durI||}|d|j  |d|j  |dur\|j|dd}|d|j  | jrc|d | jrj|d	 | jrq|d
 | jrx|d | jr|d d|S )zGet a CSS style rule.Ng      ?zcolor: ztext-decoration-color: Fr   zbackground-color: zfont-weight: boldzfont-style: italicztext-decoration: underlineztext-decoration: line-throughztext-decoration: overlinez; )r   r}   rJ   rK   rR   rM   foreground_colorget_truecolorr   from_tripletr   background_colorhexrL   rN   rO   rT   rX   r   )r   r   cssr}   rJ   rK   r   theme_colorr   r   r   get_html_style  s>   


zStyle.get_html_stylestylesc                 C      t |}t|t|S )zCombine styles and get result.

        Args:
            styles (Iterable[Style]): Styles to combine.

        Returns:
            Style: A new style instance.
        r   rk   r   rn   r   iter_stylesr   r   r   combine     
zStyle.combinec                 G   r   )zCombine styles from positional argument in to a single style.

        Args:
            *styles (Iterable[Style]): Styles to combine.

        Returns:
            Style: A new style instance.
        r   r   r   r   r   chain"  r   zStyle.chainc                 C   s   | j rtS | t}| j|_| j|_| j|_| j|_| j|_| j	|_	| j
|_
| j
r4t  dtdd nd|_| j|_d|_ |S )zwGet a copy of this style.

        Returns:
            Style: A new Style instance with identical attributes.
        rg   r   rh   ri   Fr   r   r   r   r   copy/  s   
"z
Style.copyc                 C   sr   |  t}| j|_| j|_| j|_| j|_| j|_| j|_||_|r-t	  dt
dd nd|_| j|_d|_|S )zGet a copy with a different value for link.

        Args:
            link (str, optional): New value for link. Defaults to None.

        Returns:
            Style: A new Style instance.
        rg   r   rh   ri   F)rp   r   r0   r1   r*   r+   r!   r    r.   r   r   r/   r,   r-   )r   rY   rq   r   r   r   update_linkD  s   
	 zStyle.update_linkri   F)r   legacy_windowstextr   c                C   s^   |r|du r|S |  |}|rd| d| dn|}| jr-|s-d| j d| j d| d}|S )	aD  Render the ANSI codes for the style.

        Args:
            text (str, optional): A string to style. Defaults to "".
            color_system (Optional[ColorSystem], optional): Color system to render to. Defaults to ColorSystem.TRUECOLOR.

        Returns:
            str: A string containing ANSI style codes.
        Nz[mz[0mz]8;id=r   z\z]8;;\)r   r.   r/   )r   r   r   r   attrsrenderedr   r   r   renderZ  s   

zStyle.renderc                 C   s(   |pt | }tj| | d dS )zWrite text with style directly to terminal.

        This method is for testing purposes only.

        Args:
            text (Optional[str], optional): Text to style or None for style name.

        
N)rj   sysstdoutwriter   )r   r   r   r   r   testt  s   	z
Style.testc                 C   s   t |ts|d u stS |d u s|jr| S | jr|S | t}d |_d |_|jp)| j|_|jp0| j|_| j	|j
 @ |j	|j
@ B |_	| j
|j
B |_
|jpL| j|_|jpS| j|_|j|_| jp^|j|_|S rZ   )r[   r   r   r-   rp   r0   r1   r*   r+   r!   r    r.   r/   r,   )r   rq   	new_styler   r   r   __add__  s(   

zStyle.__add__)r   r   )NNrZ   )r   r   r   r   )ri   )?r#   r$   r%   r&   r	   r   __annotations__r(   r)   r'   r   r   rj   r   classmethodro   rr   r   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   propertyru   r   r   r   r   r   r   	StyleTyper   r   r   r   r   rJ   rK   rY   r   r   r   r\   r   r   r   r   r   r   r   	TRUECOLORr   r   r   r   r   r   r   r      s&  
 	

P.*X$

c                   @   s`   e Zd ZdZdgZdddZdefd	d
Zede	fddZ
de	ddfddZde	fddZdS )
StyleStackzA stack of styles._stackdefault_styler   r   Nc                 C   s   |g| _ d S rZ   r   )r   r   r   r   r   r     s   zStyleStack.__init__c                 C   s   d| j dS )Nz<stylestack >r   rt   r   r   r   r     r   zStyleStack.__repr__c                 C   s
   | j d S )z&Get the Style at the top of the stack.r   rt   r   r   r   current  s   
zStyleStack.currentrq   c                 C   s   | j | j d |  dS )z}Push a new style on to the stack.

        Args:
            style (Style): New style to combine with current style.
        r   N)r   r}   r   r   r   r   push  s   zStyleStack.pushc                 C   s   | j   | j d S )z}Pop last style and discard.

        Returns:
            Style: New current style (also available as stack.current)
        r   )r   poprt   r   r   r   r     s   

zStyleStack.pop)r   r   r   N)r#   r$   r%   r&   r'   r   rj   r   r   r   r   r   r   r   r   r   r   r     s    
r   )r   	functoolsr   randomr   r   typingr   r   r   r   r	   r
   r   ri   r   rJ   r   r   r   r   terminal_themer   r   rj   r   r   r   rm   r   r   r   r   r   <module>   s"    $    ~