o
    Œõ±j
  ã                   @  sÊ   U d Z ddlmZ ddlZddlZddlmZ G dd„ dejƒZeddd	dd
�eddd	dd
�eddddd
�eddddd
�eddddd
�eddddd
�dœZ	de
d< G dd„ dejƒZG dd„ dejƒZdS )aÓ  
`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

The ``text`` field is optional; when omitted, the sequence occupies
``s * w`` cells (or zero when ``w`` is also unset).

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                   @  s.   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__© r   r   ú†/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/wcwidth/text_sizing.pyr   -   s
   
 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                   @  s~   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d„Zddd„Zed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                   s0   d  ‡ fd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  s:   � | ]}t ˆ |jƒ|jkr|j› d t ˆ |jƒ› �V  qdS )ú=N)Úgetattrr   r   )Ú.0Úfield©Úselfr   r   Ú	<genexpr>X   s   € þþz,TextSizingParams.__repr__.<locals>.<genexpr>ú(ú))Újoinr!   ÚvaluesÚ	__class__r   )r)   Úrepr_fmtr   r(   r   Ú__repr__Q   s   ÿzTextSizingParams.__repr__c                 C  sJ   g }t  ¡ D ]\}}t| |jƒ }|jkr| |› d|› �¡ qd |¡S )z0Build and return sub-part of an OSC 66 sequence.r$   ú:)r!   Úitemsr%   r   r   Úappendr-   )r)   ÚpartsÚ	field_keyr'   Úvalr   r   r   Úmake_sequence]   s   €
zTextSizingParams.make_sequenceÚparseÚrawÚcontrol_codesc                 C  sP  i }|s| ƒ S |  d¡D ]”}d|vr!|dkr td|›d|›�ƒ‚q| d¡\}}}t |¡}|du rA|dkr@td|› d|›�ƒ‚qzt|ƒ}	W n$ tyk }
 z|dkratd	|› d|›d
|
› �ƒ|
‚W Y d}
~
qd}
~
ww |dkr“|	|jksz|	|jk r“td|› d|›d|› d|j› d|j› d|j› �ƒ‚t	|jt
|j|	ƒƒ||j< q| 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)
        r2   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:   r;   ÚkwargsÚpartÚkeyÚ_eqr7   r'   ÚvalueÚexcr   r   r   Úfrom_paramsf   s`   ÿÿ

ÿ
ÿÿÿ€û
ÿþþýýzTextSizingParams.from_paramsN©r#   r   ©r9   )r:   r   r;   r   r#   r"   )r   r   r   Ú__doc__r   r   r   r   r   r   r   r1   r8   ÚclassmethodrJ   r   r   r   r   r"   =   s   
 

	r"   c                   @  sP   e Zd ZU dZded< ded< ded< edddd„ƒZdddd„Zddd„ZdS )Ú
TextSizingzBBasic horizontal width measurement for kitty text sizing protocol.r"   Úparamsr   ÚtextÚ
terminatorr9   Úmatchúre.Match[str]r;   r#   c                 C  s.   | t j| d¡|d�| d¡p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(width=2), text='XY', terminator='\x07')
        r   )r;   r   Ú é   )rP   rQ   rR   )r"   rJ   Úgroup)rC   rS   r;   r   r   r   Ú
from_match    s   þzTextSizing.from_matchr   Úambiguous_widthr   c                 C  sB   | j jdkr| j j| j j S t| j|d�}|dk rd}| j j| 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   )rY   )rP   r   r   r   rQ   )r)   rY   r   r   r   r   Údisplay_width¶   s   zTextSizing.display_widthc                 C  s   d| j  ¡ › d| j› | j› �S )z3Build and return complete OSC 66 Terminal Sequence.z]66;ú;)rP   r8   rQ   rR   r(   r   r   r   r8   É   s   zTextSizing.make_sequenceNrL   )rS   rT   r;   r   r#   rO   )r   )rY   r   r#   r   rK   )	r   r   r   rM   r   rN   rX   rZ   r8   r   r   r   r   rO   ™   s   
 rO   )rM   Ú
__future__r   ÚreÚtypingÚ	_wcswidthr   Ú
NamedTupler   r!   r   r"   rO   r   r   r   r   Ú<module>   s    !ú	\