o
    ˷e2                     @   sb  d Z ddlZddlZddlZddlmZ ddl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mZ ddlmZ g d	Zd
d e D ZdedefddZedddddddddd	Zdede	e fddZdedef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e	e defddZ&d e	e dd!fd"d#Z'G d$d! d!eZ(dS )%z-
Tool for creating styles from a dictionary.
    N)Enum)DictHashableListSetTuple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/ideatree/venv/lib/python3.10/site-packages/prompt_toolkit/styles/style.py
<dictcomp>   s     r   text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	classnamec                 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   )r4   resultpartsir   r   r   _expand_classname\   s
   
r=   	style_strc                 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-   noboldFr0   r0   noitalicr.   r.   nounderliner/   r/   nostriker1   r1   noblinkr2   r2   	noreverser3   r3   nohidden)romansansmonozborder:[]zbg:r!   Nr,   zfg:r+   )r   _EMPTY_ATTRSr6   _replace
startswithendswithr   )r>   attrspartr   r   r   _parse_style_strk   s\   


r[   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.)
    	KEY_ORDERMOST_PRECISEN)__name__
__module____qualname____doc__DICT_KEY_ORDERr]   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdeeeef  ddfddZedeeeef  fddZ	e
efd	eeef d
edd fddZefdededefddZdef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_rulesr   Nc                 C   s^   g }|D ]"\}}t |sJ t|t|  }t|}|||f q|| _|| _	d S N)
CLASS_NAMES_REmatchrepr	frozensetr   r6   r[   r8   _style_rulesclass_names_and_attrs)selfrc   rj   class_namesr>   class_names_setrY   r   r   r   __init__   s   
zStyle.__init__c                 C   s   | j S rd   )ri   rk   r   r   r   rc      s   zStyle.style_rules
style_dictpriorityc                 C   sH   |t jkrdtttf dtfdd}| t| |dS | t| S )za
        :param style_dict: Style dictionary.
        :param priority: `Priority` value.
        itemr   c                 S   s   t dd | d  D S )Nc                 s   s    | ]
}t |d V  qdS )r5   N)r'   r6   )r   r<   r   r   r   	<genexpr>  s    z/Style.from_dict.<locals>.key.<locals>.<genexpr>r   )sumr6   )rr   r   r   r   key  s   zStyle.from_dict.<locals>.key)ru   )r   r]   r   strintsorteditemslist)clsrp   rq   ru   r   r   r   	from_dict   s   
zStyle.from_dictr>   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
   )setrj   r8   r6   rW   r   extendr=   addrh   r7   r'   	itertoolscombinationsr[   _merge_attrs)rk   r>   r$   list_of_attrsrl   namesattrrZ   new_class_namespnew_namecomboscountc2inline_attrsr   r   r   get_attrs_for_style_str
  s6   


zStyle.get_attrs_for_style_strc                 C   s
   t | jS rd   )idrj   ro   r   r   r   invalidation_hash;     
zStyle.invalidation_hash)r^   r_   r`   ra   r   r   rv   rn   propertyrc   classmethoddefault_priorityr   r   r|   r   r   r   r   r   r   r   r   r   r      s.    

1r   _Tr   c                 C   s   dt dt f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   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   _orI  s
   z_merge_attrs.<locals>._orr#   c                 S      g | ]}|j qS r   rT   r   ar   r   r   
<listcomp>Q      z _merge_attrs.<locals>.<listcomp>c                 S   r   r   rS   r   r   r   r   r   R  r   Fc                 S   r   r   r@   r   r   r   r   r   S  r   c                 S   r   r   rD   r   r   r   r   r   T  r   c                 S   r   r   rF   r   r   r   r   r   U  r   c                 S   r   r   rB   r   r   r   r   r   V  r   c                 S   r   r   rH   r   r   r   r   r   W  r   c                 S   r   r   rJ   r   r   r   r   r   X  r   c                 S   r   r   rL   r   r   r   r   r   Y  r   r*   )r   r   )r   r   r   r   r   r   B  s   r   styles_MergedStylec                 C   s   dd | D } t | S )z)
    Merge multiple `Style` objects.
    c                 S   s   g | ]}|d ur|qS rd   r   r   sr   r   r   r   a  s    z merge_styles.<locals>.<listcomp>)r   )r   r   r   r   r   ]  s   r   c                   @   s~   e Zd ZdZdee ddfddZedefddZ	edee
eef  fd	d
ZefdededefddZdef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   Nc                 C   s   || _ tdd| _d S )Nr
   )maxsize)r   r	   _style)rk   r   r   r   r   rn   u  s   z_MergedStyle.__init__c                    s$   dt f fdd} j  |S )z=The `Style` object that has the other styles merged together.r   c                      s
   t  jS rd   )r   rc   r   ro   r   r   get}  r   z'_MergedStyle._merged_style.<locals>.get)r   r   r   r   )rk   r   r   ro   r   _merged_styley  s   z_MergedStyle._merged_stylec                 C   s    g }| j D ]}||j q|S rd   )r   r   rc   )rk   rc   r   r   r   r   rc     s   
z_MergedStyle.style_rulesr>   r$   c                 C   s   | j ||S rd   )r   r   )rk   r>   r$   r   r   r   r     s   z$_MergedStyle.get_attrs_for_style_strc                 C   s   t dd | jD S )Nc                 s   s    | ]}|  V  qd S rd   )r   r   r   r   r   rs     s    z1_MergedStyle.invalidation_hash.<locals>.<genexpr>)tupler   ro   r   r   r   r     s   z_MergedStyle.invalidation_hash)r^   r_   r`   ra   r   r   rn   r   r   r   r   rv   rc   r   r   r   r   r   r   r   r   r   r   e  s     
))ra   r   resysenumr   typingr   r   r   r   r   r   prompt_toolkit.cacher	   baser   r   r   r   r   named_colorsr   __all__ry   r%   rv   r   rU   r=   r[   compilere   r   rb   r   r   r   r   r   r   r   r   r   r   <module>   sB     0
Ds