
    _jw                       S r SSKJr  SSKrSSKJr  SSKJrJrJ	r	J
r
  SSKJr  SSKJr  SS	KJrJr  SS
KJrJrJrJrJr  SSKJrJr  SSKJrJrJr   " S S\R>                  5      r  " S S\
5      r!SS jr"                      SS jr#            SS jr$                    S S jr%                S!S jr&                    S S jr'SSSSSSS.                   S"S jjr(g)#z*This is a python implementation of clip().    )annotationsN)islice)LiteralCallableOptional
NamedTuple   )width)iter_graphemes)	HyperlinkHyperlinkParams)_SGR_STATE_DEFAULT	_SGRState_sgr_state_update_sgr_state_is_active_sgr_state_to_sequence)
TextSizingTextSizingParams)_SEQUENCE_CLASSIFY_HORIZONTAL_CURSOR_MOVEMENTINDETERMINATE_EFFECT_SEQUENCEc                      \ rS rSrSr\R                  " 5       r\R                  " 5       r\R                  " 5       r	\R                  " 5       r
Srg)_HyperlinkAction   z.Outcome of processing an OSC 8 hyperlink unit. N)__name__
__module____qualname____firstlineno____doc__enumautoNO_CLOSEEMPTYOUTSIDEVISIBLE__static_attributes__r       B/home/rurouni/.local/lib/python3.13/site-packages/wcwidth/_clip.pyr   r      s1    8yy{HIIKEiikGiikGr(   r   c                      \ rS rSr% SrS\S'   SrS\S'   SrS\S'   S	rS
\S'   S	r	S
\S'   S	r
S
\S'   SrS\S'   SrS\S'   Srg)_HyperlinkResult"   zb
Result of processing an OSC 8 hyperlink.

Only the fields relevant to each action are populated.
r   actionr   int	close_endinner_width stropen_seqclipped_inner	close_seqclipped_width
hl_col_endr   N)r   r   r   r   r    __annotations__r/   r0   r3   r4   r5   r6   r7   r'   r   r(   r)   r+   r+   "   sU     IsKHcM3IsM3Jr(   r+   c                b    Ub+  [        U5      =n(       a  X -   n [        U5      (       a  U S-  n U $ )z
Apply SGR prefix/suffix around *result*.

If an SGR state was captured at the first visible character, prefix the result with the
corresponding SGR sequence and suffix with a reset if any styles are active.
z[0m)r   r   )resultcaptured_styleprefixs      r)   _apply_sgr_wrapr=   3   s;     !+N;;6;_F//iFMr(   c                  [         R                  " X5      u  pX4S:X  a  [        [        R                  5      $ XU
 n[        XXES9nUS:X  a  [        [        R                  US9$ X-   nX::  d  X:  a  [        [        R                  UUS9$ [        SX-
  5      nX)-
  n[        XUX4USUS9n[        UUXES9n[        [        R                  UUUR                  5       UUR                  5       UUS9$ )	z
Process an OSC 8 hyperlink unit.

Finds the matching close sequence, measures the inner text width, and determines whether the
hyperlink is empty, outside the clip window, or visible (requiring inner-text clipping).
)r?   )control_codestabsizeambiguous_widthr   )r/   )r/   r0   F)fillcharrA   rB   propagate_sgrr@   )r/   r0   r3   r4   r5   r6   r7   )r   
find_closer+   r   r#   r
   r$   r%   maxclipr&   	make_open
make_close)textstartendrC   rA   rB   r@   params	match_endcolclose_startr/   
inner_textr0   r7   inner_clip_startinner_clip_endr4   r6   s                      r)   _process_hyperlinkrT   B   s   ( '11$BK8+ 0 9 9::,JK
 a 0 6 6)LL"Jcj 0 8 8I,79 	9 1ek*YNn'#M ]M
   !!####%#	 	r(   c                8   0 nU H(  u  pgnUR                  U/ 5      R                  Xx45        M*     UR                  5        H  n	U	R                  5         M     U (       a  [	        U R                  5       5      OSn
U(       a  [	        UR                  5       5      OSn[	        X5      n/ nSn[        X5      nX::  a  UR                  US5       H  u  nnUR                  U5        M     X:  a  US-  nM>  X;   a  X   u  nnUR                  U5        UU-  nO%X.s=::  a  U
::  a  O  OUR                  U5        US-  nX::  a  M  [        UR                  5       5       H+  nUU:  d  M  UU    H  u  nnUR                  U5        M     M-     SR                  U5      $ )z
Reconstruct the output string from painter's algorithm state.

Walks columns left-to-right, interleaving escape sequences and cell content, filling gaps with
*fillchar*.
r?   r   r   r	   r1   )

setdefaultappendvaluessortrF   keysmingetsortedjoin)cells	sequencesrK   rL   rC   seqs_by_colcol_posorderseq_textentriesmax_cell_colmax_seq_colmax_colpartswalk_col	col_limit_	cell_textcell_wcs                       r)   _reconstruct_painterrp      sz    57K$- w+22E3DE %.%%' ( ).3uzz|$2L-8#k&&()bK,,GEHG!I

&??8R8KAxLL" 9 ?MH %IvLL#H0L0X&MH! 
& K$$&'y=*1~8X&  . (
 775>r(   c               H
  ^ / n	Sn
SnSnU(       a  [         OSnU[        U 5      :  Ga  X   nX:  a,  US;  a&  Ub  GOU R                  SUS-   5      nUS:X  a  GOUnME  US:X  Gad  [        R                  " X5      nU(       d  U	R                  U5        US-  nM  UR                  S5      b6  U(       a/  Ub,  [        UUR                  5       5      nUR                  5       nM  [        R                  " UR                  5       5      =n(       GaH  [        XX%XdUUUR                  5       U
S9
nUR                  [        R                  L a0  U	R                  UR                  5       5        UR                  5       nOUR                  [        R                  L a  UR                   nOUR                  [        R"                  L a  U
UR$                  -  n
UR                   nOxU	R                  UR&                  5        U	R                  UR(                  5        U	R                  UR*                  5        U(       a  Uc  UnU
UR$                  -  n
UR                   nGM=  UR                  S	5      =nb  UR                  S
5      nUR                  S5      nUb  Uc   e[-        [.        R0                  " UUS9UU5      nUR3                  U5      nX:  a:  U
U-   U::  a1  U	R                  UR5                  5       5        U(       a  Uc  UnU
U-  n
OJX:  a@  U
U-   U:  a7  / mSU4S jjn[7        UXX%UU5      n
U	R9                  T5        U(       a  Uc  UnOU
U-  n
UR                  5       nGMH  UR                  5       nU(       a-  [:        R                  " U5      (       a  [=        SU SU< 35      eU	R                  U5        UR                  5       nGM  US:X  ah  US:  aI  XX-  -
  -   nU
U:  a9  Xs=::  a  U:  a   O  OU	R                  S5        U(       a  Uc  UnU
S-  n
U
U:  a  M9  OU	R                  S5        US-  nGM  [?        [A        XS95      n[C        UUS9nUS:X  a!  Xs=::  a  U:  a  O  OU	R                  U5        OrX:  a'  U
U-   U::  a  U	R                  U5        U(       a  Uc  UnOFX:  aA  U
U-   U:  a8  U	R                  U[E        X*U-   5      [G        X5      -
  -  5        U(       a  Uc  UnU
U-  n
U[        U5      -  nU[        U 5      :  a  GM  SRI                  U	5      U4$ )z
Clip text without cursor movement (simple append-to-output path).

Returns ``(result, captured_style)``.  The caller applies SGR wrapping.
r   Nz	r	   r?   
sgr_paramsrM   rN   rO   ts_metats_textts_termr@   c                (   > TR                  U 5        g )N)rW   )s_w_colts_partss      r)   	_ts_write_clip_simple.<locals>._ts_write'  s     *r(   *Indeterminate cursor sequence at position , 	 rK   rB   r1   )rz   r2   r{   r.   r|   r.   returnNone)%r   lenfindr   matchrW   groupr   rL   r   parserT   r-   r   r#   r$   r/   r%   r0   r3   r4   r5   r   r   from_paramsdisplay_widthmake_sequence_text_sizing_clipextendr   
ValueErrornextr   r
   r[   rF   r^   )rJ   rK   rL   rD   rB   rC   rA   strictr@   outputrO   idxr;   current_stylecharnext_escmhl_staterru   rv   rw   tsts_widthr~   seqnext_tabgrapheme
grapheme_wr}   s                                @r)   _clip_simpler      s   * F
C
C +/N*7&TM
D	/y :$n4)
 yyq1H2~C6>"((3Ad#q ww|$0]}G` 1- Keeg +00;;x;&!#quuwC
 88/888MM!''),%%'CXX!1!7!77++CXX!1!9!991==(C++CMM!**-MM!//2MM!++.$)?)61==(C++C 779--:''),''),*w/BBB$00VW& ++O<<C(Nc$9MM""2"2"45$)?)68OCY3>E#9*,H++C!#C MM(+$)?)68OCeeg '')C7==cBB @Rg  MM#%%'C4<{S]";<Hn)c)c*(^-C-:N1HC Hn d#1HCt788_E
 ?!c!h'\cJ.#5MM(#!7!.Y3+e3MM(c#Z/?&@3u?&RST!7!.zs8}G D	/J 776?N**r(   c           	     b  ^ ^^^ T R                  U5      nX:  a#  X-   U::  a  T" T R                  5       Xq5        X-   $ X:  d  X-   U::  a  X-   $ [        SX!-
  5      n[        X1U-   5      U-
  n	T R                  R
                  m/ n
T R                  R                  S:  a  [        [        T R                  5      T R                  R                  5       H  nU
R                  UT45        M     [        T R                  R                  [        U
5      -
  5       H  nU
R                  ST45        M     O9[        T R                  5       H   nU
R                  U[        XS9T-  45        M"     / mSUUU U4S jjnX-   nSnU
 H  u  nnUU-   nUU::  a  UnM  X:  a    O[        UU	5      [        X5      -
  nUU:X  a/  US:  a)  T(       d  U[        X5      -   nTR                  UU45        O5U" U5        U[        X5      -   n[        U5       H  nT" USUU-   5        M     UnM     U" U5        X-   $ )z
Emit tokens for a text-sizing (OSC 66) sequence, clipped to (start, end).

Calls *write_cells(text, width, col)* for each emitted cell or sequence. Returns new column
position.
r   r1   r   c                  > T(       d  g T Vs/ s H  oS   PM	     nn[        S T 5       5      n[        TTR                  R                  S:  a  [	        U5      OSTR                  R
                  TR                  R                  TR                  R                  TR                  R                  5      nT" [        USR                  U5      TR                  5      R                  5       UU 5        TR                  5         g s  snf )Nr   c              3  *   #    U  H	  oS    v   M     g7f)r	   Nr   ).0us     r)   	<genexpr>3_text_sizing_clip.<locals>.flush.<locals>.<genexpr>  s     2MqdMs   r1   )sumr   rM   r
   r   	numeratordenominatorvertical_alignhorizontal_alignr   r^   
terminatorr   clear)		flush_colr   textstotal_wrM   pending_unitsscaler   write_cellss	        r)   flush _text_sizing_clip.<locals>.flush  s    ,-}!1}-2M22!))//A-CJ1II!6!6II$$bii&@&@	B
 	vrwwu~r}}=KKM	 	 .s   Dr	   )r   r.   r   r   )r   r   rF   r[   rM   r   r
   r   r   rJ   rW   ranger   )r   rO   rK   rL   rC   rB   r   r   	rel_startrel_endunitsgrl   r   flush_col_posunit_pos	unit_textunit_wunit_endoverlap	abs_startir   r   s   `     `               @@r)   r   r   f  s     0H |#-B$$&6~
zS^u,~ Au{#I#X~&,GIIOOE#%E	yyrww/AALL!U$ BryyU34ALL"e% 5  (ALL!U1FNOP ) ,.M   OMH"	6f$y Hh(3x+CCf!  #c(&> >  )V!45- c(66I7^HaQ7 $% #( 
->r(   c                 ^^^#^$^%^& 0 m$[        5       m&/ n	Sn
SnSnSm#T(       a  [        OSm% S   SU#U$U%UU&U4S jjjnU[        U 5      :  Ga  X   nX:  a  T#b  US:w  a  GOUS:X  Gaq  [        R                  " X5      nU(       d+  U	R                  XU45        U
S-  n
T(       a  T#c  T%m#US-  nMs  UR                  S5      b6  T(       a/  T%b,  [        T%UR                  5       5      m%UR                  5       nM  [        R                  " UR                  5       5      =n(       Gax  [        XUTXdUUUR                  5       US9
nUR                  [        R                  L aC  U	R                  XUR                  5       45        U
S-  n
T(       a  T#c  T%m#UR                  5       nOUR                  [        R                  L a  UR                   nOUR                  [        R"                  L a  UUR$                  -  nUR                   nOU	R                  XUR&                  45        U
S-  n
T(       a  T#c  T%m#U" UR(                  UR*                  USS	9  UUR*                  -  nU	R                  XUR,                  45        U
S-  n
UR.                  nUR                   nGM_  UR                  S
5      =nby  UR                  S5      nUR                  S5      nUb  Uc   e[1        [2        R4                  " UUS9UU5      n[7        UXUTUU5      nT(       a  T#c  T%m#UR                  5       nGM  UR                  5       nU(       a-  [8        R                  " U5      (       a  [;        SU SU< 35      eUR                  S5      =nb*  U(       a  [=        U5      S-
  OSnUR                  5       nGMn  UR                  S5      =nbi  U(       a  [=        U5      OSnUU-   nX:  a6  UU:  a0  [?        [A        X5      [C        UU5      5       H  nU" TSU5        M     UnUR                  5       nGM  UR                  S5      =nbX  U(       a  [=        U5      OSnU(       a  UU:  a  [;        SU SU SU S35      e[A        SUU-
  5      nUR                  5       nGMW  U	R                  XUR                  5       45        U
S-  n
T(       a  T#c  T%m#UR                  5       nGM  US:X  a
  SnUS-  nGM  US:X  a  US:  a  US-  nUS-  nGM  US:X  ah  US:  a6  XX-  -
  -   nUU:  a&  Xs=::  a  U:  a  O  O
U" TSU5        US-  nUU:  a  M&  O$U	R                  XS45        U
S-  n
T(       a  T#c  T%m#US-  nGM3  [E        [G        XS95      n[I        UUS9n U S:X  a4  Xs=::  a  U:  a'  O  OU	R                  XU45        U
S-  n
T(       a  T#c  T%m#O`X:  a  UU -   U::  a  U" UU U5        OGX:  aB  UU -   U:  a9  [A        X5      n![?        [C        X+U -   5      U!-
  5       H  n"U" TSU!U"-   5        M     UU -  nU[        U5      -  nU[        U 5      :  a  GM  [K        T$XUT5      T#4$ )z
Clip text with cursor movement (painter's algorithm path).

Returns ``(result, captured_style)``.  The caller applies SGR wrapping.
r   Nc                  > [        U5       H  nX$-   nUS:  a:  TR                  US-
  S5      S   S:X  a  T	S4TUS-
  '   T
R                  US-
  5        TR                  US5      S   S:X  a  T	S4TUS-   '   T
R                  US-   5        TR                  US5        T
R                  U5        M     X4TU'   U(       a%  [        U5       H  nT
R	                  X$-   5        M     T(       a  Tc  Tmggg)zIWrite *w* cells of text *s* at *write_col*, handling wide-char splitting.r   r	   )r1   r      N)r   r\   discardpopadd)rz   w	write_colis_hyperlinkoffsetsrc_colr;   r_   r   rC   hyperlink_cellsrD   s         r)   _write_cells#_clip_painter.<locals>._write_cells  s     AhF(G{uyy1g>qAQF&.]gk"''!4yy'*1-2&.]gk"''!4IIgt$##G,  6i(##I$67 #^3*N 4=r(   rr   r	   rs   rt   T)r   ru   rv   rw   rx   r   r   hpa_n
cforward_ncbackward_nz!Cursor left movement at position z would move z cells left from column z, exceeding string startr   r   r   )F)
rz   r2   r   r.   r   r.   r   boolr   r   )&setr   r   r   r   rW   r   r   rL   r   r   rT   r-   r   r#   r$   r/   r%   r0   r3   r4   r6   r5   r7   r   r   r   r   r   r   r.   r   rF   r[   r   r   r
   rp   )'rJ   rK   rL   rD   rB   rC   rA   r   r@   r`   	seq_orderrO   r   r   r   r   r   r   ru   rv   rw   r   r   r   r   	n_forwardmove_endr   r   
n_backwardr   r   r   
clip_startr   r;   r_   r   r   s'      ` `                             @@@@r)   _clip_painterr     sk   * )+E #O,.II
C
C +/N*7&TM +0+#'+48+ +* D	/y :.46>"((3A  #$!78Q	 ^%;%2Nq ww|$0]}G` 1- Keeg +00;;x;&h!#quuwC
 88/888$$caggi%@ANI$)?)6%%'CXX!1!7!77++CXX!1!9!991==(C++C$$cajj%ABNI$)?)6 !//3.241??*C$$cakk%BCNI,,C++C 779--:''),''),*w/BBB$00VW& (C? " !^%;%2Neeg '')C7==cBB @Rg  ))6(-c%j1n1eeg  ggl33
@/9C
Oq	?9E!1"3s?C#4FG$Xq!4 Heeg  !ww}55B1<S-!
j3.$;C5%,&>se D12 
 !S:-.eeg caggi89NI!7!.%%'C 4<C1HC 6>Qwq1HC 4<{S]";<Hn)c)$Xq#61HC Hn
   #$!78Q	 ^%;%2N1HC t788_E
 ?!c!  #(!;<Q	 ^%;%2N\cJ.#5:s3Y3+e3UJCz)9 :Z GHXq*v*=> I 	zs8}M D	/P  yhGWWr(   r      Tr   )rC   rA   rB   rD   r@   
overtypingc                  [        US5      nX!::  a  gU R                  5       (       a  U R                  5       (       a  XU $ SU ;   n	U(       a	  U	(       d  SnUcM  US:g  =(       a@    SU ;   =(       d4    SU ;   =(       d(    U	=(       a    [        [        R
                  " U 5      5      nOU(       a  US:X  a  SnU(       a  [        O[        n
[        U
" U UUUUUUUS:H  US	9	6 $ )
u  
Clip text to display columns (start, end) while preserving all terminal sequences.

This function extracts a substring based on visible column positions rather than
character indices. Terminal escape sequences are preserved in the output since
they have zero display width. If a wide character (width 2) is split at
either boundary, it is replaced with ``fillchar``.

TAB characters (``\t``) are expanded to spaces up to the next tab stop,
controlled by the ``tabsize`` parameter. When cursor movement is detected,
a "painter's algorithm" is used, cursor movements actively change the write
position, allowing cursor-left and carriage return to overwrite previously
written cells. It is assumed that ``text`` begins at column 0.

**OSC 8 hyperlinks** are handled specially: the visible text inside a hyperlink
is clipped to the requested column range, and the hyperlink is rebuilt around
the clipped text.  Empty hyperlinks (those with no remaining visible text after
clipping) are removed::

    >>> clip('\x1b]8;;http://example.com\x07Click This link\x1b]8;;\x07', 6, 10)
    '\x1b]8;;http://example.com\x07This\x1b]8;;\x07'

:param text: String to clip, may contain terminal escape sequences.
:param start: Absolute starting column (inclusive, 0-indexed).
:param end: Absolute ending column (exclusive).
:param fillchar: Character to use when a wide character must be split at
    a boundary (default space). Must have display width of 1.
:param tabsize: Tab stop width (default 8). Set to 0 to pass tabs through
    as zero-width (preserved in output but don't advance column position).
:param ambiguous_width: Width to use for East Asian Ambiguous (A)
    characters. Default is ``1`` (narrow). Set to ``2`` for CJK contexts.
:param propagate_sgr: If True (default), SGR (terminal styling) sequences
    are propagated. The result begins with any active style at the start
    position and ends with a reset sequence if styles are active.
:param control_codes: How to handle control characters and sequences:

    - ``'parse'`` (default): Track horizontal cursor movement and clip
      hyperlink text.  Cursor overwrite is always allowed, with best effort
      results; indeterminate sequences (home, clear, reset, etc.) are
      preserved as zero-width.
    - ``'strict'``: Like ``parse``, but raises :exc:`ValueError` on
      sequences with indeterminate effects (cursor home, clear screen,
      reset, vertical movement, etc.) matching :func:`width` behavior.
      Also raises on out-of-bounds horizontal cursor movement.
    - ``'ignore'``: All control characters are treated as zero-width.
      Cursor movement is not tracked (fastest path).

:param overtyping: Whether to use the painter's algorithm for cursor
    movement (``\b`` backspace, ``\r`` carriage return, and CSI cursor
    left/right/position sequences).  When ``None`` (default), auto-detects
    by scanning for these characters in *text*.  Set to ``False`` for improved
    performance when the caller knows *text* contains no cursor movement
    characters.  Set to ``True`` to force the painter's algorithm (useful
    for testing).  Has no effect when ``control_codes='ignore'``.

:returns: Substring of ``text`` spanning display columns (start, end),
    with all terminal sequences preserved and wide characters at boundaries
    replaced with ``fillchar``.

:raises ValueError: If ``control_codes='strict'`` and an indeterminate-effect
    sequence or out-of-bounds cursor movement is encountered.

SGR (terminal styling) sequences are propagated by default. The result
begins with any active style and ends with a reset::

    >>> clip('\x1b[1;34mHello world\x1b[0m', 6, 11)
    '\x1b[1;34mworld\x1b[0m'

Set ``propagate_sgr=False`` to disable this behavior.

.. versionadded:: 0.3.0

.. versionchanged:: 0.5.0
   Added ``propagate_sgr`` parameter (default True).

.. versionchanged:: 0.7.0
   Added ``control_codes`` parameter (default 'parse').
   OSC 8 hyperlink-aware clipping.  OSC 66 text sizing protocol support.
   Added ``overtyping`` parameter (default None, auto-detect).

Example::

    >>> clip('hello world', 0, 5)
    'hello'
    >>> clip('中文字', 0, 3)  # Wide char split at column 3
    '中 '
    >>> clip('a\tb', 0, 10)  # Tab expanded to spaces
    'a       b'
r   r1   rr   Fignorer   r   r   )	rJ   rK   rL   rD   rB   rC   rA   r   r@   )	rF   isasciiisprintabler   r   searchr   r   r=   )rJ   rK   rL   rC   rA   rB   rD   r@   r   has_escfn_clips              r)   rG   rG     s    J qME
| ||~~$**,,# nGW  X% Kt^ Jtt| JH$:AA$GH 	
 
1
)m|GG#')#
 
 
r(   )r:   r2   r;   zOptional[_SGRState]r   r2   )rJ   r2   rK   r.   rL   r.   rC   r2   rA   r.   rB   r.   r@   $Literal['parse', 'strict', 'ignore']rM   r   rN   r.   rO   r.   r   r+   )r_   zdict[int, tuple[str, int]]r`   zlist[tuple[int, int, str]]rK   r.   rL   r.   rC   r2   r   r2   )rJ   r2   rK   r.   rL   r.   rD   r   rB   r.   rC   r2   rA   r.   r   r   r@   r   r   ztuple[str, Optional[_SGRState]])r   r   rO   r.   rK   r.   rL   r.   rC   r2   rB   r.   r   zCallable[[str, int, int], None]r   r.   )rJ   r2   rK   r.   rL   r.   rC   r2   rA   r.   rB   r.   rD   r   r@   r   r   zOptional[bool]r   r2   ))r    
__future__r   r!   	itertoolsr   typingr   r   r   r   _widthr
   r   r   	hyperlinkr   r   	sgr_stater   r   r   r   r   text_sizingr   r   escape_sequencesr   r   r   Enumr   r+   r=   rT   rp   r   r   r   rG   r   r(   r)   <module>r      s   0 "   : :  $ 10 0
 6> >
tyy z "?
?? 
? 	?
 ? ? 8? ? ? 
? ?D4%4)4 4 
	4
 4 	4nh+
h+h+ 
h+
 h+ h+ h+ h+ h+ 8h+ %h+VRR	R R 
	R
 R R 1R 	RjcX
cXcX 
cX
 cX cX cX cX cX 8cX %cXV :A!%H
HH 
H
 H H H H 8H H 	Hr(   