
    jj;                    :   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 ej                  r#d dlmZmZ d dlmZ d dlmZmZmZ  G d d	e      Zd+d
Zd,dZd e       vr e       Zn ed      dadad-dZd,dZej<                  d.d       Zd/dZ d0dZ! ee       d1dZ"	 	 	 	 	 	 	 	 	 	 	 	 d2dZ#d3dZ$d4dZ%d5dZ&d6dZ'd7dZ(d7dZ)d7dZ*d Z+d Z, G d  d!e-      Z.d" Z/d# Z0d8d$Z1d9d%Z2 G d& d'e3      Z4d:d(Z5 G d) d*ejl                  d*         Z7y);    )annotationsN)suppress)str_util)	GeneratorIterable)TracebackType)LiteralProtocolSelfc                      e Zd ZddZy)CanBeStoppedc                     y N selfs    S/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/util.pystopzCanBeStopped.stop'   s        NreturnNone)__name__
__module____qualname__r   r   r   r   r   r   &   s    #r   r   c           	         t        t        |       r>t        j                  d| dt         dt
         dt        d       t        t        |       S t        |  dt         dt
               )NzDo not import z from .z, import it from 'urwid'.   )
stacklevelz is not defined in )	hasattrr   warningswarn__package__r   DeprecationWarninggetattrAttributeError)names    r   __getattr__r(   *   sc    xTHF;-q
B[\	

 x&&
D6!4[M8*M
NNr   c                    t         j                  dk(  rt        j                         dk(  rydd l} | j	                  d      }|dk7  r|S | j                  | j                        }	 t        | j                        5  | j                  | j                  d       d d d        | j	                  d      t        | j                        5  | j                  | j                  |       d d d        S # 1 sw Y   TxY w# 1 sw Y   S xY w# t        | j                        5  | j                  | j                  |       d d d        w # 1 sw Y   w xY wxY w)Nwin32utf-8r   Fascii )	sysplatformgetdefaultencodinglocalegetpreferredencoding	setlocaleLC_CTYPEr   Error)r1   no_set_localeold_locs      r   detect_encodingr8   5   s    ||w3#9#9#;w#F//6M  v/G7fll# 	2V__b1	2 **51fll# 	7V__g6	7 	7	2 	2
	7 	7Xfll# 	7V__g6	7 	7 	7sH   $D 9C)D C5)C2.D 5C>	E	D=4	E	=EE	detected_encodingzEncoding detection brokenr,   Tc                L   | j                         } | dv rt        j                  d       dan3| dv rt        j                  d       dant        j                  d       dadat        j                  t              5  | rd	j                  |        | ad
d
d
       y
# 1 sw Y   y
xY w)z~
    Set the byte encoding to assume when processing strings and the
    encoding to use when converting unicode strings.
    >   utfutf8r+   r<   F>   cn-gbeuc-cneuc-jpeuc-kreuc-twgbkuhcbig5cncbeuccneucjpeuckreuctwgb2312wideTnarrowr,   r-   N)	lowerr   set_byte_encoding_use_dec_special_target_encoding
contextlibr   LookupErrorencode)encodings    r   set_encodingrU   _   s    
 ~~H ++""6* 	  
" 	""6*""8, 			[	) (IIh'( ( (s   ;BB#c                     t         S )zGet target encoding.rP   r   r   r   get_encodingrX      s    r   c              #  n   K   t         }	 t        |        d t        |       y# t        |       w xY ww)zhInternal helper for encoding specific validation in unittests/doctests.

    Not exported globally.
    N)rP   rU   )encoding_nameold_encodings     r   set_temporary_encodingr\      s-      $L#]#\"\"s   5% 525c                 *    t        j                         S )z
    Get the mode Urwid is using when processing text strings.
    Returns 'narrow' for 8-bit encodings, 'wide' for CJK encodings
    or 'utf8' for UTF-8 encodings.
    )r   get_byte_encodingr   r   r   get_encoding_moder_      s     %%''r   c                j   ddl m} t        r+t        | t              r| j                  |j                        } t        | t              rD| j                  |j                  |j                  z   d      } t        j                  | t        d      } t        | t              st        |       |j                  j                  d      }|j                  j                  d      }| j                  |      }|d   j                  |d      }g }g }|r-|j!                  |       |j!                  dt#        |      f       t#        |      dk(  r||fS |dd D ]  }|j                  |d      }	t#        |	      dk(  r8|	d   }
|j!                  |
       t%        ||j&                  t#        |
      f       [|	\  }
}|j                  |d      }|
r2|j!                  |
       t%        ||j&                  t#        |
      f       |s|j!                  |       t%        |dt#        |      f        dj)                  |      }||fS )	z:
    Return (encoded byte string, character set rle).
    r   )escaper-   replacer,   r   N   )urwid.displayra   rO   
isinstancestr	translateDEC_SPECIAL_CHARMAPrb   SISOcodecsrS   rP   bytes	TypeErrorsplitappendlenrle_append_modifyDEC_TAGjoin)sra   rj   ri   sissis0soutcoutsnslsinsonoutstrs                r   apply_target_encodingr~      s   
 %Jq#.KK223!SIIfii&))+R0MM!-y9al			'	"B			'	"B
''"+Cq6>>"c"DDDDT3t9%&
3x1}Tz!"g 6XXb!_r7a<Q%CKKdV^^SX$>?Skk"c"KKdV^^SX$>?KKdT3s8$45!6$ XXd^F4<r   c                 $    t         xr	 t         dk7  S )zi
    Return True if python is able to convert non-ascii unicode strings
    to the current encoding.
    r,   rW   r   r   r   supports_unicoder      s    
 ; 0G ;;r   c                    |}dx}}|dkD  r@t        j                  | |||      \  }}||k  r d}t        j                  | |||dz         \  }}||z
  |z
  }	t        j                  | |||	      \  }
}||	k  rd}||
||fS )a	  
    Calculate the result of trimming text.
    start_offs -- offset into text to treat as screen column 0
    end_offs -- offset into text to treat as the end of the line
    start_col -- screen column to trim at the left
    end_col -- screen column to trim at the right

    Returns (start, end, pad_left, pad_right), where:
    start -- resulting start offset
    end -- resulting end offset
    pad_left -- 0 for no pad or 1 for one space to be added
    pad_right -- 0 for no pad or 1 for one space to be added
    r   rc   )r   calc_text_pos)text
start_offsend_offs	start_colend_colspospad_left	pad_rightscrunposs              r   calc_trim_textr      s    ( DHy1}))$h	Jb	>H--dJ)VW-XHD"
I

(C$$T43?GC	Cx	#x++r   c                n   t        | dt        |       ||      \  }}}}t        |||      }	t        |||      }
|r)t        ||dz
        }t	        |	|df       t	        |
d       |r&t        ||      }t        |	|df       t        |
d       dj                  |      | || z   dj                  |      z   |	|
fS )z<
    Return ( trimmed text, trimmed attr, trimmed cs ).
    r   rc   )Nrc   r   )r   rp   
rle_subseg
rle_get_atrle_prepend_modifyrq   rjust)r   attrcsr   r   r   eposr   r   attrtrcstrals               r   trim_text_attr_csr     s     '5T1c$iT[&\#D$)dD)Fb$%DdQh'6B7+4+d#&2q'*$	*IIh$tD/1CIIi4HH&RVWWr   c                J    d}|dk  ry| D ]  \  }}||z   |kD  r|c S ||z  } y)z-
    Return the attribute at offset pos.
    r   Nr   )rler   xar   s        r   r   r   !  sD     	
A
Qw 3s7S=H	S r   c                    g }d}| D ]P  \  }}|r||k\  r||z  }||z  }||z  }||z  }d}||k\  r |S ||z   |kD  r||z
  }||z  }|j                  ||f       R |S )z#Return a sub segment of a rle list.r   ro   )r   startendsub_segmentr   r   r   s          r   r   r   /  s    K	A %3|SJA5LCE8
 	 s7S='C	SAs8$% r   c                b    d}| D ]'  }t        |t              st        |       |\  }}||z  }) |S )z]
    Return the number of characters covered by a run length
    encoded attribute list.
    r   )re   tuplerm   )r   r   v_ars        r   rle_lenr   E  sE     C !U#C. Aq	
 Jr   c                d    |\  }}| s	||fg| dd y| d   \  }}||k(  r|||z   f| d<   y||fg| dd y)z
    Append (a, r) (unpacked from *a_r*) to BEGINNING of rle.
    Merge with first run when possible

    MODIFIES rle parameter contents. Returns None.
    Nr   r   )r   a_rr   r   r   r   s         r   r   r   T  sU     DAqa&Aa&C7q\CFAxC!Hr   c                t    |\  }}| r| d   d   |k7  r| j                  ||f       y| d   \  }}|||z   f| d<   y)z
    Append (a, r) (unpacked from *a_r*) to the rle list rle.
    Merge with last run when possible.

    MODIFIES rle parameter contents. Returns None.
    r   Nr   )r   r   r   r   _lalrs         r   rq   rq   f  sO     DAq#b'!*/

Aq6"gGC"q&kCGr   c                8    |syt        | |d          | |dd z  } y)z
    Append attribute list rle2 to rle.
    Merge last run of rle with first run of rle2 when possible.

    MODIFIES attr parameter contents. Returns None.
    Nr   rc   rq   )r   rle2s     r   rle_join_modifyr   u  s'     c47#48OCr   c                   dx}}| r|sg S | d   \  }}|d   \  }}g }|rm|rkt        ||      }	t        |||f|	f       ||	z  }|dk(  r|t        |       k  r| |   \  }}|dz  }||	z  }|dk(  r|t        |      k  r||   \  }}|dz  }|r|rk|S )a  
    Merge the runs of rle1 and rle2 like this:
    eg.
    rle1 = [ ("a", 10), ("b", 5) ]
    rle2 = [ ("Q", 5), ("P", 10) ]
    rle_product: [ (("a","Q"), 5), (("a","P"), 5), (("b","P"), 5) ]

    rle1 and rle2 are assumed to cover the same total run.
    rc   r   )minrq   rp   )
rle1r   i1i2a1r1a2r2resultr   s
             r   rle_productr     s     KBt	!WFB!WFBF
BK&B8Q-0
a7rCI~"XFB!GB
a7rCI~"XFB!GB  Mr   c                d    g }g }| D ]$  \  \  }}}t        |||f       t        |||f       & ||fS )z!
    Inverse of rle_product.
    r   )r   r   r   r   r   r   s         r   
rle_factorr     sM     DD )R!$Q($Q() :r   c                      e Zd Zy)TagMarkupExceptionN)r   r   r   r   r   r   r   r     s    r   r   c                z    t        | d      \  }}|r|d   dd j                  |      }nd}|r|d   d   |d= ||fS )z:Return (text string, attribute list) for tagmarkup passed.Nr   r-   r   )_tagmarkup_recursers   )tmtlr   r   s       r   decompose_tagmarkupr     sT      D)FB	!uRay~~b!	bfQirF8Or   c                   t        | t              rLg }g }| D ]?  }t        ||      \  }}|r"|d   \  }}|d   \  }	}
||	k(  r|	||
z   f|d<   |d= ||z  }||z  }A ||fS t        | t              r-t	        |       dk7  rt        d|       | \  }}t        ||      S t        | t        t        f      st        d|       | g|t	        |       fgfS )zsReturn (text list, attribute list) for tagmarkup passed.

    tm -- tagmarkup
    attr -- current attribute or Noner   r      z3Tuples must be in the form (attribute, tagmarkup): zInvalid markup element: )re   listr   r   rp   r   rf   rl   )r   r   rtlralelementr   r   	last_attrlast_runtop_attrtop_runs              r   r   r     s    "d 
	G'6FB&)"g#	8$&qE!'('G);<CG12IC2IC
	 Cx"er7a<$'Z[]Z`%abbg!'400b3,' #;B6!BCC 44R/"""r   c                T    t        | t              xr t        |       dk(  xr d| d   v S )N   mouser   )re   r   rp   evs    r   is_mouse_eventr     s*    b% FSW\FgA6FFr   c                
    d| v S )Npressr   r   s    r   is_mouse_pressr     s    b=r   c                      e Zd ZdZdZy)	MetaSuperzDeprecated metaclass.

    Present only for code compatibility, all logic has been removed.
    Please move to the last position in the class bases to allow future changes.
    r   N)r   r   r   __doc__	__slots__r   r   r   r   r     s     Ir   r   c                P    t        | |dz
  z  dz  |dz
  z         }|dz
  dz  }||z  S )aW  
    Scale val in the range [0, val_range-1] to an integer in the range
    [0, out_range-1].  This implementation uses the "round-half-up" rounding
    method.

    >>> "%x" % int_scale(0x7, 0x10, 0x10000)
    '7777'
    >>> "%x" % int_scale(0x5F, 0x100, 0x10)
    '6'
    >>> int_scale(2, 6, 101)
    40
    >>> int_scale(1, 3, 4)
    2
    rc   r   )int)val	val_range	out_rangenumdems        r   	int_scaler     s=     cY]#a'9q=9
:Cq=A
C#:r   c                  <    e Zd ZdZdZddZddZ	 	 	 	 	 	 	 	 d	dZy)
StoppingContextzContext manager that calls ``stop`` on a given object on exit.  Used to
    make the ``start`` method on `MainLoop` and `BaseScreen` optionally act as
    context managers.
    _wrappedc                    || _         y r   r   )r   wrappeds     r   __init__zStoppingContext.__init__  s	    r   c                    | S r   r   r   s    r   	__enter__zStoppingContext.__enter__  s    r   c                8    | j                   j                          y r   )r   r   )r   exc_typeexc_valexc_tbs       r   __exit__zStoppingContext.__exit__  s     	r   N)r   r   r   r   )r   r   )r   ztype[BaseException] | Noner   zBaseException | Noner   zTracebackType | Noner   r   )r   r   r   r   r   r   r   r   r   r   r   r   r     sC    
 I , & %	
 
r   r   )r'   rf   r   z
typing.Any)r   rf   )rT   rf   r   r   )rZ   rf   r   zGenerator[None])r   z!Literal['wide', 'narrow', 'utf8'])rt   str | bytesr   z8tuple[bytes, list[tuple[Literal['U', '0'] | None, int]]])r   bool)r   r   r   r   r   r   r   r   r   r   r   ztuple[int, int, int, int])r   rl   r   r   r   r   )r   r   )r   r   r   r   )r   z Iterable[tuple[typing.Any, int]]r   r   r   )r   z&tuple[str, int, int, int] | typing.Anyr   r   )r   rf   r   r   )r   r   r   r   r   r   r   r   )8
__future__r   rk   rQ   r.   typingr!   r   urwidr   TYPE_CHECKINGcollections.abcr   r   typesr   typing_extensionsr	   r
   r   r   r(   r8   localsr9   RuntimeErrorrP   rO   rU   rX   contextmanagerr\   r_   r~   r   r   r   r   r   r   r   rq   r   r   r   	Exceptionr   r   r   r   r   typer   r   AbstractContextManagerr   r   r   r   <module>r     sp  * #   
    	3#99$x $O7B fh&')
2
33  *(Z
 
# 
#(3p  <,
,, , 	,
 , ,DX&, $
>		 	 ##LG *j778IJ r   