
    ;OOf'                     p   d dl mZmZ d dl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mZ d dlmZ  G d de      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Z e       Z G d de      Z G d de      Zd Zd Zd Zd Zd%dZ d Z!d Z"d Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z*d# Z+d$ Z,y)&    )divisionprint_functionN)rle_lenrle_append_modifyrle_join_modifyrle_product
calc_widthcalc_text_posapply_target_encodingtrim_text_attr_cs)	trim_lineLayoutSegment)bytesc                       e Zd ZdZi Zi Zi ZdZdZdZ	d Z
 ee
      Z
d Z ee      Zd Z ee      Zd Z ee      Zd Z ee      Zy)	CanvasCachea`  
    Cache for rendered canvases.  Automatically populated and
    accessed by Widget render() MetaClass magic, cleared by
    Widget._invalidate().

    Stores weakrefs to the canvas objects, so an external class
    must maintain a reference for this cache to be effective.
    At present the Screen classes store the last topmost canvas
    after redrawing the screen, keeping the canvases from being
    garbage collected.

    _widgets[widget] = {(wcls, size, focus): weakref.ref(canvas), ...}
    _refs[weakref.ref(canvas)] = (widget, wcls, size, focus)
    _deps[widget} = [dependent_widget, ...]
    r   c                   	 |j                   sy|j                  sJ d       |j                  \  }}}	fd	t        |dd      }|t        |d      r 	|      }|rI|D ]  }|| j                  vs y |D ]-  }| j
                  j                  |g       j                  |       / t        j                  || j                        }||||f| j                  |<   || j                  j                  |i       |||f<   y)z
        Store a weakref to canvas in the cache.

        wcls -- widget class that contains render() function
        canvas -- rendered canvas with widget_info (widget, size, focus)
        Nz&Can't store canvas without widget_infoc                     g }| j                   D ]V  \  }}}}|j                  r|j                  |j                  d          3t        |d      s@|j	                   |             X |S )za
            Collect all child widgets for determining who we
            depend on.
            r   children)r   widget_infoappendhasattrextend)canvdependsxycposwalk_dependss         M/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/urwid/canvas.pyr   z'CanvasCache.store.<locals>.walk_dependsC   sb     G $ 41a==NN1==#34Q
+NN<?3	4
 N    
depends_onr   )	cacheabler   getattrr   _widgets_deps
setdefaultr   weakrefrefcleanup_refs)
clswclscanvaswidgetsizefocusr"   wr)   r   s
            @r    storezCanvasCache.store7   s    !!K#KK!$00e	  V\48
'&*"=%f-J CLL(   :		$$Qr*11&9: kk&#++. $e4		#CF+T4,?@r!   c                     | xj                   dz  c_         | j                  j                  |d      }|sy|j                  |||fd      }|sy |       }|r| xj                  dz  c_        |S )z
        Return the cached canvas or None.

        widget -- widget object requested
        wcls -- widget class that contains render() function
        size, focus -- render() parameters
           N)fetchesr%   gethits)r,   r/   r-   r0   r1   sizesr)   r   s           r    fetchzCanvasCache.fetchb   sg     	q  .iitU+T2uHHMHr!   c                 |   	 | j                   |   j                         D ]  }	 | j                  |=  | j                   |= || j                  vry| j                  j                  |g       }	 | j                  |= |D ]  }| j                  |        y# t        $ r Y }w xY w# t        $ r Y lw xY w# t        $ r Y Bw xY w)z8
        Remove all canvases cached for widget.
        N)r%   valuesr+   KeyErrorr&   r7   
invalidate)r,   r/   r)   
dependantsr2   s        r    r>   zCanvasCache.invalidatex   s    	||F+224 		#
 V$ "YY]]62.
			&!  	ANN1	     		  		sE    B  BB  +B/ 	BB  BB   	B,+B,/	B;:B;c                 X   | xj                   dz  c_         | j                  j                  |d       }| j                  |= |sy |\  }}}}| j                  j                  |d       }|sy 	 ||||f= |s	 | j                  |= | j
                  |= y y # t        $ r Y *w xY w# t        $ r Y y w xY wNr5   )cleanupsr+   r7   r%   r=   r&   )r,   r)   r2   r/   r-   r0   r1   r9   s           r    r*   zCanvasCache.cleanup   s    IIMM#t$IIcN$%!dE  .	tT5)* LL(IIf%   		  s$   )B 2B 	BB	B)(B)c                 .    i | _         i | _        i | _        y)z"
        Empty the cache.
        N)r%   r+   r&   )r,   s    r    clearzCanvasCache.clear   s     		r!   N)__name__
__module____qualname____doc__r%   r+   r&   r8   r6   rB   r3   classmethodr:   r>   r*   rD    r!   r    r   r       s~     HEEDGH(GR E( E, Z(J* '"G Er!   r   c                       e Zd Zy)CanvasErrorNrE   rF   rG   rJ   r!   r    rL   rL      s    r!   rL   c                       e Zd ZdZdZ ed      Z ed      Z ed      ZddZ	d Z
d	 Z ee      Zdd
Zd Z eee      Z eee      Z eee      Z	 	 ddZd Zd Zd Zd Zd Z eee      Zd Zd Zd Zy)Canvasz!
    base class for canvases
    TzdThis canvas has been finalized. Use CompositeCanvas to wrap this canvas if you need to make changes.zcThe old Canvas class is now called TextCanvas. Canvas is now the base class for all canvas classes.zThe internal representation of canvases is no longer stored as .text, .attr, and .cs lists, please see the TextCanvas class for the new representation of canvas content.Nc                 J    || j                   d| _        i | _        i | _        y)z
        value1, value2, value3 -- if not None, raise a helpful error:
            the old Canvas class is now called TextCanvas.
        N)_renamed_error_widget_infocoords	shortcuts)selfvalue1value2value3s       r    __init__zCanvas.__init__   s,    
 %%% r!   c                 H    | j                   r| j                  |||f| _        y)a  
        Mark this canvas as finalized (should not be any future
        changes to its content). This is required before caching
        the canvas.  This happens automatically after a widget's
        'render call returns the canvas thanks to some metaclass
        magic.

        widget -- widget that rendered this canvas
        size -- size parameter passed to widget's render method
        focus -- focus parameter passed to widget's render method
        N)r   _finalized_errorrR   )rU   r/   r0   r1   s       r    finalizezCanvas.finalize   s(     '''"D%/r!   c                     | j                   S N)rR   rU   s    r    _get_widget_infozCanvas._get_widget_info   s       r!   c                     | j                   r^   )_old_repr_error)rU   vals     r    _raise_old_repr_errorzCanvas._raise_old_repr_error   s    """r!   c                     | j                         D cg c]1  }t               j                  |D cg c]  \  }}}|
 c}}}      3 c}}}}S c c}}}w c c}}}}w )zg
        Return the text content of the canvas as a list of strings,
        one for each row.
        )contentr   join)rU   rowattrcstexts        r    _text_contentzCanvas._text_content   sR     ||~' ' 3??'7b$d?@ ' 	'? 's   A
AA
A
c                     t               r^   NotImplementedError)rU   	trim_lefttrim_topcolsrowsri   s         r    rf   zCanvas.content   s    !##r!   c                     t               r^   rn   r_   s    r    rr   zCanvas.cols       !##r!   c                     t               r^   rn   r_   s    r    rs   zCanvas.rows   ru   r!   c                     t               r^   rn   r_   s    r    content_deltazCanvas.content_delta   ru   r!   c                 J    | j                   j                  dd       }|sy |d d S )Ncursor   rS   r7   rU   r   s     r    
get_cursorzCanvas.get_cursor  s'    KKOOHd+!ur!   c                     | j                   r| j                  r| j                  |	 | j                  d= y |dz   | j                  d<   y # t        $ r Y y w xY w)Nrz   r^   )r   r#   r[   rS   r=   r}   s     r    
set_cursorzCanvas.set_cursor  sa    '''9KK)  !GH  s   A	 		AAc                 D    | j                   j                  dd       }|sy |S )Npop upr|   r}   s     r    
get_pop_upzCanvas.get_pop_up  s!    KKOOHd+r!   c                 v    | j                   r| j                  r| j                  |||||ff| j                  d<   y)a  
        This method adds pop-up information to the canvas.  This information
        is intercepted by a PopUpTarget widget higher in the chain to
        display a pop-up at the given (left, top) position relative to the
        current canvas.

        :param w: widget to use for the pop-up
        :type w: widget
        :param left: x position for left edge of pop-up >= 0
        :type left: int
        :param top: y position for top edge of pop-up >= 0
        :type top: int
        :param overlay_width: width of overlay in screen columns > 0
        :type overlay_width: int
        :param overlay_height: height of overlay in screen rows > 0
        :type overlay_height: int
        r   N)r   r#   r[   rS   )rU   r2   lefttopoverlay_widthoverlay_heights         r    
set_pop_upzCanvas.set_pop_up  s@    $ '''!%s}n-. !/Hr!   c                 r    i }| j                   j                         D ]  \  }\  }}}||z   ||z   |f||<    |S )z4
        Return coords shifted by (dx, dy).
        )rS   items)rU   dxdydnamer   r   datas           r    translate_coordszCanvas.translate_coords/  sM     "&++"3"3"5 	)D,1atQrT4(AdG	)r!   )NNNr^   r   r   NNN)rE   rF   rG   rH   r#   rL   r[   rQ   rb   rY   r\   r`   propertyr   rd   rl   rk   ri   rj   rf   rr   rs   rx   r~   r   rz   r   r   r   rJ   r!   r    rO   rO      s     I" $$ % ! " N " #, -O
	0 !+,K#' M#89D)+@AD	')>	?B?C$$$$
	, j*-F
/0r!   rO   c                   @    e Zd ZdZ	 	 d	dZd Zd Zd Z	 	 d
dZd Z	y)
TextCanvasz8
    class for storing rendered text and attributes
    Nc           
      <   t         j                  |        |dk(  rg }|rVg }|D ]N  }t        |      t        k7  rt	        dt        |            |j                  t        |dt        |                   P n#t        |      t        k(  sJ |gt        |      z  }||rt        |      }nd}|dk(  r!t        t        |            D 	cg c]  }	g  }}	|dk(  r!t        t        |            D 	cg c]  }	g  }}	t        t        |            D ]  }
||
   }||kD  rt	        d|d|d|      ||k  r%||
   t               j                  ||z
        z   ||
<   t        ||
         t        ||
         z
  }|dk  rt	        d||
   d||
         |rt        ||
   d|f       t        ||
         t        ||
         z
  }|dk  rt	        d||
   d||
         |st        ||
   d|f        || _        || _        || _        || _        || _        yc c}	w c c}	w )a[  
        text -- list of strings, one for each line
        attr -- list of run length encoded attributes for text
        cs -- list of run length encoded character set for text
        cursor -- (x,y) of cursor or None
        maxcol -- screen columns taken by this canvas
        check_width -- check and fix width of all lines in text
        NzBCanvas text must be plain strings encoded in the screen's encodingr   z0Canvas text is wider than the maxcol specified 

zAttribute extends beyond text 
z#Character Set extends beyond text 
)rO   rY   typer   rL   reprr   r	   lenintmaxrangerjustr   r   _attr_csrz   _text_maxcol)rU   rk   ri   rj   rz   maxcolcheck_widthwidthstr   ir2   a_gapcs_gaps                 r    rY   zTextCanvas.__init__>  s)    	4<DF ;7e#%&jlpqulvwwz1aQ8:;
 <3&&&XD	)F>V4< %c$i 011B1D1:#CI./"/B/ s4y! 	:Aq	A6z!aghnos"tuu6zq'EGMM&($;;QQL7DG#55Eqy!dSTgVZ[\V]"^``!47T5M:a\GRU$44Fz!RVWXRYZ\]^Z_"`bb!2a54.9!	:$ 

7 2/s   	H+	Hc                 T    t        | j                        }t        |t              sJ |S )z)Return the number of rows in this canvas.)r   r   
isinstancer   )rU   rs   s     r    rs   zTextCanvas.rows{  s$    4::$$$$r!   c                     | j                   S )z.Return the screen column width of this canvas.)r   r_   s    r    rr   zTextCanvas.cols  s    ||r!   c                 N    | j                   r| j                   \  }}||z   ||z   fS y)zb
        Return cursor coords shifted by (dx, dy), or None if there
        is no cursor.
        N)rz   )rU   r   r   r   r   s        r    translated_coordszTextCanvas.translated_coords  s-    
 ;;;;DAqR42:r!   c           
   #     K   | j                         | j                         }}|s||z
  }|s||z
  }|dk\  r||k  sJ |dkD  r||z   |k  sJ |dk\  r||k  sJ |dkD  r||z   |k  sJ |s||k  r>t        | j                  |||z    | j                  |||z    | j
                  |||z          }n+t        | j                  | j                  | j
                        }|D ]x  \  }	}
}|s|| j                  k  rt        |	|
||||z         \  }	}
}t        |
|      }d}g }|D ]2  \  \  }}}|r	||v r||   }|j                  |||	|||z    f       ||z  }4 | z yw)z
        Return the canvas content as a list of rows where each row
        is a list of (attr, cs, text) tuples.

        trim_left, trim_top, cols, rows may be set by
        CompositeCanvas when rendering a partially obscured
        canvas.
        r   N)
rr   rs   zipr   r   r   r   r   r   r   )rU   rp   rq   rr   rs   attr_mapr   maxrowtext_attr_csrk   a_rowcs_rowattr_csr   rh   arj   runs                     r    rf   zTextCanvas.content  s     diikI%DH$DA~)f"444axI,666!|6 111axHtOv555tf}

8HTM2

8HTM2(4-02L
 tzz4::txx@L#/ 	D%D4<</&7%$'&#eV "%0GAC ' BX A

Ar4!C%=12S	
 I	s   EEc                 n    || u r"| j                         g| j                         z  S | j                         S )z
        Return the differences between other and this canvas.

        If other is the same object as self this will return no
        differences, otherwise this is the same as calling
        content().
        rr   rs   rf   rU   others     r    rx   zTextCanvas.content_delta  s0     D=IIK=,,||~r!   )NNNNNTr   )
rE   rF   rG   rH   rY   rs   rr   r   rf   rx   rJ   r!   r    r   r   :  s9     15.2;z @D*Z
r!   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)BlankCanvaszv
    a canvas with nothing on it, only works as part of a composite canvas
    since it doesn't know its own size
    c                 0    t         j                  | d        y r^   )rO   rY   r_   s    r    rY   zBlankCanvas.__init__  s    d#r!   c              #      K   d}|r	d|v r|d   }|dt               j                  |      fg}t        |      D ]  }|  yw)zH
        return (cols, rows) of spaces with default attributes.
        N)r   r   r   	rU   rp   rq   rr   rs   ri   def_attrliner   s	            r    rf   zBlankCanvas.content  sQ      DDLDzH4t!456t 	AJ	s   A Ac                     t        d      Nz&BlankCanvas doesn't know its own size!rn   r_   s    r    rr   zBlankCanvas.cols      !"JKKr!   c                     t        d      r   rn   r_   s    r    rs   zBlankCanvas.rows  r   r!   c                     t        d      r   rn   r_   s    r    rx   zBlankCanvas.content_delta  r   r!   N)	rE   rF   rG   rH   rY   rf   rr   rs   rx   rJ   r!   r    r   r     s#    $	LLLr!   r   c                   4    e Zd ZdZd Zd Zd Z	 	 ddZd Zy)	SolidCanvasz=
    A canvas filled completely with a single character.
    c                     t         j                  |        t        |dt        |      d      \  }}|dk(  s
J d|z         t	        |d |       \  | _        }|d   d   | _        ||f| _        d | _        y )Nr   r5   zInvalid fill_char: %r)	rO   rY   r
   r   r   r   r   r0   rz   )rU   	fill_charrr   rs   endcolrj   s          r    rY   zSolidCanvas.__init__  st     As9~qASax<09<<x.y#?
Ba58$J	r!   c                      | j                   d   S )Nr   r0   r_   s    r    rr   zSolidCanvas.cols      yy|r!   c                      | j                   d   S rA   r   r_   s    r    rs   zSolidCanvas.rows  r   r!   Nc              #      K   || j                   d   }|| j                   d   }d }|r	d |v r|d    }|| j                  | j                  |z  fg}t        |      D ]  }|  y w)Nr   r5   )r0   r   r   r   r   s	            r    rf   zSolidCanvas.content  st     <99Q<D<99Q<DDDLDzH488TZZ_56t 	AJ	s   A"A$c                 n    || u r"| j                         g| j                         z  S | j                         S )G
        Return the differences between other and this canvas.
        r   r   s     r    rx   zSolidCanvas.content_delta
  s0     D=IIK=,,||~r!   r   )	rE   rF   rG   rH   rY   rr   rs   rf   rx   rJ   r!   r    r   r     s*     @Dr!   r   c                   b    e Zd ZdZddZd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zd Zd Zd Zy)CompositeCanvasz5
    class for storing a combination of canvases
    Nc                    t         j                  |        |g | _        g | _        yt	        |d      r|j                  | _        n;|j                         dd|j                         |j                         d|fgfg| _        dd|dfg| _        | j                  j                  |j                         |j                  D ]  }d| j                  |<    y)z
        canv -- a Canvas object to wrap this CompositeCanvas around.

        if canv is a CompositeCanvas, make a copy of its contents
        Nshardsr   wrap)
rO   rY   r   r   r   rs   rr   rS   updaterT   )rU   r   shortcuts      r    rY   zCompositeCanvas.__init__  s    " 	<DKDMtX&"kk $		499;		$ .!  " #  D$/0DMKKt{{+ NN 2+1x(2r!   c                    | j                   D ]  \  }}	 t        |t              sJ  t	        | j                   D cg c]  \  }}|	 c}}      }t        |t              sJ |S # t        $ r t        ||      w xY wc c}}w r^   )r   r   r   AssertionErrorsum)rU   rcvrs   s       r    rs   zCompositeCanvas.rows;  s    KK 	,DAb,!!S)))	,
 $++.$!BA./$$$$	 " ,$Q++,.s   A$A=
$A:c                     | j                   syt        | j                   d   d   D cg c]  }|d   	 c}      }t        |t              sJ |S c c}w )Nr   r5   r{   )r   r   r   r   )rU   r   rr   s      r    rr   zCompositeCanvas.colsE  sL    {{DKKN1$56bBqE67$$$$ 7s   Ac              #      K   g }| j                   D ]:  \  }}t        ||      }t        |      D ]  }t        |        t	        ||      }< yw)zz
        Return the canvas content as a list of rows where each row
        is a list of (attr, cs, text) tuples.
        N)r   
shard_bodyr   shard_body_rowshard_body_tail)rU   
shard_tailnum_rowscviewssbodyr   s         r    rf   zCompositeCanvas.contentM  s_     
 
 $ 		:Hfvz2E 8_ ,$U++, )59J		:s   AAc              #   d  K   t        |d      s| j                         D ]  }|  yg }t        | j                  |j                        D ]a  \  }}t	        ||      }g }t        |      D ]4  }t        |      dk7  st        |d         t        k7  rt        |      }| 6 t        ||      }c yw)r   r   Nr5   r   )r   rf   shards_deltar   r   r   r   r   r   r   r   )rU   r   rh   r   r   r   r   r   s           r    rx   zCompositeCanvas.content_delta`  s      uh'||~ 	
 ,U\\!+ 	:Hf vz2E C8_  s8q=DQLC$7(/C	 )59J	:s   B.B0c                 j   |dk\  s
J d|z         || j                         k  sJ d|| j                         fz         | j                  r| j                  |rt        | j                  |      | _        |dk(  rg | _        n|t        | j                  |      | _        | j                  d|       | _        y)zTrim lines from the top and/or bottom of canvas.

        top -- number of lines to remove from top
        count -- number of lines to keep, or None for all the rest
        r   invalid trim amount %d!cannot trim %d lines from %d!N)rs   r   r[   shards_trim_topr   shards_trim_rowsr   rS   )rU   r   counts      r    trimzCompositeCanvas.trim|  s     ax62366xTYY[  	"AC # 	 ''')$++s;DKA:DK*4;;>DK++At4r!   c                    |dkD  s
J d|z         || j                         k  sJ d|| j                         fz         | j                  r| j                  t        | j                  | j                         |z
        | _        y)ziTrim lines from the bottom of the canvas.

        end -- number of lines to remove from the end
        r   r   r   N)rs   r   r[   r   r   )rU   r   s     r    trim_endzCompositeCanvas.trim_end  s    
 Qw51#55wdiik! 	#BD $ 	!'''&t{{DIIK#4EFr!   c                    | j                   r| j                  | j                  }|dk  s|dk  r;t        d|       }| j	                         |z
  t        d|       z
  }t        |||      }| j                         }|dkD  s|dkD  rN|d   \  }}|dkD  rdd||dt        fg|z   }	n|dd }	|dkD  r|	j                  dd||dt        f       ||	fg|dd z   }| j                  |d      | _
        || _        y)z
        Pad or trim this canvas on the left and right

        values > 0 indicate screen columns to pad
        values < 0 indicate screen columns to trim
        r   Nr5   )r   r[   r   r   rr   shards_trim_sidesrs   blank_canvasr   r   rS   )
rU   r   rightr   rp   rr   rs   top_rows
top_cviewsnew_top_cviewss
             r    pad_trim_left_rightz#CompositeCanvas.pad_trim_left_right  s    '''!8uqyAuI99;*SUF^;D&vy$?Fyy{!8uqy#)!9 Hjax$tD67  ",Aqy%%qtD6801F12J>F++D!4r!   c           	         | j                   r| j                  | j                  }|dk  s|dk  r@t        d|       }| j	                         |z
  t        d|       z
  }| j                  ||       | j                         }|dkD  r9|dd||dt        fgfg| j                  z   | _        | j                  d|      | _	        |dkD  rK|| j                  u r| j                  dd | _        | j                  j                  |dd||dt        fgf       yy)z@
        Pad or trim this canvas on the top and bottom.
        r   N)r   r[   r   r   rs   r   rr   r   r   rS   r   )rU   r   bottomorig_shardsrq   rs   rr   s          r    pad_trim_top_bottomz#CompositeCanvas.pad_trim_top_bottom  s    '''kk7fqj1sd|H99;)CF7O;DIIh%yy{7Ad3tL124 5DK //37DKA:dkk)"kk!nKKAd6$|45 7 8 r!   c                 r   | j                   r| j                  |j                         }|j                         }| j                         |z
  |z
  }| j                         |z
  |z
  }|dk\  s%J dt	        |j                         |||f      z          |dk\  s%J dt	        |j                         |||f      z          | j
                  }g }	| j
                  }
g }|rt        ||      }
t        ||      }	|rt        |
|      }t        |
|      }
g }g }|dkD  rt        |
d|      g}|dkD  rt        |
t        d||z         |      g}| j                         sg }n-|s|rt        ||j
                  gz   |z         }n|j
                  }|	|z   |z   | _        | j                  j                  |j                  ||             y)zOverlay other onto this canvas.r   z,top canvas of overlay not the size expected!N)r   r[   rr   rs   r   r   r   r   r   r   shards_joinrS   r   r   )rU   r   r   r   widthheightr   r   r   
top_shardsside_shardsbottom_shardsleft_shardsright_shardsmiddle_shardss                  r    overlayzCompositeCanvas.overlay  s   '''

		d"U*s"V+zqIDRWR\R\R^_cdijoQpLqqqz{sJTSXS]S]S_`cdjkqRrMsss{
kk)&#6K)&#6J+K@M*;?K!8,[!TBCK19-kAte|$e- .L yy{MU')!-). /M "LLM =0=@511$<=r!   c                 *    | j                  d|i       y)z
        Apply attribute a to all areas of this canvas with default
        attribute currently set to None, leaving other attributes
        intact.N)fill_attr_apply)rU   r   s     r    	fill_attrzCompositeCanvas.fill_attr  s    
 	d1X&r!   c                    | j                   r| j                  g }| j                  D ]  \  }}g }|D ]  }|d   |j                  |dd |fz   |dd z          't	        |      }|j                  |d   j                         D 	cg c]  \  }}	||j                  |	|	      f c}	}       |j                  |dd |fz   |dd z           |j                  ||f        || _        yc c}	}w )z
        Apply an attribute-mapping dictionary to the canvas.

        mapping -- dictionary of original-attribute:new-attribute items
           N   )r   r[   r   r   dictr   r   r7   )
rU   mappingr   r   original_cviews
new_cviewsr   combinedkvs
             r    r  zCompositeCanvas.fill_attr_apply  s    ''')- 	2%HoJ% 
.a5=%%b!f 
'#%'V', -  $G}HOO:<Q%++-%I361QGKK1-.%I J%%b!f!'$&(f'- .
. MM8Z01	2 %Is   C'c                 B    | j                   r| j                  || _        y)z
        Explicitly specify the list of widgets that this canvas
        depends on.  If any of these widgets change this canvas
        will have to be updated.
        N)r   r[   r"   )rU   widget_lists     r    set_dependszCompositeCanvas.set_depends,  s      '''%r!   r^   )rE   rF   rG   rH   rY   rs   rr   rf   rx   r   r   r   r  r  r  r  r  rJ   r!   r    r   r     sL     2D:&:85.GD86*>Z'4	&r!   r   c                     g }| D ]]  \  }}}|r|j                  t        |             $|r$t        |d         t        k(  r|d   |d   z   |d<   J|j	                  |d          _ |S )zx
    Return one row, advancing the iterators in sbody.

    ** MODIFIES sbody by calling next() on its iterators **
    r{   )r   nextr   r   r   )r   rh   	done_rowscontent_itercviews        r    r   r   8  st     C*/ %&	<JJtL)* tCG}+b'E!H,B

58$% Jr!   c                     g }d}d}|D ]5  \  }}}|dd \  }}|| z  }||k(  r||z  }|j                  ||||f       d}7 |S )z?
    Return a new shard tail that follows this shard body.
    r   r{   r  )r   )	r   r   r   col_gapr   r!  r"  rr   rs   s	            r    r   r   L  sz     JGI*/ &	<1QZ
dX	tOG7I|UCD r!   c              #      K   t        |      }dx}}dx}}| D ]`  \  }}|t        |      \  }}||k  r||z  }t        |      \  }}||k  r||kD  r||f ||z  }E|t        ||      f ||z  }d}||z  }b yw)zX
    Yield shards1 with cviews that are the same as shards2
    having canv = None.
    Nr   )iterr  shard_cviews_delta)	r   other_shardsother_shards_iterother_num_rowsother_cviewsdone
other_doner   r   s	            r    r   r   ^  s     
 \*$((N\D:" &!+/0A+B(NL4.(J+/0A+B(NL 4 V$$HD+FLABBn$
s   A	A<0A<c              #   .  K   t        |      }d}dx}}| D ]{  }|t        |      }||k  r||d   z  }t        |      }||k  r||kD  r| ||d   z  }@|d   |d   u r|dd |dd k(  r|dd dz   |dd z    n| ||d   z  }d}||d   z  }} yw)z
    Nr   r{   r  r^      )r&  r  )r   r+  other_cviews_iterother_cvrr   
other_colsr   s          r    r'  r'  v  s      \*HD: -.H4(1+%J-.H 4 HBqEMDa5HQKBrFhrl$:Ra&.AB''Hhqk!
1#s   ABABc                    d}g }t        |       }|D ]  \  }}}	}
|re	 t        |      }|dd \  }}}}}}||z  }||z  }|dk  rt        d      |r|r|j	                  |||||      }n|}|j                  d||f       |re|j                  ||	|
f        |D ]>  }|dd \  }}}}}}|r|r|j	                  |||||      }n|}|j                  d||f       @ |S # t        $ r Y fw xY w)a^  
    Return a list of (done_rows, content_iter, cview) tuples for
    this shard and shard tail.

    If a canvas in cviews is None (eg. when unchanged from
    shard_cviews_delta()) or if create_iter is False then no
    iterator is created for content_iter.

    iter_default is the value used for content_iter when no iterator
    is created.
    r   Nr/  z#cviews overflow gaps in shard_tail!)r&  r  StopIterationrL   rf   r   )r   r   create_iteriter_defaultr   bodycviews_iterr$  r   r!  
tail_cviewr"  rp   rq   rr   rs   r   r   new_iters                      r    r   r     sX    CDv,K8B ;4L*[) bq	 >Y$h4KCtOG{! ## $ $t<<	8$* (KKHe,-# $ 	Yj9:';(  *"1I 	:HdD(D4||IxtH $HQ%()* K3 ! s   C	C('C(c           
         |dkD  sJ t        |       }g }|D ]*  \  }}||k  r n+t        ||d      }t        ||      }||z  }, t        d      t        ||d      }t        ||      }g }|D ]'  \  }}	}
|j	                  d|	t        |
||z         f       ) |}||z
  |D 	
cg c]  \  }}	}
|

 c}
}	}fg}|j                  |       |S c c}
}	}w )z.
    Return shards with top rows removed.
    r   Fz%tried to trim shards out of existence)r&  r   r   rL   r   cview_trim_topr   )r   r   
shard_iterr   r   r   r   	new_sbodyr   r!  r   
new_shardss               r    r   r     s    7N7fJJ& C&>6:u5$Xu5
xC ABBvz51E 51JI', 0#	<!\2y}-/ 	00 EC</455+	<57 8J j! 	6s   "C	
c           	      @   |dk\  sJ |       g }d}| D ]  \  }}||k\  r |S g }|D ]=  }|d   |z   |kD  r|j                  t        |||z
               -|j                  |       ? ||z   |kD  r|j                  ||z
  |f       n|j                  ||f       ||z  } |S )z8
    Return the topmost keep_rows rows from shards.
    r      )r   cview_trim_rows)r   	keep_rowsr?  r   r   r   r  r   s           r    r   r     s     >$9$>JI" &	!  
 	&B!uy 9,!!/"	)#+ , !!"%	& i)+y94jABx45X	" r!   c                    |dk\  r|dkD  s	J ||f       g }g }||z   }| D ]  \  }}t        ||d      }t        ||      }g }	d}
|D ]\  \  }}}|d   }|
|z   }|s
||k  s|
|k\  r|}
 |
|k  rt        |||
z
        }|}
||kD  rt        |||
z
        }|	j	                  |       |}
^ |	s|d   \  }}||z   |f|d<   |j	                  ||	f        |S )zL
    Return shards with starting from column left and cols total width.
    r   Fr{   r  )r   r   cview_trim_leftcview_trim_colsr   )r   r   rr   r   r?  r   r   r   r   r  r   r   r!  r   cv_colsnext_colprev_num_rowsprev_cviewss                     r    r   r     s3    19/D$</!JJ4KE" 6&6:u5$Xu5

+0 	'I|ReGW}HH,uTz$R4%$R5b!C	 )3B&M;+H4kBJrNx45-6. r!   c                    | D cg c]  }t        |       }}|D cg c]  }t        |       }}g }	 g }t        |D cg c]  \  }}|	 c}}      }	g }
|D ].  \  }}|r|j                  |       |
j	                  ||	z
  df       0 |
}|j	                  |	|f       	 t        t        |            D ]  }||   d   dkD  rt        ||         ||<   ! 	 c c}w c c}w c c}}w # t        $ r Y |S w xY w)zx
    Return the result of joining shard lists horizontally.
    All shards lists must have the same number of rows.
    Nr   )r&  r  minr   r   r   r   r4  )shard_listsslshards_itersr   shards_currentr?  r  r   r   r   shards_nextrs   r   s                r    r  r    s$   
 (33DH3L3'34!d1g4N4J

n5da56* 	8LD&!!&)x67	8
 %8Z01	3~./ :!!$Q'!+$(a$9q!: 	 44
 6"  		s"   CC C!
6C' '	C43C4c                      | d d |fz   | dd  z   S )NrA  r  rJ   )r   rs   s     r    rB  rB  @       bq6TGbf$$r!   c                 >    | d   || d   z   | d   | d   |z
  f| dd  z   S )Nr   r5   r{   rA  r  rJ   r   r   s     r    r<  r<  C  s4    qE4"Q%<A1512>>r!   c                 6    | d   |z   | d   | d   |z
  f| dd  z   S )Nr   r5   r{   rA  rJ   rU  s     r    rE  rE  F  s.    qEDL"Q%A/"QR&88r!   c                      | d d |fz   | dd  z   S )Nr{   rA  rJ   )r   rr   s     r    rF  rF  I  rS  r!   c                 ,   | D cg c]  \  }}}t        |      ||f }}}}t               }g }g }d}d}	d}
|D ]  \  }}}|r|
}	|j                  d|||f       |j                  |j                         |j                  j                  |j                  d|             |j                  j                         D ]  }||j                  |<    ||j                         z  }|
dz  }
 |	r||	   g|d|	 z   ||	dz   d z   }||_        ||_
        |S c c}}}w )a|  Stack canvases in l vertically and return resulting canvas.

    :param l: list of (canvas, position, focus) tuples:

              position
                a value that widget.set_focus will accept or None if not
                allowed
              focus
                True if this canvas is the one that would be in focus
                if the whole widget is in focus
    r   r5   N)r   r   r   r   rS   r   r   rT   keysrs   r   )lr   pfclistcombined_canvasr   r   rh   focus_indexnr   r   r1   r   s                  r    CanvasCombinera  O  sD    4555%!Aaoa 1%5E5%'OFH
CK	A! 	c5KCs+,dkk"%%d&;&;As&CD++- 	6H25O%%h/	6tyy{	Q	 [)*Xl{-CC[]^$% $O'O3 6s   Dc                     t        |      }|j                  | ||       ||| dfdd|dfg|_        i |_        | j                  j	                         D ]  }d|j                  |<    |S )zE
    Overlay canvas top_c onto bottom_c at position (left, top).
    Nr   fg)r   r  r   rT   rY  )top_cbottom_cr   r   overlayed_canvasr   s         r    CanvasOverlayrg  w  s     'x0UD#."&UD!9	
Ax! !#OO((* 2-1""8,2r!   c                    g }d}d}d}| D ]O  \  }}}}|j                         }	||j                         z
  }
|r|}|	|kD  r|	}|j                  |||
|	f       |dz  }Q g }g }t               }d}|D ]  \  }}}
}	t        |      }|
r|j	                  d|
       |	|k  r|j                  d||	z
         |j                  j                  |j                  |d             |j                  j                         D ]  }||j                  |<    |j                  |j                         |j                  |d||f       ||j                         z  } |r||   g|d| z   ||dz   d z   }t        |      |_        ||_        |S )aG  
    Join canvases in l horizontally. Return result.

    :param l: list of (canvas, position, focus, cols) tuples:

              position
                value that widget.set_focus will accept or None if not allowed
              focus
                True if this canvas is the one that would be in focus if
                the whole widget is in focus
              cols
                is the number of screen columns that this widget will require,
                if larger than the actual canvas.cols() value then this widget
                will be padded on the right.
    r   r5   N)rs   rr   r   r   r   r  rS   r   r   rT   rY  r   r  r   )rZ  l2
focus_itemr   r`  r   r   r1   rr   rs   	pad_rightrM  r   joined_canvasr   r   s                   r    
CanvasJoinrm    s   " 
BJF	A"# c5$yy{499;&	J&=F
		4i./	Q KH#%M
C&( "c9dt$$$Q	2&=$$Q6##D$9$9#q$AB++- 	4H03M##H-	44;;'as+,tyy{ Z()H[j,AAZ\]#$ '{3M%Mr!   c           	          g }g }g } G d d      }|d_         d_        fd|D ]=  }t        | d|      }g }	g g }
 fd}|D ]  }t        |      }|j                  rjt         |j                  |j                         \  }}|	j                  |        ||j                  |j                  t        |             t        |
|       |j                  r]t        |j                        \  }}|	j                  |        ||j                  |j                  t        |             t        |
|       |j                  rj|j                  s|	j                  t               j                  |j                                ||j                  |j                  |j                         d|	j                  t               j                  |j                               j                  d |j                  f       |
j                  d |j                  f        |j                  t               j                  |	             |j                         |j                  |
       @ t!        ||||      S )Nc                       e Zd Zy)#apply_text_layout.<locals>.AttrWalkNrM   rJ   r!   r    AttrWalkrp    s    r!   rq  r   c                    | j                   k  rd_        d_         g }j                   |k  rEt              j                  k  r-|j                  d|t	        | j                         z
  f       	 |S j                     \  }}j                   |z   | k  r+xj                  dz  c_        xj                   |z  c_         |j                   |z   k  r-|j                  ||t	        | j                         z
  f       	 |S |j                  |j                   |z   t	        | j                         z
  f       xj                  dz  c_        xj                   |z  c_         j                   |k  rE|S )z9Return an attribute list for the range of text specified.r   Nr5   )offr  r   r   r   )
start_offsend_offsoatr   ri   aws        r    arangez!apply_text_layout.<locals>.arange  s=   BDBFff 4y"$$$xJrvv(>>?@  "$$ZFBsvvczZ'	#266#:%"hs:bff'==>?  HHb"&&*SRVV%<<=>DDAIDFFcMF! ff " r!   c                 b   | |k(  r 	| |      \  \  }}t        
||f       y||| z
  k(  r  	| |      D ]  \  }}t        
||f        y| } 	| |      D ]V  \  }}||z   |k(  rt        
||f        y|||z    }t        |      \  }}t        |      }t        
||f       ||z  }||z  }X y)zi
            Add attributes based on attributes between
            start_offs and end_offs.
            N)r   r   r   )rt  ru  destwrw  r   rv  tsegrj   segwry  lineark   s            r    	attrrangez$apply_text_layout.<locals>.attrrange  s    
 X%#Jx8
"S!5B-8++%j: ;GB%uCk:;A!*h7 
CS5H$%uEm<Aae}0$8br{!5B,7S
r!   )r   )r  rs  r   r   r   r   offsr   r   r   rk   r   scr   r   rg   r   )rk   ri   lsr   r   r   r   rq  line_layoutr   linecr  segsr|  rj   ry  rx  r~  s   ``              @@@r    apply_text_layoutr    s   
A
A
A 	BBDBF6  =dAv?	8  	+Cc"Auu0&(bD!!&&!%%5,0!&&:bD!1661663t96,44KKadd 34qvvqvvqtt5EGMM!$$/0dADD\*dADD\*+	+. 	
d#$		{=~ aAf--r!   )TN)-
__future__r   r   r(   
urwid.utilr   r   r   r   r	   r
   r   r   urwid.text_layoutr   r   urwid.compatr   objectr   	ExceptionrL   rO   r   r   r   r   r   r   r   r   r'  r   r   r   r   r  rB  r<  rE  rF  ra  rg  rm  r  rJ   r!   r    <module>r     s   , 0 H H H 6 N& Nd	) 	AV AJL L`L& L8 }'& 'X`&f `&F	($06,^!F6BB%?9%%P6re.r!   