
    AHj5                       U 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m	Z	m
Z
 ddlZddlmZ erddlmZ ddlmZ d	Zd
ed<   	 eZd
ed<   	 eZd
ed<   	 e	eef   Z	  G d de      Z G d de      Z G d de      Z ed       G d d             Z G d de
      Zej>                  j@                   G d d             Z! G d de
      Z"y)z

This module contains the `Binding` class and related objects.

See [bindings](/guide/input#bindings) in the guide for details.
    )annotationsN)	dataclass)TYPE_CHECKINGIterableIteratorMapping
NamedTuple)_character_to_key)	TypeAlias)DOMNodez0Binding | tuple[str, str] | tuple[str, str, str]r   BindingTypeBindingIDString	KeyStringc                      e Zd ZdZy)BindingErrorzA binding related error.N__name__
__module____qualname____doc__     G/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/binding.pyr   r   *       "r   r   c                      e Zd ZdZy)	NoBindingzA binding was not found.Nr   r   r   r   r   r   .   r   r   r   c                      e Zd ZdZy)InvalidBindingz$Binding key is in an invalid format.Nr   r   r   r   r   r   2   s    .r   r   Tfrozenc                      e Zd ZU dZded<   	 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<   	  ed       G d d             Zd
Zded<   	 ddZdddZedd       Zy
)Bindingz#The configuration of a key binding.strkeyaction descriptionTboolshowN
str | Nonekey_displayFprioritytooltipidsystemr   c                  0    e Zd ZU dZdZded<   	 dZded<   y)	Binding.GroupzIA binding group causes the keys to be grouped under a single description.r&   r#   r'   Fr(   compactN)r   r   r   r   r'   __annotations__r2   r   r   r   Groupr1   W   s    WS'4r   r4   zGroup | Nonegroupc                F    | j                   j                  d      ^ }}||fS )zParse a key into a list of modifiers, and the actual key.

        Returns:
            A tuple of (MODIFIER LIST, KEY).
        +)r$   split)self	modifiersr$   s      r   	parse_keyzBinding.parse_keyd   s$     ((..-C#~r   c                2    t        j                  | ||      S )a  Return a new binding with the key and key_display set to the specified values.

        Args:
            key: The new key to set.
            key_display: The new key display to set.

        Returns:
            A new binding with the key set to the specified value.
        r$   r+   )dataclassesreplace)r9   r$   r+   s      r   with_keyzBinding.with_keym   s     ""4SkJJr   c              #  p  K   t        |      }|D ]!  }t        |t              r#t        |      dvrt	        d|      t        | }|j                  j                  d      D ]  }|j                         }|st        d|j                        t        |      dk(  rt        |      }t        ||j                  |j                  t        |j                  xr |j                        |j                  |j                   |j"                  |j$                  |j&                  |j(                  
        $ yw)aB  Convert a list of BindingType (the types that can be specified in BINDINGS)
        into an Iterable[Binding].

        Compound bindings like "j,down" will be expanded into 2 Binding instances.

        Args:
            bindings: An iterable of BindingType.

        Returns:
            An iterable of Binding.
        )      z;BINDINGS must contain a tuple of two or three strings, not ,zCan not bind empty string in    )
r$   r%   r'   r)   r+   r,   r-   r.   r/   r5   N)list
isinstancetuplelenr   r"   r$   r8   stripr   r
   r%   r'   r(   r)   r+   r,   r-   r.   r/   r5   )clsbindingsbindingr$   s       r   make_bindingszBinding.make_bindingsy   s      > !	G'5)w<v-&UV]U`a  "7+
 {{((- iik(7G  s8q=+C0C">> ' 3 3g11BgllC ' 3 3$--#OOzz">>!-- !	s   D4D6)returnztuple[list[str], str]N)r$   r#   r+   r*   rO   r"   )rL   zIterable[BindingType]rO   zIterable[Binding])r   r   r   r   r3   r'   r)   r+   r,   r-   r.   r/   r   r4   r5   r;   r@   classmethodrN   r   r   r   r"   r"   6   s    -	HoKK D$6"K" Hd/GS-B

 FDRd5 5 5 E<P
K . .r   r"   c                  D    e Zd ZU dZded<   	 ded<   	 ded<   	 dZd	ed
<   y)ActiveBindingzmInformation about an active binding (returned from [active_bindings][textual.screen.Screen.active_bindings]).r   noder"   rM   r(   enabledr&   r#   r-   N)r   r   r   r   r3   r-   r   r   r   rS   rS      s+    w
M0"MOGS+r   rS   c                      e Zd ZdZ	 d	 	 	 ddZddZddZedd       ZddZ	ddZ
edd	       Zdd
Zedd       Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZy)BindingsMapzManage a set of bindings.Nc                    i | _         	 t        j                  |xs i       D ]7  }| j                   j                  |j                  g       j                  |       9 y)a9  Initialise a collection of bindings.

        Args:
            bindings: An optional set of initial bindings.

        Note:
            The iterable of bindings can contain either a `Binding`
            instance, or a tuple of 3 values mapping to the first three
            properties of a `Binding`.
        N)key_to_bindingsr"   rN   
setdefaultr$   append)r9   rL   rM   s      r   __init__zBindingsMap.__init__   sT     :<N,,X^< 	MG  ++GKK<CCGL	Mr   c                n    | j                   j                  |j                  g       j                  |       y)zSAdd a new binding.

        Args:
            binding: New Binding to add.
        N)rY   rZ   r$   r[   )r9   rM   s     r   _add_bindingzBindingsMap._add_binding   s)     	''R8??Hr   c           
         t        | j                  j                         D cg c]  \  }}|D ]  }||f  c}}}      S c c}}}w )z7Iterating produces a sequence of (KEY, BINDING) tuples.)iterrY   items)r9   r$   rL   rM   s       r   __iter__zBindingsMap.__iter__   s]     &*%9%9%?%?%A !C'  g
 	
s   Ac                "     |        }||_         |S )zConstruct a BindingsMap from a dict of keys and bindings.

        Args:
            keys: A dict that maps a key on to a list of `Binding` objects.

        Returns:
            New `BindingsMap`
        rY   )rK   keysrL   s      r   	from_keyszBindingsMap.from_keys   s     5#' r   c                X    t               }| j                  j                         |_        |S )zZReturn a copy of this instance.

        Return:
            New bindings object.
        )rW   rY   copy)r9   rh   s     r   rh   zBindingsMap.copy   s'     }#3388:r   c              #  (   K   | j                    y wrP   rd   )r9   s    r   __rich_repr__zBindingsMap.__rich_repr__   s     """s   c                    i }|D ]E  }|j                   j                         D ]&  \  }}|j                  |g       j                  |       ( G t        j                  |      S )zMerge a bindings.

        Args:
            bindings: A number of bindings.

        Returns:
            New `BindingsMap`.
        )rY   ra   rZ   extendrW   rf   )rK   rL   re   	_bindingsr$   key_bindingss         r   mergezBindingsMap.merge   sg     *,! 	>I%.%>%>%D%D%F >!\R(//=>	> $$T**r   c           	        t               }i }t        | j                  j                               }|D ]y  \  }}|D ]m  }|j                  |j                        x}s'|j                  d      }	|D ]F  \  }}
|j                         }t        fd|
D              s+|| j                  v s:| j                  |= H |	D ]  }|| j                  v s||v s| j                  j                  |g       |j                  |g       z   }|D ]H  }|j                  r)|j                  |j                        |j                  k7  r8|j                  |       J || j                  v s| j                  |=  |	D ]4  }|j                  |g       j                  |j                  |d             6 p | | j                  j                  |       t        |      S )a4  Replace bindings for keys that are present in `keymap`.

        Preserves existing bindings for keys that are not in `keymap`.

        Args:
            keymap: A keymap to overlay.

        Returns:
            KeymapApplyResult: The result of applying the keymap, including any clashed bindings.
        NrD   c              3  <   K   | ]  }|j                   k(    y wrP   )r.   ).0rM   
binding_ids     r   	<genexpr>z+BindingsMap.apply_keymap.<locals>.<genexpr>+  s     TGwzzZ7Ts   r=   )setrF   rY   ra   r.   getr8   rJ   anyr$   addrZ   r[   r@   updateKeymapApplyResult)r9   keymapclashed_bindingsnew_bindingsrY   r$   rL   rM   keymap_key_stringkeymap_keysrn   
keymap_keyclashing_bindingsclashed_bindingrs   s                 @r   apply_keymapzBindingsMap.apply_keymap  s    *-13t3399;<, ,	MC# +$ZZ
% )/

:(>>$>"3"9"9#">K .= >)\!iikT|TT"d&:&::$($8$8$=	> '2 E
&$*>*>>)\9
 150D0D0H0H *B1 , 0 0R @1A- 4E J %4$6$6(.

?3E3E(F'6':':); %5$8$8$IJ  *T-A-AA$($8$8$D+E. '2 
$//
B?FF#,,,NQ+,	^ 	##L1 !122r   c                    | j                   j                         D cg c]  }|D ]  }|j                  r|  }}}|S c c}}w )z"A list of bindings for shown keys.)rY   valuesr)   )r9   rL   rM   re   s       r   
shown_keyszBindingsMap.shown_keysO  sX    
 !00779
#
 || 

 
 
s   ?c                   |j                  d      D cg c]  }|j                          }}|D ]I  }| j                  j                  |g       j	                  t        |||t        |xr |      ||             K yc c}w )a  Bind keys to an action.

        Args:
            keys: The keys to bind. Can be a comma-separated list of keys.
            action: The action to bind the keys to.
            description: An optional description for the binding.
            show: A flag to say if the binding should appear in the footer.
            key_display: Optional string to display in the footer for the key.
            priority: Is this a priority binding, checked form app down to focused widget?
        rD   )r)   r+   r,   N)r8   rJ   rY   rZ   r[   r"   r(   )	r9   re   r%   r'   r)   r+   r,   r$   all_keyss	            r   bindzBindingsMap.bindZ  s~    & ,0::c?;CCIIK;; 
	C  ++C4;;k2d3 +%	
	 <s   A<c                Z    	 | j                   |   S # t        $ r t        d|       dw xY w)zGet a list of bindings for a given key.

        Args:
            key: Key to look up.

        Raises:
            NoBinding: If the binding does not exist.

        Returns:
            A list of bindings associated with the key.
        zNo binding for N)rY   KeyErrorr   )r9   r$   s     r   get_bindings_for_keyz BindingsMap.get_bindings_for_keyz  s;    	?'',, 	?ocU34$>	?s    *rP   )rL   zIterable[BindingType] | NonerO   None)rM   r"   rO   r   )rO   zIterator[tuple[str, Binding]])re   zdict[str, list[Binding]]rO   rW   )rO   rW   )rO   zrich.repr.Result)rL   zIterable[BindingsMap]rO   rW   )r{   KeymaprO   rz   )rO   list[Binding])r&   TNF)re   r#   r%   r#   r'   r#   r)   r(   r+   r*   r,   r(   rO   r   )r$   r#   rO   r   )r   r   r   r   r\   r^   rb   rQ   rf   rh   rj   ro   r   propertyr   r   r   r   r   r   rW   rW      s    # 26M.M 
M*I
  # + +?3B   "&  	
     
@?r   rW   c                      e Zd ZU dZded<   y)rz   z The result of applying a keymap.zset[Binding]r|   N)r   r   r   r   r3   r   r   r   rz   rz     s    *""Jr   rz   )#r   
__future__r   r>   r   typingr   r   r   r   r	   	rich.reprrichtextual.keysr
   typing_extensionsr   textual.domr   r   r3   r#   r   r   r   	Exceptionr   r   r   r"   rS   reprautorW   rz   r   r   r   <module>r      s    #  ! I I  *+#KY K M   
 	9 
 
)+	, X#9 ##	 #/Y / $q q qh
,J 
, P? P? P?fK
 Kr   