
    i                    ^   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dS )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                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )
_FieldMetastrnameintlowhighdefaultN)__name__
__module____qualname____annotations__     Z/home/piyush/.hermes/hermes-agent/venv/lib/python3.11/site-packages/wcwidth/text_sizing.pyr   r   *   s4         IIIHHHIIILLLLLr   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dS )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   returnr   c                     d                      fdt                                          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   | ]A}t          |j                  |j        k     |j         d t          |j                   V  BdS )=N)getattrr	   r   ).0fieldselfs     r   	<genexpr>z,TextSizingParams.__repr__.<locals>.<genexpr>U   sh       L L!& 'ej 9 9U] J J !&
HHWT5:-F-FHH J J J JL Lr   ())joinr$   values	__class__r   )r.   repr_fmts   ` r   __repr__zTextSizingParams.__repr__N   sq     99 L L L L*<*C*C*E*EL L L L L .)77H7777r   c                    g }t                                           D ]?\  }}t          | |j                  x}|j        k    r|                    | d|            @d                    |          S )z0Build and return sub-part of an OSC 66 sequence.r*   :)r$   itemsr+   r	   r   appendr2   )r.   parts	field_keyr-   vals        r   make_sequencezTextSizingParams.make_sequenceZ   sv     2 8 8 : : 	3 	3IutUZ000U]BB	11C11222xxr   parserawcontrol_codesc                   i }|s
 |             S |                     d          D ]7}d|vr|dk    rt          d|d|          #|                    d          \  }}}t                              |          }||dk    rt          d| d|          t	 t          |          }	n6# t          $ r)}
|dk    rt          d	| d|d
|
           |
Y d}
~
d}
~
ww xY w|dk    rF|	|j        k    s|	|j        k     r0t          d| d|d| d|j         d|j         d|j                   t          |j        t          |j        |	                    ||j        <   9 | di |S )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)
        r8   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)clsr@   rA   kwargspartkey_eqr=   r-   valueexcs              r   from_paramszTextSizingParams.from_paramsc   so   $ )+ 	355LIIcNN 	H 	HD$ H,,$ &Q,0&Q &QIL&Q &Q R R R NN3//MCc&**3//E} H,,$ &D3 &D &D<?&D &D E E E C    H,,$ &K3 &K &K<?&K &KEH&K &K L LQTU  ((eej.@.@EEIDUDU  "CS "C "C8;"C "C7:"C "C?Dz"C "C (-y"C "C 7<j"C "C D D D "%UYEJ0F0F!G!GF5:s}}V}}s   B))
C3CCNr'   r   r?   )r@   r   rA   r   r'   r&   )r   r   r   __doc__r   r   r   r   r   r   r   r6   r>   classmethodrQ   r   r   r   r&   r&   :   s         
 
 ENNNNENNNNIKN
8 
8 
8 
8    0 0 0 0 [0 0 0r   r&   c                  ^    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dS )
TextSizingzBBasic horizontal width measurement for kitty text sizing protocol.r&   paramsr   text
terminatorr?   matchre.Match[str]rA   r'   c                     | t                               |                    d          |          |                    d          |                    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   )rA   r      )rX   rY   rZ   )r&   rQ   group)rJ   r[   rA   s      r   
from_matchzTextSizing.from_match   sX    & s*66u{{1~~Ub6ccA#kk!nn. . . 	.r   r   ambiguous_widthr
   c                    | j         j        dk    r| j         j        | j         j        z  S t          | j        |          }| j         j        t          d|          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   )ra   )rX   r   r   r   rY   rH   )r.   ra   r   s      r   display_widthzTextSizing.display_width   sU     ;q  ;$t{'888TY@@@{ 3q!99,,r   c                X    d| 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   s/    T$+3355TT	T4?TTTr   NrS   )r[   r\   rA   r   r'   rW   )r   )ra   r
   r'   r
   rR   )	r   r   r   rT   r   rU   r`   rc   r>   r   r   r   rW   rW      s         LLIIIOOO. . . . [.,- - - - -"U U U U U Ur   rW   )rT   
__future__r   retyping	_wcswidthr   
NamedTupler   r$   r   r&   rW   r   r   r   <module>rk      s    < # " " " " " 
			            "    
aa	;	;	;	aa	;	;	;	!"a	@	@	@	AB	B	B	B	)qq!	D	D	D	+A	F	F	F-H -H  H H H HZ Z Z Z Zv( Z Z Zz1U 1U 1U 1U 1U" 1U 1U 1U 1U 1Ur   