
    jj2                    $   U d dl mZ d dlZd dlZd dlZd dlZej                  rd dlmZ  ej                  d      Z
 ej                  d      Zdaded<   dd	Zdd
ZddZddZddZddZddZddZd dZd!dZd"dZd#dZd"dZd"dZd$dZy)%    )annotationsN)Literalz^[ -~]*$s   ^[ -~]*$narrow!Literal['utf8', 'narrow', 'wide']_byte_encodingc                t    t        j                  dt        d       t        j                  |       x}dk\  r|S y)z[
    Return the screen column width for a single character.

    .. deprecated:: 3.0.4
    z6get_char_width is deprecated in favor of wcwidth.width   
stacklevelr   )warningswarnDeprecationWarningwcwidth)charwidths     W/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/str_util.pyget_char_widthr   &   s9     MM@
 &&1,    c                    t        j                  dt        d       t        j                  t	        |             x}dk\  r|S y)zZ
    Return the screen column width for unicode ordinal o.

    .. deprecated:: 3.0.4
    z1get_width is deprecated in favor of wcwidth.widthr	   r
   r   )r   r   r   r   chr)or   s     r   	get_widthr   7   s=     MM;
 Q((Q.r   c                    | || j                  d      }t        t        j                  |      d      }|j	                  d      }||t        |      z   fS )a/  
    Decode bytes starting at `start` to get the first grapheme cluster.

    :param text: UTF-8 encoded bytes
    :param start: starting byte position
    :param end: ending byte position
    :returns: (grapheme_string, next_byte_position)

    Assumes caller provides valid UTF-8 byte boundaries.
    utf-8 )decodenextr   iter_graphemesencodelen)textstartenddecodedgraphemegrapheme_bytess         r   _decode_grapheme_atr'   H   sT     5o$$W-GG**73R8H__W-NUS0000r   c           
        t        |       |z
  }d}d}d}	 t        | t              rQt        | |         }|dkD  rt        | |dz            }|dkD  rt        | |dz            }|dkD  r>t        | |dz            }n,| |   }|dkD  r| |dz      }|dkD  r| |dz      }|dkD  r| |dz      }|d	z  s||dz   fS t        d
      |dz   f}|dk  r|S |dz  dk(  r&|dz  d	k7  r|S |dz  dz  |dz  z  x}	d	k\  r|	|dz   fS |S |dk  r|S |dz  dk(  r9|dz  d	k7  r|S |dz  d	k7  r|S |dz  dz  |dz  dz  z  |dz  z  x}	dk\  r|	|dz   fS |S |dk  r|S |dz  dk(  rL|dz  d	k7  r|S |dz  d	k7  r|S |dz  d	k7  r|S |dz  dz  |dz  dz  z  |dz  dz  z  |dz  z  x}	dk\  r|	|dz   fS |S |S # t        $ r5}t        | d| d|d|      j                  |j                        |d}~ww xY w)zH
    Return (ordinal at pos, next position) for UTF-8 encoded text.
    r      r	      z: text=z, pos=z, lt=N   ?            ?            i               i   )r    
isinstancestrord	Exception
ValueErrorwith_traceback__traceback__)
r!   posltb2b3b4b1eerrorr   s
             r   
decode_onerH   Y   s    
TSB	
B	
B	
BodC T#YBAvcAg'AvcAg'AvcAg'cBAv#']Av#']Av#'] 937{HcAgE	Av	DyD9L9"rDy11Ad:cAg:	Av	DyD9L9L9#dq(89R$YGGAEQcAg:	Av	DyD9L9L9L9#dr(9:rDyQ>NOSUX\S\]]AbiicAg:LK  oA3gdXVC7%vFGVVWXWfWfgmnnos   BF 	G"0GGc                (    t        | |         |dz   fS )z7
    decode_one implementation for unicode strings
    r)   )r;   )r!   is     r   decode_one_unirK      s     tAw<Qr   c                    t        | t              st        |       t        d      |dz
  f}|}|dk\  r6| |   dz  dk7  rt	        | |      \  }}||dz
  fS |dz  }||dz
  k(  r|S |dk\  r6y)z
    Return (ordinal at pos, next position) for UTF-8 encoded text.
    pos is assumed to be on the trailing byte of a utf-8 sequence.
    r,   r)   r   r.   r+   r5   N)r9   bytes	TypeErrorr;   rH   )r!   r@   rG   pr   	_next_poss         r   decode_one_rightrQ      s    
 dE"oHcAgEA
q&7T>T!%dA.LAya!e8O	QA:L q& r   c                &    | dvrt        |       | ay )N>   utf8wider   )r=   r   )encs    r   set_byte_encodingrV      s    
,,oNr   c                     t         S )N)r    r   r   get_byte_encodingrY      s    r   c                    ||kD  rt        ||f      d}|}t        j                  | ||       D ]:  }t        j                  |d      }||z   |kD  r||fc S ||z  }|t	        |      z  }< ||fS )a  
    Calculate the closest position to the screen column pref_col in text
    where start_offs is the offset into text assumed to be screen column 0
    and end_offs is the end of the range to search.

    Iterates by grapheme clusters for emoji ZWJ sequences, flags,
    combining characters, and other multi-codepoint unicode sequences.

    :param text: string
    :param start_offs: starting text position
    :param end_offs: ending text position
    :param pref_col: target column
    :returns: (position, actual_col)
    r   ignorecontrol_codes)r=   r   r   r   r    )r!   
start_offsend_offspref_colcolsr@   r%   grapheme_widths           r   calc_string_text_posrc      s     H*h/00D
C**4
8+DE  xxHD 8+9s8} T>r   c                   ||kD  rt        ||f      t        | t              rt        | |||      S t        | t              st        |       t        dk(  rR| || j                  d      }t        |dt        |      |      \  }}t        |d| j                  d            }||z   |fS ||z   }||k\  r|||z
  fS t        dk(  rt        | ||      dk(  r|dz  }|||z
  fS )a9  
    Calculate the closest position to the screen column pref_col in text
    where start_offs is the offset into text assumed to be screen column 0
    and end_offs is the end of the range to search.

    text may be unicode or a byte string in the target _byte_encoding

    Returns (position, actual_col).
    rS   r   r   NrT   r	   r)   )r=   r9   r:   rc   rM   rN   r   r   r    r   within_double_byte)	r!   r^   r_   r`   r$   str_posra   byte_offsetrJ   s	            r   calc_text_posrh      s    H*h/00$#D*hIIdE"oz(+227;,WaWxP'(7+227;<K'-- 	XAH}J...$6tZ$Kq$P	Qa*nr   c                   ||kD  rd|d|}t        |      t        | t              rt        j                  | || d      S t
        dk(  r*	 t        j                  | || j                  d      d      S ||z
  S # t        $ r)}t        j                  d| t        d	       Y d
}~nd
}~ww xY w|}d}||k  r=t        | |      \  }}t        j                  t        |            x}dkD  r||z  }||k  r=|S )an  
    Return the screen column width of text between start_offs and end_offs.

    text may be unicode or a byte string in the target _byte_encoding

    Some characters are wide (take two columns) and others affect the
    previous character (take zero columns), while others are grouped
    in sequence by "grapheme boundaries" (Emoji, Skin tones, flags, etc).
    zstart_offs=z > end_offs=r[   r\   rS   r   zx`calc_width` with text encoded to bytes can produce incorrect resultsdue to possible offset in the middle of character: r	   r
   Nr   )r=   r9   r:   r   r   r   r   UnicodeDecodeErrorr   r   UnicodeWarningrH   r   )	r!   r^   r_   msgexcrJ   scr   ws	            r   
calc_widthrp     s    H],o$}}T*X6hOO	==j!:!A!A'!JZbcc" j  ! " 	MMFFIUL	 	 (ldA&DAq__SV,,1a (l 	s   (A; ;	B-B((B-c                z   t        | t              r;t        t        j                  | |d             }t        j
                  |d      dk(  S t        | t              st        |       t        dk(  r3t        | |t        |             \  }}t        j
                  |d      dk(  S t        dk(  rt        | ||      dk(  S y)	ac  
    Test if the grapheme cluster at offs within text is wide (2 columns).

    For Unicode strings, extracts the full grapheme cluster starting at offs
    and checks if it renders as wide. This correctly handles multi-codepoint
    graphemes like emoji ZWJ sequences and flags.

    text may be unicode or a byte string in the target _byte_encoding
    Nr[   r\   r	   rS   rT   r)   F)r9   r:   r   r   r   r   rM   rN   r   r'   r    re   )r!   offsr%   _s       r   is_wide_charrt   )  s     $..tDE{;<}}XX>!CCdE"o)$c$i@!}}XX>!CC!$d3q88r   c                   ||k\  rt        ||f      t        | t              rt        j                  | |      S t        | t
              st        |       t        dk(  rW| || j                  d      }t        |      }t        j                  ||      }|d| }|t        |j                  d            z   S t        dk(  rt        | ||dz
        dk(  r|dz
  S |dz
  S )z
    Return the position of the grapheme cluster before end_offs.

    For Unicode strings, handle multi-codepoint, "grapheme clusters",
    to better measure emoji ZWJ, flags, combining characters, skin tones.
    rS   r   NrT   r)   r	   )r=   r9   r:   r   grapheme_boundary_beforerM   rN   r   r   r    r   re   )r!   r^   r_   r$   rf   prev_str_posprefixs          r   move_prev_charry   @  s     X*h/00$//h??dE"oz(+227;g,77I,'Cg 6777$6tZTU$VZ[$[!|a<r   c                V   ||k\  rt        ||f      t        | t              r/t        t	        j
                  | ||             }|t        |      z   S t        | t              st        |       t        dk(  rt        | ||      \  }}|S t        dk(  rt        | ||      dk(  r|dz   S |dz   S )z
    Return the position of the next grapheme cluster after start_offs.

    For Unicode strings, handle multi-codepoint, "grapheme clusters",
    to better measure emoji ZWJ, flags, combining characters, skin tones.
    rS   rT   r)   r	   )r=   r9   r:   r   r   r   r    rM   rN   r   r'   re   )r!   r^   r_   r%   rs   next_poss         r   move_next_charr|   X  s     X*h/00$..tJx/HIJCM))dE"o)$
HE8$6tZ$TXY$YA~>r   c                   t        | t              st        |       | |   }d|cxk  rdk  r)n n&||k(  ry| |dz
     dk\  rt        | ||dz
        dk(  ryy|dk  ry|dz
  }||k\  r| |   dk  rn|dz  }||k\  r||z
  dz  ryy)aj  Return whether pos is within a double-byte encoded character.

    text -- byte string in question
    line_start -- offset of beginning of line (< pos)
    pos -- offset in question

    Return values:
    0 -- not within dbe char, or double_byte_encoding == False
    1 -- pos is on the 1st half of a dbe char
    2 -- pos is on the 2nd half of a dbe char
    @      r   r)      r	   r+   )r9   rM   rN   re   )r!   
line_startr@   vrJ   s        r   re   re   n  s     dE"oS	Aq4*a=D %7j#PQ'%RVW%W4xaA
z/7T>	Q z/
 	a1}r   )r   r:   returnLiteral[0, 1, 2])r   intr   r   )r!   rM   r"   r   r#   r   r   ztuple[str, int])r!   zbytes | strr@   r   r   tuple[int, int])r!   r:   rJ   r   r   r   )r!   rM   r@   r   r   ztuple[int, int] | None)rU   r   r   None)r   r   )
r!   r:   r^   r   r_   r   r`   r   r   r   )
r!   str | bytesr^   r   r_   r   r`   r   r   r   )r!   r   r^   r   r_   r   r   r   )r!   r   rr   r   r   bool)r!   rM   r   r   r@   r   r   r   )
__future__r   retypingr   r   TYPE_CHECKINGtyping_extensionsr   compileSAFE_ASCII_RESAFE_ASCII_BYTES_REr   __annotations__r   r   r'   rH   rK   rQ   rV   rY   rc   rh   rp   rt   ry   r|   re   rX   r   r   <module>r      s   * # 	   	)

;' bjj. 4<1 <""1"@F&<D%!P.0,$r   