o
    jE                     @  s   d Z ddlmZ ddlmZ g dZddlmZ ddlm	Z	 ddl
m
Z
 dd	lmZmZmZmZmZmZmZmZ dd
lmZ ddlmZ ddlmZ d#ddZ			d$d%ddZ				d&d'd!d"ZdS )(z.This is a python implementation of wcswidth().    )annotations)Optional)zwcwidth._constantszwcwidth._wcwidthzwcwidth.bisearchzwcwidth.table_graphemezwcwidth.table_vs16   )table_grapheme_overrideswcwidth)bisearch)_EMOJI_ZWJ_SET_ISC_VIRAMA_SET_CATEGORY_MC_TABLE_FITZPATRICK_RANGE_REGIONAL_INDICATOR_SETresolve_terminalget_term_overrides_clamp_ambiguous_width)VS15_WIDE_TO_NARROW)VS16_NARROW_TO_WIDE)GRAPHEME_EXTENDtextstrstartintendreturnc                 C  s   |d }||k r"t t| | tr"|d7 }||k r"t t| | ts||k rgt| | dkr1	 |S |d7 }||k rdt| | tv rd|d7 }||k rct t| | trc|d7 }||k rct t| | tsRq"	 |S |S )z
    Scan forward from *start* (base character) to end of a ZWJ grapheme cluster.

    Follows the UAX #29 GB11 pattern (ExtPict Extend* ZWJ x ExtPict) chained repeatedly until no
    more ZWJ joins are found.
    r      )r   ordr   r	   )r   r   r   idx r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/wcwidth/_wcswidth.py_scan_zwj_cluster_end   s"   r   NautopwcsnOptional[int]unicode_versionambiguous_widthc                   s  |du r|   r|  rt| S t   dkrtn fdd}|du r(t| nt|t| }d}d}d}d}	d}
d}d}td	 }td	 }t}||k rK| | }t	|}|d
kru|r_|d7 }n|d |k rnd}
d}|d7 }nd}|d7 }qG|dkr|dkr||	|rd}d}|d7 }qG|dkr|dkrt|	|r|
dkr|d8 }d}|d7 }qG|dkr|t
v rd}|d }|dkrt	| | t
v r|d7 }|d8 }|dkrt	| | t
v s|d dkr|}	|d7 }qGntd |  krtd krn n	|	tv r|d7 }qG||}|dk rdS |dkr'|rd}n|r||7 }|}n|}|}|}	|}
d}n|tv r/d}n|dkrA||trAd}d}d}nd}|d7 }||k sL|rR||7 }|S )a  
    Given a unicode string, return its printable length on a terminal.

    See :ref:`Specification` for details of cell measurement.

    This implementation differs from Markus Khun's original POSIX C implementation, in that this
    ``wcswidth()`` processes graphemes strings yielded by :func:`wcwidth.iter_graphemes` defined by
    `Unicode Standard Annex #29`_. POSIX wcswidth(3) is not grapheme-aware and does not measure many
    kinds of Emojis or complex scripts correctly.

    :param pwcs: Measure width of given unicode string.
    :param n: When ``n`` is None (default), return the length of the entire
        string, otherwise only the first ``n`` characters are measured.
    :param unicode_version: Ignored. Retained for backwards compatibility.

        .. deprecated:: 0.3.0
           Only the latest Unicode version is now shipped.

    :param ambiguous_width: Width to use for East Asian Ambiguous (A)
        characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
    :returns: The width, in cells, needed to display the first ``n`` characters
        of the unicode string ``pwcs``.  Returns ``-1`` for C0 and C1 control
        characters!

    .. _`Unicode Standard Annex #29`: https://www.unicode.org/reports/tr29/
    Nr   c                      t | d S Nr    r   cr%   r   r   <lambda>f       zwcswidth.<locals>.<lambda>r   F9.0.0r            T)isasciiisprintablelenr   r   minr   r   r   r   r   r   r	   r
   r   )r!   r"   r$   r%   _wcwidthr   total_widthr   last_measured_idxlast_measured_ucslast_measured_wprev_was_viramacluster_widthvs16_nw_tablevs15_wn_table	_bisearchcharucs	ri_beforejwr   r*   r   wcswidth=   s   $



 


QrG   Tterm_program
bool | strc           )        s  |du r|   r|  rt| S t  t|}|r6t|}|j}|j}|j}	|j	}
|j
}|j}t|}nd}d}d}	d}
d}d}i } dkrJtn fdd}|du rXt| nt|t| }d}d}d}d}d}d	}d}d}d}td
 }td
 }t}||k rb| | }t|}|dkr|r|d7 }nG|d |k r|r|dkr|tv rt| ||}| || }||} | dur|| | 7 }d}d}d}d	}d}|}q{d}d	}|d7 }nd	}|d7 }q{|dkr|dkr|r|||rn|||rd}d}|d7 }q{|dkr(|dkr(|}!t|!|}"|	rt|!|	rd	}"|"r!|dkr!|d8 }d}|d7 }q{|dkr|tv rkd}#|d }$|$dkr\t| |$ tv r\|#d7 }#|$d8 }$|$dkr\t| |$ tv sF|#d dkrj|}|d7 }q{ntd |  kr{td krn n
|tv r|d7 }q{||}%|%dk rdS |%dkr|rt||rd}%n|%dkr|
rt||
rd}%|%dkr|rt||rd}%n|%dkr|rt||rd}%|%dkr>|rd}nV|r/d	}&|r!|dkr!| ||d  }'||'} | dur||  }d}&d}n| || }(||(} | dur||  }n	||7 }n||7 }|&s.|%}|}|}n|%}|}|}|}|}|%}d	}n|tv rFd}n|dkrX||trXd}d}d	}nd	}|d7 }||k s|r|r|dkr| || }(||(} | dur||  }|S ||7 }|S ||7 }|S )a  
    Given a unicode string, return its printable length on a terminal given by ``term_program``.

    See :ref:`Specification` for details of cell measurement.

    Unlike :func:`wcswidth`, this function applies per-terminal correction tables for
    emoji presentation and grapheme clusters.

    :param pwcs: Measure width of given unicode string.
    :param n: When ``n`` is None (default), return the length of the entire
        string, otherwise only the first ``n`` characters are measured.
    :param unicode_version: Ignored. Retained for backwards compatibility.
    :param ambiguous_width: Width to use for East Asian Ambiguous (A)
        characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
    :param term_program: Terminal software identifier for table correction.
        ``True`` (default) reads the ``TERM_PROGRAM`` or ``TERM`` environment
        variable for auto-detection.  ``False`` disables override lookup.
        Accepts a canonical terminal name matching :func:`list_term_programs`,
        such as from XTVERSION_, ENQ_, or ``TERM_PROGRAM``.

        .. versionadded:: 0.8.0
    :returns: The width, in cells, needed to display the first ``n`` characters
        of the unicode string ``pwcs``.  Returns ``-1`` for C0 and C1 control
        characters!
    Nr   r   c                   r&   r'   r   r(   r*   r   r   r+     r,   zwcstwidth.<locals>.<lambda>r   r-   r.   Fr/   r   r0   r1   r2   r3   T)r4   r5   r6   r   r   r   narrowervs16_narrower
vs15_widerzeroernarrow_widernarrow_zeroerr   getr   r7   r   r   r   r   r	   r   r   r   r
   r   ))r!   r"   r$   r%   rH   term_canonical	overrides	_narrower_vs16_narrower_vs15_wider_zeroer_narrow_wider_narrow_zeroer_grapheme_overridesr8   r   r9   r   r:   r;   r<   r=   cluster_starttotal_before_clusterr>   r?   r@   rA   rB   rC   cluster_endcluster
override_wbase_ucsvs15_narrowrD   rE   rF   flushed	candidatecluster_textr   r*   r   	wcstwidth   s@  %







$









  

rd   )r   r   r   r   r   r   r   r   )Nr    r   )
r!   r   r"   r#   r$   r   r%   r   r   r   )Nr    r   T)r!   r   r"   r#   r$   r   r%   r   rH   rI   r   r   )__doc__
__future__r   typingr   __lazy_modules__ r   r8   r   r   
_constantsr	   r
   r   r   r   r   r   r   
table_vs15r   
table_vs16r   table_graphemer   r   rG   rd   r   r   r   r   <module>   s,    (
  