o
    � o_C4  ã                   @   s`  d dl mZmZ d dlmZ d dlmZmZmZ d dl	Z	ej
Z
e
jZe
jZe
jZe
jZe
jZe
jZdd„ Zdeƒ vr>eƒ ZnJ dƒ‚dad	ad
d„ Zdd„ Zdd„ Zeeƒ 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#G d&d'„ d'e$ƒZ%d(d)„ Z&d*d+„ Z'd,d-„ Z(d.d/„ Z)G d0d1„ d1e*ƒZ+d2d3„ Z,G d4d5„ d5e-ƒZ.dS )6é    )ÚdivisionÚprint_function)Úescape)ÚbytesÚ	text_typeÚ
text_typesNc                  C   s  dd l } |  ¡ }zfz1z	|  | jd¡ W n
 | jy   Y nw |  ¡ d p%dW W z	|  | j|¡ W S  | jy:   Y S w  tyn } z(|jri|jd  d¡riW Y d }~W z
|  | j|¡ W dS  | jyh   Y dS w ‚ d }~ww z	|  | j|¡ W w  | jy‚   Y w w )Nr   Ú é   zunknown locale)ÚlocaleÚ	getlocaleÚ	setlocaleÚLC_ALLÚErrorÚ
ValueErrorÚargsÚ
startswith)r
   ÚinitialÚe© r   ú,/usr/lib/python3/dist-packages/urwid/util.pyÚdetect_encoding)   s<   ÿÿ÷ÿü€ûÿr   Údetected_encodingz
It worked!Tc                 C   s~   |   ¡ } | dv rt d¡ dan| dv rt d¡ dant d¡ dadaz| r2d	 | ¡ | aW d
S W d
S  ty>   Y d
S w )z~
    Set the byte encoding to assume when processing strings and the
    encoding to use when converting unicode strings.
    )zutf-8Úutf8Úutfr   F)zeuc-jpzeuc-krzeuc-cnzeuc-twÚgb2312ÚgbkÚbig5zcn-gbÚuhcÚeucjpÚeuckrÚeuccnÚeuctwÚcncbÚwideTÚnarrowÚasciir   N)ÚlowerÚstr_utilÚset_byte_encodingÚ_use_dec_specialÚ_target_encodingÚencodeÚLookupError)Úencodingr   r   r   Úset_encodingH   s    




þr.   c                   C   s   t  ¡ 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   r   Úget_encoding_modej   s   r0   c              	   C   s  t r2t| ƒtkr2z|  tj¡} W n! ty1   ttjtj	ƒD ]\}}|  
|tj| tj ¡} qY nw t| ƒtkrI|  
tjtj d¡} t | td¡} t| tƒsPJ ‚tj d¡}tj d¡}|  |¡}t|d tƒsjJ ‚|d  
|tƒ ¡}g }g }|r‡| |¡ | dt|ƒf¡ t|ƒdkr‘||fS |dd… D ]f}	t|	tƒs J ‚t|tƒs§J ‚|	 |d¡}
t|
ƒdkrÑ|
d }t|tƒs¾J ‚| |¡ t|tj d¡t|ƒfƒ q—|
\}}| 
|tƒ ¡}|rí| |¡ t|tjt|ƒfƒ |rý| |¡ t|dt|ƒfƒ q—tƒ  |¡}||fS )z:
    Return (encoded byte string, character set rle).
    r   Úreplacer%   r   Nr	   )r)   Útyper   Ú	translater   ÚDEC_SPECIAL_CHARMAPÚNotImplementedErrorÚzipÚDEC_SPECIAL_CHARSÚALT_DEC_SPECIAL_CHARSr1   ÚSOÚSIÚcodecsr+   r*   Ú
isinstancer   ÚsplitÚappendÚlenÚrle_append_modifyÚDEC_TAGÚjoin)ÚsÚcÚaltr9   r:   ÚsisÚsis0ÚsoutÚcoutÚsnÚslÚsinÚsonÚoutstrr   r   r   Úapply_target_encodings   s^   ÿþþ




€rO   c                   C   s   t ot dkS )zi
    Return True if python is able to convert non-ascii unicode strings
    to the current encoding.
    r%   )r*   r   r   r   r   Úsupports_unicode´   s   rP   c                 C   s~   |}d }}|dkr$t | |||ƒ\}}||k r$d}t | |||d ƒ\}}|| | }	t | |||	ƒ\}
}||	k r9d}||
||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   r	   )Úcalc_text_pos)ÚtextÚ
start_offsÚend_offsÚ	start_colÚend_colÚsposÚpad_leftÚ	pad_rightÚscÚrunÚposr   r   r   Úcalc_trim_text¿   s   ÿr]   c                 C   s®   t | dt| ƒ||ƒ\}}}}t|||ƒ}	t|||ƒ}
|r/t||d ƒ}t|	|dfƒ t|
dƒ |rBt||ƒ}t|	|dfƒ t|
dƒ tƒ  |¡| ||…  tƒ  |¡ |	|
fS )z<
    Return ( trimmed text, trimmed attr, trimmed cs ).
    r   r	   )Nr	   )r]   r?   Ú
rle_subsegÚ
rle_get_atÚrle_prepend_modifyr@   r   Úrjust)rR   ÚattrÚcsrU   rV   rW   ÚeposrX   rY   ÚattrtrÚcstrÚalr   r   r   Útrim_text_attr_csÞ   s$   ÿ



ÿÿrh   c                 C   s>   d}|dk rdS | D ]\}}|| |kr|  S ||7 }q
dS )z-
    Return the attribute at offset pos.
    r   Nr   )Úrler\   ÚxÚar[   r   r   r   r_   ó   s   
r_   c                 C   s„   g }d}| D ]9\}}|r#||kr||8 }||7 }q||7 }||8 }d}||kr* |S || |kr4|| }||7 }|  ||f¡ q|S )z$Return a sub segment of an rle list.r   ©r>   )ri   ÚstartÚendÚlrj   rk   r[   r   r   r   r^     s&   ür^   c                 C   s:   d}| D ]}t |ƒtksJ t| ƒƒ‚|\}}||7 }q|S )z]
    Return the number of characters covered by a run length
    encoded attribute list.
    r   )r2   ÚtupleÚrepr)ri   r[   Úvrk   Úrr   r   r   Úrle_len  s   
rt   c                 C   s`   |\}}| s||fg| dd…< dS | d \}}||kr%||| f| d< dS ||fg| dd…< dS )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   )ri   Úa_rrk   rs   rg   r[   r   r   r   r`   $  s   r`   c                 C   sN   |\}}| r| d d |kr|   ||f¡ dS | d \}}||| f| d< dS )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   Nrl   )ri   ru   rk   rs   ÚlaÚlrr   r   r   r@   6  s   r@   c                 C   s*   |sdS t | |d ƒ | |dd… 7 } dS )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   r	   ©r@   )ri   Úrle2r   r   r   Úrle_join_modifyD  s   r{   c           
      C   sÀ   d }}| r|s
g S | d \}}|d \}}g }|r^|r^t ||ƒ}	t|||f|	fƒ ||	8 }|dkrB|t| ƒk rB| | \}}|d7 }||	8 }|dkrZ|t|ƒk rZ|| \}}|d7 }|r^|s|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.
    r	   r   )Úminr@   r?   )
Úrle1rz   Úi1Úi2Úa1Úr1Úa2Úr2ro   rs   r   r   r   Úrle_productP  s$   

ör„   c                 C   s>   g }g }| D ]\\}}}t |||fƒ t |||fƒ q||fS )z!
    Inverse of rle_product.
    ry   )ri   r}   rz   r€   r‚   rs   r   r   r   Ú
rle_factorn  s   r…   c                   @   s   e Zd ZdS )ÚTagMarkupExceptionN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r†   z  s    r†   c                 C   sF   t | dƒ\}}|d dd…  |¡}|r|d d du r|d= ||fS )z:Return (text string, attribute list) for tagmarkup passed.Nr   rv   )Ú_tagmarkup_recurserB   )ÚtmÚtlrg   rR   r   r   r   Údecompose_tagmarkup|  s
   r�   c                 C   sæ   t | ƒtkr?g }g }| D ].}t||ƒ\}}|r2|d \}}|d \}	}
||	kr2|	||
 f|d< |d= ||7 }||7 }q||fS t | ƒtkr[t| ƒdkrRtd| f ƒ‚| \}}t||ƒS t| ttf ƒsitd|  ƒ‚| g|t| ƒfgfS )zsReturn (text list, attribute list) for tagmarkup passed.

    tm -- tagmarkup
    attr -- current attribute or Nonerv   r   é   z5Tuples must be in the form (attribute, tagmarkup): %rzInvalid markup element: %r)	r2   ÚlistrŠ   rp   r?   r†   r<   r   r   )r‹   rb   ÚrtlÚralÚelementrŒ   rg   Ú	last_attrÚlast_runÚtop_attrÚtop_runr   r   r   rŠ   ˆ  s,   

rŠ   c                 C   s*   t | ƒtkot| ƒdko| d  d¡dkS )Né   r   Úmouse)r2   rp   r?   Úfind©Úevr   r   r   Úis_mouse_event¯  s   *rœ   c                 C   s   |   d¡dkS )NÚpressr   )r™   rš   r   r   r   Úis_mouse_press²  ó   rž   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )Ú	MetaSuperzadding .__superc                    sB   t t| ƒ |||¡ t| d| ƒrtdƒ‚t| d| t | ƒƒ d S )Nz
_%s__superz/Class has same name as one of its super classes)Úsuperr    Ú__init__ÚhasattrÚAttributeErrorÚsetattr)ÚclsÚnameÚbasesÚd©Ú	__class__r   r   r¢   ¹  s   zMetaSuper.__init__)r‡   rˆ   r‰   Ú__doc__r¢   Ú__classcell__r   r   rª   r   r    ·  s    r    c                 C   s0   t | |d  d |d  ƒ}|d d }|| 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
    r	   rŽ   )Úint)ÚvalÚ	val_rangeÚ	out_rangeÚnumÚdemr   r   r   Ú	int_scaleÁ  s   r´   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚStoppingContextz±Context 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.
    c                 C   s
   || _ d S ©N)Ú_wrapped)ÚselfÚwrappedr   r   r   r¢   Û  s   
zStoppingContext.__init__c                 C   s   | S r¶   r   )r¸   r   r   r   Ú	__enter__Þ  s   zStoppingContext.__enter__c                 G   s   | j  ¡  d S r¶   )r·   Ústop)r¸   Úexc_infor   r   r   Ú__exit__á  rŸ   zStoppingContext.__exit__N)r‡   rˆ   r‰   r¬   r¢   rº   r½   r   r   r   r   rµ   Ö  s
    rµ   )/Ú
__future__r   r   Úurwidr   Úurwid.compatr   r   r   r;   r'   rQ   Ú
calc_widthÚis_wide_charÚmove_next_charÚmove_prev_charÚwithin_double_byter   Úlocalsr   r*   r)   r.   r0   rO   rP   r]   rh   r_   r^   rt   r`   r@   r{   r„   r…   Ú	Exceptionr†   r�   rŠ   rœ   rž   r2   r    r´   Úobjectrµ   r   r   r   r   Ú<module>   sP   
"	='
