o
    Zh2                     @  s"  d Z ddlmZ ddlZ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 dd	lmZ g d
Zdd e D Zd+ddZedddddddddd	Zd,ddZd-ddZedZG dd deZejZG dd deZ e	d Z!d.d#d$Z"d/d(d)Z#G d*d' d'eZ$dS )0z-
Tool for creating styles from a dictionary.
    )annotationsN)Enum)HashableTypeVar)SimpleCache   )ANSI_COLOR_NAMESANSI_COLOR_NAMES_ALIASESDEFAULT_ATTRSAttrs	BaseStyle)NAMED_COLORS)Styleparse_colorPrioritymerge_stylesc                 C  s    i | ]\}}|  |d qS )#)lowerlstrip).0kv r   R/var/www/html/lang_env/lib/python3.10/site-packages/prompt_toolkit/styles/style.py
<dictcomp>   s     r   textstrreturnc                 C  s   | t v r| S | tv rt|  S zt|   W S  ty   Y nw | dd dkr\| dd }|t v r3|S |tv r;t| S t|dkrC|S t|dkr[|d d |d d  |d d  S n| dv rb| S td	|  )
z
    Parse/validate color format.

    Like in Pygments, but also support the ANSI color names.
    (These will map to the colors of the 16 color palette.)
    r   r   r   N         ) defaultzWrong color format %r)r   r	   _named_colors_lowercaser   KeyErrorlen
ValueError)r   colr   r   r   r       s.   $r   	colorbgcolorbold	underlinestrikeitalicblinkreversehidden	classname	list[str]c                 C  sF   g }|  d}tdt|d D ]}|d|d|   q|S )z
    Split a single class name at the `.` operator, and build a list of classes.

    E.g. 'a.b.c' becomes ['a', 'a.b', 'a.b.c']
    .r   N)splitranger%   appendjoinr   )r2   resultpartsir   r   r   _expand_classname]   s
   
r<   	style_strr   c                 C  s  d| v rt }nt}|  D ]}|dkrq|dkr|jdd}q|dkr*|jdd}q|dkr5|jdd}q|d	kr@|jdd}q|d
krK|jdd}q|dkrV|jdd}q|dkra|jdd}q|dkrl|jdd}q|dkrw|jdd}q|dkr|jdd}q|dkr|jdd}q|dkr|jdd}q|dkr|jdd}q|dkr|jdd}q|dv rq|drq|dr|drq|dr|jt|dd d }q|d!r|jt|dd d"}q|jt|d"}q|S )#zd
    Take a style string, e.g.  'bg:red #88ff00 class:title'
    and return a `Attrs` instance.
    	noinheritr+   Tr+   noboldFr.   r.   noitalicr,   r,   nounderliner-   r-   Znostriker/   r/   Znoblinkr0   r0   Z	noreverser1   r1   Znohidden)romansansmonozborder:[]zbg:r   Nr*   zfg:r)   )r
   _EMPTY_ATTRSr5   _replace
startswithendswithr   )r=   attrspartr   r   r   _parse_style_strl   s\   


rV   z^[a-z0-9.\s_-]*$c                   @  s   e Zd ZdZdZdZdS )r   a  
    The priority of the rules, when a style is created from a dictionary.

    In a `Style`, rules that are defined later will always override previous
    defined rules, however in a dictionary, the key order was arbitrary before
    Python 3.6. This means that the style could change at random between rules.

    We have two options:

    - `DICT_KEY_ORDER`: This means, iterate through the dictionary, and take
       the key/value pairs in order as they come. This is a good option if you
       have Python >3.6. Rules at the end will override rules at the beginning.
    - `MOST_PRECISE`: keys that are defined with most precision will get higher
      priority. (More precise means: more elements.)
    Z	KEY_ORDERMOST_PRECISEN)__name__
__module____qualname____doc__DICT_KEY_ORDERrW   r   r   r   r   r      s    r   c                   @  sR   e Zd ZdZdddZeddd	ZeefdddZ	e
fdddZdddZdS )r   a  
    Create a ``Style`` instance from a list of style rules.

    The `style_rules` is supposed to be a list of ('classnames', 'style') tuples.
    The classnames are a whitespace separated string of class names and the
    style string is just like a Pygments style definition, but with a few
    additions: it supports 'reverse' and 'blink'.

    Later rules always override previous rules.

    Usage::

        Style([
            ('title', '#ff0000 bold underline'),
            ('something-else', 'reverse'),
            ('class1 class2', 'reverse'),
        ])

    The ``from_dict`` classmethod is similar, but takes a dictionary as input.
    style_ruleslist[tuple[str, str]]r   Nonec                 C  s^   g }|D ]"\}}t |sJ t|t|  }t|}|||f q|| _|| _	d S N)
CLASS_NAMES_REmatchrepr	frozensetr   r5   rV   r7   _style_rulesclass_names_and_attrs)selfr]   rf   class_namesr=   Zclass_names_setrT   r   r   r   __init__   s   
zStyle.__init__c                 C  s   | j S r`   )re   rg   r   r   r   r]      s   zStyle.style_rules
style_dictdict[str, str]priorityr   c                 C  s8   |t jkrd	dd}| t| |dS | t| S )
za
        :param style_dict: Style dictionary.
        :param priority: `Priority` value.
        itemtuple[str, str]r   intc                 S  s   t dd | d  D S )Nc                 s  s    | ]
}t |d V  qdS )r4   N)r%   r5   )r   r;   r   r   r   	<genexpr>  s    z/Style.from_dict.<locals>.key.<locals>.<genexpr>r   )sumr5   )rn   r   r   r   key  s   zStyle.from_dict.<locals>.key)rs   N)rn   ro   r   rp   )r   rW   sorteditemslist)clsrk   rm   rs   r   r   r   	from_dict   s   

zStyle.from_dictr=   r   r"   r   c              	   C  s  |g}t  }| jD ]\}}|s|| q	| D ]m}|dr}g }|dd  dD ]	}	|t|	 q-|D ]B}
t  }|t	|
g t
dt|d D ]}t||D ]}|t	||
f  qWqO| jD ]\}}||v ru|| qh||
 q9qt|}|| qt|S )z9
        Get `Attrs` for the given style string.
        zclass:r   N,r   )setrf   r7   r5   rR   r   extendr<   addrd   r6   r%   	itertoolscombinationsrV   _merge_attrs)rg   r=   r"   list_of_attrsrh   namesattrrU   Znew_class_namespnew_nameZcomboscountc2Zinline_attrsr   r   r   get_attrs_for_style_str  s6   


zStyle.get_attrs_for_style_strr   c                 C  s
   t | jS r`   )idrf   rj   r   r   r   invalidation_hash<     
zStyle.invalidation_hashN)r]   r^   r   r_   r   r^   )rk   rl   rm   r   r   r   r=   r   r"   r   r   r   r   r   )rX   rY   rZ   r[   ri   propertyr]   classmethoddefault_priorityrx   r
   r   r   r   r   r   r   r      s    
1r   _Tr   list[Attrs]c                 C  s   ddd}t |dgdd | D R  |dgd	d | D R  |d
gdd | D R  |d
gdd | D R  |d
gdd | D R  |d
gdd | D R  |d
gdd | D R  |d
gdd | D R  |d
gdd | D R  d	S )z
    Take a list of :class:`.Attrs` instances and merge them into one.
    Every `Attr` in the list can override the styling of the previous one. So,
    the last one has highest priority.
    valuesr   r   c                  W  s(   | ddd D ]
}|dur|  S qt )z/Take first not-None value, starting at the end.N)r&   )r   r   r   r   r   _orJ  s
   z_merge_attrs.<locals>._orr!   c                 S     g | ]}|j qS r   rO   r   ar   r   r   
<listcomp>R      z _merge_attrs.<locals>.<listcomp>c                 S  r   r   rN   r   r   r   r   r   S  r   Fc                 S  r   r   r?   r   r   r   r   r   T  r   c                 S  r   r   rC   r   r   r   r   r   U  r   c                 S  r   r   rE   r   r   r   r   r   V  r   c                 S  r   r   rA   r   r   r   r   r   W  r   c                 S  r   r   rF   r   r   r   r   r   X  r   c                 S  r   r   rG   r   r   r   r   r   Y  r   c                 S  r   r   rH   r   r   r   r   r   Z  r   r(   N)r   r   r   r   )r   )r   r   r   r   r   r   C  s   
r   styleslist[BaseStyle]_MergedStylec                 C  s   dd | D } t | S )z)
    Merge multiple `Style` objects.
    c                 S  s   g | ]}|d ur|qS r`   r   r   sr   r   r   r   b  s    z merge_styles.<locals>.<listcomp>)r   )r   r   r   r   r   ^  s   r   c                   @  sN   e Zd ZdZdddZedd	d
ZedddZefdddZ	dddZ
dS )r   z
    Merge multiple `Style` objects into one.
    This is supposed to ensure consistency: if any of the given styles changes,
    then this style will be updated.
    r   r   r   r_   c                 C  s   || _ tdd| _d S )Nr   )maxsize)r   r   _style)rg   r   r   r   r   ri   v  s   z_MergedStyle.__init__r   c                   s    d fdd} j   |S )z=The `Style` object that has the other styles merged together.r   r   c                     s
   t  jS r`   )r   r]   r   rj   r   r   get~  r   z'_MergedStyle._merged_style.<locals>.getNr   r   )r   r   r   )rg   r   r   rj   r   _merged_stylez  s   z_MergedStyle._merged_styler^   c                 C  s    g }| j D ]}||j q|S r`   )r   r{   r]   )rg   r]   r   r   r   r   r]     s   
z_MergedStyle.style_rulesr=   r   r"   r   c                 C  s   | j ||S r`   )r   r   )rg   r=   r"   r   r   r   r     s   z$_MergedStyle.get_attrs_for_style_strr   c                 C  s   t dd | jD S )Nc                 s  s    | ]}|  V  qd S r`   )r   r   r   r   r   rq     s    z1_MergedStyle.invalidation_hash.<locals>.<genexpr>)tupler   rj   r   r   r   r     s   z_MergedStyle.invalidation_hashN)r   r   r   r_   r   r   r   r   )rX   rY   rZ   r[   ri   r   r   r]   r
   r   r   r   r   r   r   r   f  s    
)r   r   r   r   )r2   r   r   r3   )r=   r   r   r   )r   r   r   r   )r   r   r   r   )%r[   
__future__r   r}   reenumr   typingr   r   Zprompt_toolkit.cacher   baser   r	   r
   r   r   Znamed_colorsr   __all__ru   r#   r   rP   r<   rV   compilera   r   r\   r   r   r   r   r   r   r   r   r   r   <module>   sB    
0


Ds

