
    AHj&                       d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlZd dlmZmZmZ  G d d	e      Z G d
 de      Z G d de      Ze G d de             Zej0                  dej2                  dej4                  dej6                  dej8                  dej:                  dej<                  diZejA                         D  ci c]  \  } }|| 
 c}} Z! ejD                  d      jF                  Z$ ed      Z%	 	 	 	 	 	 	 	 	 	 d'dZ&	 	 	 	 	 	 	 	 	 	 d'dZ'	 	 	 	 	 	 	 	 	 	 d'dZ(	 	 	 	 	 	 	 	 	 	 d'dZ)	 	 	 	 	 	 	 	 	 	 d'dZ*	 	 	 	 	 	 	 	 	 	 d'dZ+ej0                  e&ej2                  e'ej6                  e(ej8                  e)ej:                  e*ej<                  e+iZ,d(dZ- G d  d!e      Z.ej^                  ja                  d"#       G d$ d%e             Z1 e1e.je                  d       e.je                  d             Z3d)d&Z4yc c}} w )*    )annotationsN)EnumuniqueFraction)	lru_cache)Iterable
NamedTuple)OffsetSizeclampc                      e Zd ZdZy)ScalarErrorz5Base class for exceptions raised by the Scalar class.N__name__
__module____qualname____doc__     J/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/css/scalar.pyr   r      s    ?r   r   c                      e Zd ZdZy)ScalarResolveErrorzDRaised for errors resolving scalars (unlikely to occur in practice).Nr   r   r   r   r   r      s    Nr   r   c                      e Zd ZdZy)ScalarParseErrorz6Raised when a scalar couldn't be parsed from a string.Nr   r   r   r   r   r      s    @r   r   c                  0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)Unitz4Enumeration of the various units inherited from CSS.                        N)r   r   r   r   CELLSFRACTIONPERCENTWIDTHHEIGHT
VIEW_WIDTHVIEW_HEIGHTAUTOr   r   r   r   r      s-    >EHGEFJKDr   r    fr%whvwvhz ^(-?\d+\.?\d*)(fr|%|w|h|vw|vh)?$r   c                    t        |       S )a  Resolves explicit cell size, i.e. width: 10

    Args:
        value: Scalar value.
        size: Size of widget.
        viewport: Size of viewport.
        fraction_unit: Size of fraction, i.e. size of 1fr as a Fraction.

    Returns:
        Resolved unit.
    r   valuesizeviewportfraction_units       r   _resolve_cellsr;   8   s     E?r   c                    |t        |       z  S )a  Resolves a fraction unit i.e. width: 2fr

    Args:
        value: Scalar value.
        size: Size of widget.
        viewport: Size of viewport.
        fraction_unit: Size of fraction, i.e. size of 1fr as a Fraction.

    Returns:
        Resolved unit.
    r   r6   s       r   _resolve_fractionr=   I   s     8E?**r   c                F    t        |       t        |j                  d      z  S )a  Resolves width unit i.e. width: 50w.

    Args:
        value: Scalar value.
        size: Size of widget.
        viewport: Size of viewport.
        fraction_unit: Size of fraction, i.e. size of 1fr as a Fraction.

    Returns:
        Resolved unit.
    d   r   widthr6   s       r   _resolve_widthrB   Z   s     E?Xdjj#666r   c                F    t        |       t        |j                  d      z  S )a  Resolves height unit, i.e. height: 12h.

    Args:
        value: Scalar value.
        size: Size of widget.
        viewport: Size of viewport.
        fraction_unit: Size of fraction, i.e. size of 1fr as a Fraction.

    Returns:
        Resolved unit.
    r?   r   heightr6   s       r   _resolve_heightrF   k   s     E?Xdkk3777r   c                F    t        |       t        |j                  d      z  S )a  Resolves view width unit, i.e. width: 25vw.

    Args:
        value: Scalar value.
        size: Size of widget.
        viewport: Size of viewport.
        fraction_unit: Size of fraction, i.e. size of 1fr as a Fraction.

    Returns:
        Resolved unit.
    r?   r@   r6   s       r   _resolve_view_widthrH   |   s     E?Xhnnc:::r   c                F    t        |       t        |j                  d      z  S )a
  Resolves view height unit, i.e. height: 25vh.

    Args:
        value: Scalar value.
        size: Size of widget.
        viewport: Size of viewport.
        fraction_unit: Size of fraction, i.e. size of 1fr as a Fraction.

    Returns:
        Resolved unit.
    r?   rD   r6   s       r   _resolve_view_heightrJ      s     E?Xhoos;;;r   c                :    | D cg c]  }t         |    c}S c c}w )z~Get symbols for an iterable of units.

    Args:
        units: A number of units.

    Returns:
        List of symbols.
    )UNIT_SYMBOL)unitsunits     r   get_symbolsrO      s     +00$K000s   c                  Z   e Zd ZU dZded<   ded<   ded<   ddZedd       Zedd	       Zedd
       Z	edd       Z
edd       Zedd       Zedd       Zedd       Ze ed      ej$                  fdd              Z ed      	 d	 	 	 	 	 	 	 dd       Z	 	 	 d	 	 	 	 	 	 	 ddZy) ScalarzA numeric value and a unit.floatr7   r   rN   percent_unitc                    | \  }}}|t         j                  k(  ry|j                         rt        |      n| | j                   S )Nauto)r   r-   
is_integerintsymbolselfr7   rN   _s       r   __str__zScalar.__str__   sA    tQ499 % 0 0 2#e*>t{{mLLr   c                <    | j                   t        j                  k(  S )z&Check if the Scalar is explicit cells.)rN   r   r&   rZ   s    r   is_cellszScalar.is_cells   s     yyDJJ&&r   c                <    | j                   t        j                  k(  S )z)Check if the Scalar is a percentage unit.)rN   r   r(   r^   s    r   
is_percentzScalar.is_percent   s     yyDLL((r   c                <    | j                   t        j                  k(  S )z Check if the unit is a fraction.)rN   r   r'   r^   s    r   is_fractionzScalar.is_fraction   s     yyDMM))r   c                N    | \  }}}|t         j                  k(  rt        |      S dS )z$Check if the unit is explicit cells.N)r   r&   rW   rY   s       r   cellszScalar.cells   s)     tQ!TZZ/s5z9T9r   c                N    | \  }}}|t         j                  k(  rt        |      S dS )z/Get the fraction value, or None if not a value.N)r   r'   rW   rY   s       r   fractionzScalar.fraction   s)     tQ!T]]2s5z<<r   c                (    t         | j                     S )zGet the symbol of this unit.)rL   rN   r^   s    r   rX   zScalar.symbol   s     499%%r   c                <    | j                   t        j                  k(  S )zCheck if this is an auto unit.)rN   r   r-   r^   s    r   is_autozScalar.is_auto   s     yyDII%%r   c                `     | t        |      t        j                  t        j                        S )zCreate a scalar with cells unit.

        Args:
            value: A number of cells.

        Returns:
            New Scalar.
        )rR   r   r&   r)   )clsr7   s     r   from_numberzScalar.from_number   s     5<TZZ88r   i   )maxsizec                   |j                         dk(  r( | dt        j                  t        j                        }|S t        |      }|t	        |d      |j                         \  }} | t        |      t        |xs d   |      }|S )zParse a string into a Scalar

        Args:
            token: A string containing a scalar, e.g. "3.14fr"

        Raises:
            ScalarParseError: If the value is not a valid scalar

        Returns:
            New scalar
        rU         ?z is not a valid scalarr.   )lowerr   r-   _MATCH_SCALARr   groupsrR   SYMBOL_UNIT)rl   tokenrS   scalarmatchr7   	unit_names          r   parsezScalar.parse   s     ;;=F"dii3F  "%(E}&%2H'IJJ$||~E9u{9?'C\RFr   i   Nc                    | \  }}}|t         j                  k(  r|}	 t        |   ||||xs t              }|S # t        $ r t        dt        |             w xY w)a+  Resolve scalar with units into a dimensions.

        Args:
            size: Size of the container.
            viewport: Size of the viewport (typically terminal size)

        Raises:
            ScalarResolveError: If the unit is unknown.

        Returns:
            A size (in cells)
        zexpected dimensions; found )r   r(   RESOLVE_MAP_FRACTION_ONEKeyErrorr   str)rZ   r8   r9   r:   r7   rN   rS   	dimensions           r   resolvezScalar.resolve
  sv      %)!t\4<<D	R#D)tX}'EI
   	R$'B3t9-%PQQ	Rs	   8 !Ac                x    t        ||n| j                  ||n| j                  ||      S | j                        S )a'  Get a copy of this Scalar, with values optionally modified

        Args:
            value: The new value, or None to keep the same value
            unit: The new unit, or None to keep the same unit
            percent_unit: The new percent_unit, or None to keep the same percent_unit
        )rQ   r7   rN   rS   )rZ   r7   rN   rS   s       r   	copy_withzScalar.copy_with&  sJ     &EDJJ$D$))(4L
 	
 ;?:K:K
 	
r   )returnr~   r   bool)r   z
int | None)r7   rR   r   rQ   )ru   r~   rS   r   r   rQ   N)r8   r   r9   r   r:   zFraction | Noner   r   )NNN)r7   zfloat | NonerN   Unit | NonerS   r   r   rQ   )r   r   r   r   __annotations__r\   propertyr_   ra   rc   re   rg   rX   rj   classmethodrm   r   r   r)   ry   r   r   r   r   r   rQ   rQ      sU   %L
JM ' ' ) ) * * : :
 = =
 & & & & 	9 	9 t48JJ   , tKO$(9H	 : # $(	

 
 "	

 

r   rQ   T)angularc                  b    e Zd ZU dZded<   ded<   edd       Zedd       ZddZddZ	dd	Z
y
)ScalarOffsetz?An Offset with two scalars, used to animate between to Scalars.rQ   xyc                    t         S )z Get a null scalar offset (0, 0).)NULL_SCALAR)rl   s    r   nullzScalarOffset.nullA  s
     r   c                    |\  }} | t        |t        j                  t        j                        t        |t        j                  t        j                              S )zCreate a Scalar offset from a tuple of integers.

        Args:
            offset: Offset in cells.

        Returns:
            New offset.
        )rQ   r   r&   r)   r*   )rl   offsetr   r   s       r   from_offsetzScalarOffset.from_offsetF  sB     11djj$**-1djj$++.
 	
r   c                R    | \  }}t        |j                  xs |j                        S r   )r   r7   )rZ   r   r   s      r   __bool__zScalarOffset.__bool__V  s#    1AGG&qww''r   c              #  p   K   d t        | j                        f d t        | j                        f y wr   )r~   r   r   r^   s    r   __rich_repr__zScalarOffset.__rich_repr__Z  s,     CKCKs   46c           	         | \  }}t        t        |j                  ||            t        |j                  ||                  S )zResolve the offset into cells.

        Args:
            size: Size of container.
            viewport: Size of viewport.

        Returns:
            Offset in cells.
        )r   roundr   )rZ   r8   r9   r   r   s        r   r   zScalarOffset.resolve^  s@     1!))D(+,!))D(+,
 	
r   N)r   r   )r   ztuple[int, int]r   r   r   )r   zrich.repr.Result)r8   r   r9   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   :  sD    III  
 
( 
r   r   c                    | j                         } | j                  d      rt        t        | dd       dz  dd      }|S t        |       }|S )z~Convert a string percentage e.g. '20%' to a float e.g. 20.0.

    Args:
        string: The percentage string to convert.
    r0   Ng      Y@g        rp   )stripendswithr   rR   )stringfloat_percentages     r   percentage_string_to_floatr   r  sT     \\^Fs vcr{!3e!;S#F  !=r   )
r7   rR   r8   r   r9   r   r:   r   r   r   )rM   zIterable[Unit]r   z	list[str])r   r~   r   rR   )5
__future__r   reenumr   r   	fractionsr   	functoolsr   typingr	   r
   	rich.reprrichtextual.geometryr   r   r   	Exceptionr   r   r   r   r&   r'   r(   r)   r*   r+   r,   rL   itemsrt   compilerw   rr   r|   r;   r=   rB   rF   rH   rJ   r{   rO   rQ   reprrU   r   rm   r   r   )kvs   00r   <module>r      s   " 	    '  0 0@) @O OA{ A 
4 
 
 	JJMM4LL#JJKKOOTd !, 1 1 341q!t4

>?EE(,=E"+++(,+=E++"777(,7=E77"888(,8=E88";;;(,;=E;;"<<<(,<=E<<$ 	JJMM$JJKKOO(*	1C
Z C
L 1
: 1
 1
h 6--a0&2D2DQ2GHA
 5s   H