U
    祡cf                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	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 dd
lmZmZ eedf ZG dd dZ eG dd dZ!e! Z"G dd dZ#dS )    N)	lru_cache)dumpsloads)randint)AnyDictIterableListOptionalTypeUnioncast   )errors)ColorColorParseErrorColorSystem	blend_rgb)Result	rich_repr)DEFAULT_TERMINAL_THEMETerminalThemeStylec                   @   s@   e Zd ZdZdgZeddddZded ee	 dd	d
Z
dS )_Bitz.A descriptor to get/set a style attribute bit.bitN)bit_noreturnc                 C   s   d|> | _ d S )Nr   )r   )selfr    r   ./tmp/pip-unpacked-wheel-qkhzhrww/rich/style.py__init__   s    z_Bit.__init__r   )objobjtyper   c                 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 ed< eed< ee	 ed< ddddd	d
dddddg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$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  eeeef  ee ee ee ee ee ee ee ee ee ee ee ee ee ee eeeef  d*d+d,Zed d-d.d/Zedee ee d d0d1d2Zeeeeef  d d3d4d5Zedeeeef  ed d6d7d8Zed9Zed:Zed;Zed<Zed=Zed>Zed?Zed@ZedAZ edBZ!edCZ"edDZ#edEZ$e%ed-dFdGZ&ed-dHdIZ'ed-dJdKZ(e)edLdMdNZ*ee+dOdPeedQdRdSZ,eee- e-dTdUdVZ.e/d-dWdXZ0eedYdZd[Z1eedYd\d]Z2ed-d^d_Z3e%ee d-d`daZ4e%ee d-dbdcZ5e%ee d-dddeZ6e%ed-dfdgZ7e%d d-dhdiZ8e%eeef d-djdkZ9e%d d-dldmZ:ee+dndPed dodpdqZ;e+dOdPdee< edrdsdtZ=ee>d  d dudvdwZ?ed d dudxdyZ@d d-dzd{ZAdee d d|d}d~ZBde)jCddeee) eedddZDdee d)dddZEe+dOdPed  d dQddZFed  d dQddZGd)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_meta_link_link_id_ansi_style_definition123456789Z21Z51Z52Z53)r   r                        	   
         dimbolditalic	underlineblinkblink2reverseconcealstrike
underline2frameencircleoverline)rJ   drK   brL   irM   urN   rO   rP   rrQ   crR   srS   uurT   rU   rV   oN)colorbgcolorrK   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   linkmetac                C   s  d | _ d | _tttf tddd}|d kr0d n||| _|d krFd n||| _t|d k	|d k	obd|d k	old|d k	ovd|d k	od|d k	od|	d k	od	|
d k	od
|d k	od|d k	od|d k	od|d k	od|d k	odf| _| jrt|rdpd|rdpd|odpd|rdpd|rdpd|r*dp,d|	r8d	p:d|
rFd
pHd|rTdpVd|rbdpdd|rpdprd|r~dpd|rdpdfnd| _	|| _
|d krd nt|| _|s|rtdd t| j nd| _d | _| jp|p|p|p| | _d S )N)r`   r   c                 S   s   t | tr| S t| S N)
isinstancer   parse)r`   r   r   r   _make_color   s    z#Style.__init__.<locals>._make_colorr?   rA   rE          @                     r   r   ?B  )r4   r5   r   r   strr-   r.   sumr#   r$   r2   r   r1   r   hashr3   r/   r0   )r   r`   ra   rK   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rb   rc   rg   r   r   r   r    z   s\    











#

&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.null)r`   ra   r   c                 C   sV   |  t}d|_d|_||_||_d|_d|_d|_d|_	d|_
|pF| |_d|_|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   rr   )__new__r   r4   r5   r-   r.   r#   r$   r2   r3   r1   r0   r/   )rx   r`   ra   styler   r   r   
from_color   s    

zStyle.from_color)rc   r   c                 C   sj   |  t}d|_d|_d|_d|_d|_d|_d|_t	||_
tdd t|j
 |_d|_| |_|S )zCreate a new style with meta data.

        Returns:
            meta (Optional[Dict[str, Any]]): A dictionary of meta data. Defaults to None.
        Nr   rq   )rz   r   r4   r5   r-   r.   r#   r$   r2   r   r1   r   ru   r3   r/   r0   )rx   rc   r{   r   r   r   	from_meta   s    

zStyle.from_meta)rc   handlersr   c                 K   s2   |dkri n|}| dd | D  | |S )a  Create a blank style with meta information.

        Example:
            style = Style.on(click=self.on_click)

        Args:
            meta (Optional[Dict[str, Any]], optional): An optional dict of meta information.
            **handlers (Any): Keyword arguments are translated in to handlers.

        Returns:
            Style: A Style with meta information attached.
        Nc                 S   s   i | ]\}}d | |qS )@r   ).0keyvaluer   r   r   
<dictcomp>  s      zStyle.on.<locals>.<dictcomp>)updateitemsr}   )rx   rc   r~   r   r   r   on   s    zStyle.onr   r   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   c                 C   s   | j S )z+Get a link id, used in ansi code for links.)r3   r   r   r   r   link_id  s    zStyle.link_idc                 C   s  | j dkrg }|j}| j}|d@ r|d@ r>|| jr8dnd |d@ rX|| jrRdnd |d	@ rr|| jrld
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 |d@ r|| jrdnd |d@ r|d @ rD|| jr>d!nd" |d#@ rb|| jr\d$nd% |d&@ r|| jrzd'nd( |d)@ r|| jrd*nd+ | jdk	r|| jj | jdk	r|d, || jj | jr|d- || j d.|pd/| _ | j S )0z-Re-generate style definition from attributes.N   r   rK   znot boldr?   rJ   znot dimrA   rL   z
not italicrE   rM   znot underline  rh   rN   z	not blinkri   rO   z
not blink2rj   rP   znot reverserk   rQ   znot concealrl   rR   z
not strike   rm   rS   znot underline2rn   rT   z	not framero   rU   znot encirclerp   rV   znot overliner   rb    none)r5   appendr#   rK   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r-   namer.   r2   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.)r0   r   r   r   r   __bool__K  s    zStyle.__bool__)color_systemr   c                 C   s4  | j dkr.g }|j}| j}| j| j@ }|r|d@ r@||d  |d@ rT||d  |d@ rh||d  |d@ r|||d  |d@ rtdd	D ]}|d|> @ r|||  q|d
@ rtd	dD ]}|d|> @ r|||  q| jdk	r|| j|	  | j
dk	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@   r   rF   r      FZ
foreground;)r4   r   
_style_mapr$   r#   ranger-   extendZ	downgradeZget_ansi_codesr.   r   )r   r   Zsgrr   r   r   r   r   r   r   _make_ansi_codesO  s@    

zStyle._make_ansi_codesrn   )maxsizer{   r   c                 C   s8   zt | |W S  tjk
r2   |   Y S X dS )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.
        N)rs   rf   r   StyleSyntaxErrorstriplower)rx   r{   r   r   r   	normalizez  s    zStyle.normalize)valuesr   c                 G   s&   |D ]}|dk	r|  S qt ddS )zPick first non-None style.Nz$expected at least one non-None style)
ValueError)rx   r   r   r   r   r   
pick_first  s    
zStyle.pick_firstc                 c   s   d| j d fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d	| jd fV  d
| j	d fV  d| j
d fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  | jrd| jfV  d S )Nr`   ra   rK   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rb   rc   )r`   ra   rK   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rb   r1   rc   r   r   r   r   __rich_repr__  s"    zStyle.__rich_repr__)otherr   c                 C   s   t |tstS |  | kS rd   re   r   NotImplemented__hash__r   r   r   r   r   __eq__  s    
zStyle.__eq__c                 C   s   t |tstS |  | kS rd   r   r   r   r   r   __ne__  s    
zStyle.__ne__c                 C   s8   | j d k	r| j S t| j| j| j| j| j| jf| _ | j S rd   )r/   ru   r-   r.   r$   r#   r2   r1   r   r   r   r   r     s    

zStyle.__hash__c                 C   s   | j S )z.The foreground color or None if it is not set.)r-   r   r   r   r   r`     s    zStyle.colorc                 C   s   | j S )z.The background color or None if it is not set.)r.   r   r   r   r   ra     s    zStyle.bgcolorc                 C   s   | j S )zLink text, if set.)r2   r   r   r   r   rb     s    z
Style.linkc                 C   s   | j dkp| j jS )z6Check if the style specified a transparent background.N)ra   Z
is_defaultr   r   r   r   transparent_background  s    zStyle.transparent_backgroundc                 C   s   t | jdS )zA Style with background only.)ra   )r   ra   r   r   r   r   background_style  s    zStyle.background_stylec                 C   s&   | j dkri S ttttf t| j S )z=Get meta information (can not be changed after construction).N)r1   r   r   rs   r   r   r   r   r   r   rc     s    z
Style.metac                 C   sr   | j r
tS | t}d|_d|_d|_d|_| j|_| j	|_	| j
|_
| j
rVtdd nd|_d|_ d|_d|_|S )z+Get a copy of the style with color removed.Nr   rq   rr   F)r0   rw   rz   r   r4   r5   r-   r.   r$   r#   r2   r   r3   r1   r/   r   r{   r   r   r   without_color  s    
zStyle.without_colorrp   )style_definitionr   c                 C   s  |  dks|s|  S | j}d}d}i }d}t| }|D ]P}| }	|	dkrt|d}	|	sltdzt	
|	dk W n: tk
r }
 ztd|	d|
 dW 5 d}
~
X Y nX |	}q>|	dkrt|d}	||	}|dkrtd	|	d
||< q>|	dkr(t|d}	|	s"td|	}q>|	|kr@d|||	 < q>zt	
|	 W n< tk
r }
 ztd|	d|
 dW 5 d}
~
X Y nX |	}q>tf |||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   Nr   rr   zcolor expected after 'on'zunable to parse z as background color; notz,expected style attribute after 'not', found Frb   zURL expected after 'link'Tz as color; )r`   ra   rb   )r   ry   STYLE_ATTRIBUTESitersplitr   nextr   r   r   rf   r   getr   )rx   r   r   r`   ra   r   rb   wordsZoriginal_wordworderror	attributer{   r   r   r   rf     sb    









zStyle.parse)themer   c                 C   s
  |pt }g }|j}| j}| j}| jr.|| }}| jr`|dkrB|jn||}t	t
||jd}|dk	r||}|d|j  |d|j  |dk	r|j|dd}|d|j  | jr|d | jr|d	 | jr|d
 | jr|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   r`   ra   rP   rJ   foreground_colorZget_truecolorr   Zfrom_tripletr   Zbackground_colorhexrK   rL   rM   rR   rV   r   )r   r   cssr   r`   ra   r   Ztheme_colorr   r   r   get_html_style3  s>    

zStyle.get_html_style)stylesr   c                 C   s   t |}t|t|S )zCombine styles and get result.

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

        Returns:
            Style: A new style instance.
        r   rt   r   rx   r   Ziter_stylesr   r   r   combineX  s    
zStyle.combinec                 G   s   t |}t|t|S )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   chaine  s    
zStyle.chainc                 C   s~   | j r
tS | t}| j|_| j|_| j|_| j|_| j|_| j	|_	| j
|_
| j
r^tdd nd|_| j|_d|_ | j|_|S )zwGet a copy of this style.

        Returns:
            Style: A new Style instance with identical attributes.
        r   rq   rr   F)r0   rw   rz   r   r4   r5   r-   r.   r$   r#   r2   r   r3   r/   r1   r   r   r   r   copyr  s    
z
Style.copy)rb   r   c                 C   sn   |  t}| j|_| j|_| j|_| j|_| j|_| j|_||_|rPt	dd nd|_
d|_d|_| j|_|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.
        r   rq   rr   NF)rz   r   r4   r5   r-   r.   r$   r#   r2   r   r3   r/   r0   r1   )r   rb   r{   r   r   r   update_link  s    	
zStyle.update_linkrr   F)r   legacy_windows)textr   r   r   c                C   sd   |r|dkr|S | j p| |}|r6d| 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;;\)r4   r   r2   r3   )r   r   r   r   attrsrenderedr   r   r   render  s    
zStyle.render)r   r   c                 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)rs   sysstdoutwriter   )r   r   r   r   r   test  s    	z
Style.testc                 C   s   |d ks|j r| S | j r|S | t}d |_d |_|jp<| j|_|jpJ| j|_| j|j @ |j|j@ B |_| j|jB |_|j	p| j	|_	|j
p| j
|_
|j |_ | jr|jrt| j|j|_n| jp|j|_d |_|S rd   )r0   rz   r   r4   r5   r-   r.   r$   r#   r2   r3   r1   r   rc   r/   )r   r{   Z	new_styler   r   r   _add  s*    

z
Style._addc                 C   s   |  |}|jr| S |S rd   )r   rb   r   )r   r{   Zcombined_styler   r   r   __add__  s    
zStyle.__add__)NN)N)N)N)rr   )N)Hr&   r'   r(   r)   r
   r   __annotations__r+   r,   bytesr*   r   r   r   rs   r   r   r    classmethodry   r|   r}   r   r   rK   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   propertyr   r   r   r   r   r   r   	StyleTyper   r   r   r   r   r   r`   ra   rb   r   r   rc   r   rf   r   r   r   r   r   r   r   Z	TRUECOLORr   r   r   r   r   r   r   r   r      sZ  
L    ".+A$ c                   @   sd   e Zd ZdZdgZdddddZedd	d
Zee	dddZ
e	ddddZe	dddZdS )
StyleStackzA stack of styles._stackr   N)default_styler   c                 C   s   |g| _ d S rd   r   )r   r   r   r   r   r      s    zStyleStack.__init__rv   c                 C   s   d| j dS )Nz<stylestack >r   r   r   r   r   __repr__  s    zStyleStack.__repr__c                 C   s
   | j d S )z&Get the Style at the top of the stack.r   r   r   r   r   current  s    zStyleStack.currentr   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   popr   r   r   r   r     s    
zStyleStack.pop)r&   r'   r(   r)   r*   r    rs   r   r   r   r   r   r   r   r   r   r   r     s   r   )$r   	functoolsr   marshalr   r   randomr   typingr   r   r   r	   r
   r   r   r   rr   r   r`   r   r   r   r   reprr   r   Zterminal_themer   r   rs   r   r   r   rw   r   r   r   r   r   <module>   s&   (     I