
    ^j,                    D   U d Z ddlmZ ddlZddlZddlmZ  G d dej                        Z eddd	d
       eddd	d
       edddd
       edddd
       edddd
       edddd
      dZ	de
d<    G d dej                        Z G d dej                        Zy)aV  
`kitty text sizing protocol`_ (OSC 66) parsing and measurement.

The kitty text sizing protocol allows terminal apps to explicitly tell
terminals how many cells text occupies, using the escape sequence::

    ESC ] 66 ; metadata ; text BEL/ST

Metadata is colon-separated ``key=value`` pairs:

- ``s``: scale
- ``w``: width in cells
- ``n``: fractional numerator
- ``d``: fractional denominator
- ``v``: vertical alignment
- ``h``: horizontal alignment

Parsing is pretty straight-forward:

- When ``w > 0``, return ``s * w``.
- Otherwise ``w == 0``, ``s * wcswidth(inner_text_width)`` cells.

Numerator, denominator, and alignment codes and values are parsed but otherwise ignored
and have no effect on measurements made in this library.

.. _`kitty text sizing protocol`: https://sw.kovidgoyal.net/kitty/text-sizing-protocol/

.. versionadded:: 0.7.0
    )annotationsN   )wcswidthc                  6    e Zd ZU ded<   ded<   ded<   ded<   y)
_FieldMetastrnameintlowhighdefaultN)__name__
__module____qualname____annotations__     L/opt/ringagent/.cad-venv/lib/python3.12/site-packages/wcwidth/text_sizing.pyr   r   *   s    
I	H
ILr   r   scale   )r	   r   r   r   width	numerator   denominatorvertical_align   horizontal_align)swndvhzdict[str, _FieldMeta]TEXT_FIELD_MAPPINGc                      e Zd ZU dZ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dZddZeddd       Zy)TextSizingParamsa/  
    Parsed parameters from a text sizing escape sequence (OSC 66).

    :param scale: Scale factor (1-7). Text occupies ``scale`` rows tall and ``scale * width``
        columns wide.
    :param width: Width in cells (0-7). When 0, width is auto-calculated from the inner text.
    :param numerator: Fractional scaling numerator (0-15).
    :param denominator: Fractional scaling denominator (0-15).
    :param vertical_align: Vertical alignment (0=top, 1=bottom, 2=center).
    :param horizontal_align: Horizontal alignment (0=left, 1=right, 2=center).
    r   r
   r   r   r   r   r   r   r   c                     dj                   fdt        j                         D              } j                  j                   d| dS )z
        Return a compact representation including only non-default fields.

        This avoids verbose output when most fields are defaults.
        z, c              3     K   | ]L  }t        |j                        |j                  k7  r'|j                   d t        |j                          N yw)=N)getattrr	   r   ).0fieldselfs     r   	<genexpr>z,TextSizingParams.__repr__.<locals>.<genexpr>U   sM      L!& 'ejj 9U]] J !&

|1WT5::-F,GH Ls   AA())joinr$   values	__class__r   )r-   repr_fmts   ` r   __repr__zTextSizingParams.__repr__N   sL     99 L*<*C*C*EL L ..))*!H:Q77r   c                    g }t         j                         D ]A  \  }}t        | |j                        x}|j                  k7  s,|j                  | d|        C dj                  |      S )z0Build and return sub-part of an OSC 66 sequence.r)   :)r$   itemsr*   r	   r   appendr1   )r-   parts	field_keyr,   vals        r   make_sequencezTextSizingParams.make_sequenceZ   sg     2 8 8 : 	3IutUZZ00U]]B	{!C512	3 xxr   c                   i }|s |        S |j                  d      D ]  }d|vr|dk(  rt        d|d|      |j                  d      \  }}}t        j	                  |      }||dk(  rt        d| d|      b	 t        |      }	|dk(  rY|	|j                  kD  s|	|j                  k  r;t        d| d|d| d|j                   d|j                   d|j                         t        |j                  t        |j                  |	            ||j                  <     | di |S # t        $ r%}
|dk(  rt        d	| d|d
|
       |
Y d}
~
7d}
~
ww xY w)a  
        Parse colon-separated ``key=value`` metadata string.

        :param raw: Metadata string, e.g. ``'s=2:w=3'``.
        :param control_codes: 'parse' or 'strict'.
        :raises ValueError: If ``control_codes='strict'`` unrecognized text sizing parameters raise
            ValueError.
        :returns: Parsed parameters with values clamped to valid ranges.
            Unknown keys are ignored. Non-integer values use defaults.

        Example::

            >>> TextSizingParams.from_params('s=2:w=3')
            TextSizingParams(scale=2, width=3, numerator=0, denominator=0,             vertical_align=0, horizontal_align=0)
        r7   r)   strictz5Expected '=' in text sizing parameter (key=val), got z in OSC 66 sequence, NzUnknown text sizing field 'z' in OSC 66 sequence, zIllegal text sizing value 'z: z!Out of bounds text sizing value 'z: allowed range for 'z' (z) is z to r   )split
ValueError	partitionr$   getr
   r   r   r	   maxmin)clsrawcontrol_codeskwargspartkey_eqr<   r,   valueexcs              r   from_paramszTextSizingParams.from_paramsc   s   $ )+5LIIcN 	HD$ H,$ (,,083H&Q R R NN3/MCc&**3/E} H,$'B3% H<<?7&D E E C (eejj.@EEIIDU #DSE J88;w ?77:e3uzzl K'',yykejj\"C D D "%UYYEJJ0F!GF5::7	H8 }V}   H,$'B3% H<<?7"SE&K LQTU s    D++	E4EENreturnr   parse)rG   r   rH   r   rQ   r&   )r   r   r   __doc__r   r   r   r   r   r   r   r5   r=   classmethodrO   r   r   r   r&   r&   :   sb    
 E3NE3NIsKNCc
8 0 0r   r&   c                  V    e Zd ZU dZded<   ded<   ded<   eddd       ZdddZdd	Zy
)
TextSizingzBBasic horizontal width measurement for kitty text sizing protocol.r&   paramsr   text
terminatorc                     | t         j                  |j                  d      |      |j                  d      |j                  d            S )a.  
        Parse using matching OSC 66 Sequence.

        :param match: match object from :attr:`wcwidth.escape_sequences.TEXT_SIZING_PATTERN`.
        :param control_codes: 'parse' or 'strict', same meaning as delegated by
            :func:`wcwidth.width`.
        :raises ValueError: When ``control_codes='strict'`` for unrecognized, invalid, or out of
            bounds text sizing parameters.
        :returns: TextSizing object from parsed sequence

        Example::

            from wcwidth.escape_sequences import TEXT_SIZING_PATTERN
            >>> TextSizing.from_match(TEXT_SIZING_PATTERN.match('\x1b]66;w=2;XY\x07'))
            TextSizing(params=TextSizingParams(scale=1, width=2, numerator=0, denominator=0, \
            vertical_align=0, horizontal_align=0), text='XY', terminator='\x07')
        r   )rH   r      )rX   rY   rZ   )r&   rO   group)rF   matchrH   s      r   
from_matchzTextSizing.from_match   sC    & *66u{{1~Ub6cA#kk!n. 	.r   c                    | j                   j                  dkD  r-| j                   j                  | j                   j                  z  S t        | j                  |      }|dk  rd}| j                   j                  |z  S )a  
        Calculate the display width of a text sizing sequence.

        :param ambiguous_width: Width for East Asian Ambiguous characters.
        :returns: Display width in terminal cells. When ``width > 0``, returns
            ``params.scale * params.width``. When ``width == 0``, returns
            ``params.scale * measured_inner_width``.

        .. note: Fractional scaling (numerator/denominator) does not affect the
            cell count, it adjusts only the font size within the cells allocated by 'w'.
        r   )ambiguous_width)rX   r   r   r   rY   )r-   ra   r   s      r   display_widthzTextSizing.display_width   sf     ;;q ;;$$t{{'8'888TYY@q5A{{  1$$r   c                n    d| j                   j                          d| j                   | j                   S )z3Build and return complete OSC 66 Terminal Sequence.z]66;;)rX   r=   rY   rZ   )r-   s    r   r=   zTextSizing.make_sequence   s0    $++3356a		{4??BSTTr   NrR   )r^   zre.Match[str]rH   r   rQ   rW   )r   )ra   r
   rQ   r
   rP   )	r   r   r   rT   r   rU   r_   rb   r=   r   r   r   rW   rW      s2    L
IO. .,%&Ur   rW   )rT   
__future__r   retyping	_wcswidthr   
NamedTupler   r$   r   r&   rW   r   r   r   <module>rj      s   < # 
   ""  
aa	;	aa	;	!"a	@	AB	B	)qq!	D	+A	F-H ) HZv(( Zz3U"" 3Ur   