
    jjT                       d dl mZ d dlZd dlmZmZmZ d dlmZ ddl	m
Z
mZ ddlmZ ddlmZmZmZmZmZ ej(                  rd d	lmZ  G d
 de      Zd Z G d de      Z G d dee      ZddZ G d de      ZddZy)    )annotationsN)CanvasCombineCompositeCanvasSolidCanvas)get_encoding_mode   )BAR_SYMBOLSSizing)Text)WidgetWidgetError
WidgetMetanocache_widget_rendernocache_widget_render_instance)Literalc                  "     e Zd ZdZ fdZ xZS )BarGraphMetaz
    Detect subclass get_data() method and dynamic change to
    get_data() method and disable caching in these cases.

    This is for backwards compatibility only, new programs
    should use set_data() instead of overriding get_data().
    c                    t         |   |||       d|v r!t        |       | _        | j                  | _        t        d t              | _        y )Nget_datac                    | j                   S N)	_get_dataselfs    _/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/widget/bar_graph.py<lambda>z'BarGraphMeta.__init__.<locals>.<lambda>!   s
    T^^     )super__init__r   renderr   r   propertynocache_bargraph_get_data)clsnamebasesd	__class__s       r   r   zBarGraphMeta.__init__   sD     	ua(?.s3CJLLCM ;=VWr   )__name__
__module____qualname____doc__r   __classcell__r'   s   @r   r   r      s    X Xr   r   c                2    t        |       | _        || _        y)zm
    Disable caching on this bargraph because get_data_fn needs
    to be polled to get the latest data.
    N)r   r    r   )r   get_data_fns     r   r"   r"   $   s    
 16DK DNr   c                      e Zd Zy)BarGraphErrorN)r(   r)   r*    r   r   r1   r1   -   s    r   r1   c                       e Zd Z eej
                  g      ZdZej                  dd Z
dZdd fdZddZ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 ZdddZ xZS )BarGraphTN   u   _⎺⎻─⎼⎽c                    t         |           | j                  |||       | j                  g dd       | j	                  d       y)z
        Create a bar graph with the passed display characteristics.
        see set_segment_attributes for a description of the parameters.
        r   N)r   r   set_segment_attributesset_dataset_bar_width)r   attlisthattsattr'   s       r   r   zBarGraph.__init__9   s@    
 	##GT48b!T"4 r   c                   g | _         g | _        t        |      dk  rt        d|      t        |      dk  rt        d      |D ]  }t	        |t
              s7| j                   j                  |       | j                  j                  d       J|\  }}| j                   j                  |       | j                  j                  |        g | _        || j                   d   g}nt	        |t              s|g}|| _        |i }|j                         D ]z  }	 |\  \  }}	}t	        |t              r|t        |      k\  rt        d|      t	        |	t              r|	t        |      k\  rt        d	|      ||	k  sjt        d
| d|	 d       || _        y# t        $ r-}
t        d|      j                  |
j                        |
d}
~
ww xY w)a  
        :param attlist: list containing display attribute or
                        (display attribute, character) tuple for background,
                        first segment, and optionally following segments.
                        ie. len(attlist) == num segments+1
                        character defaults to ' ' if not specified.
        :param hatt: list containing attributes for horizontal lines. First
                     element is for lines on background, second is for lines
                     on first segment, third is for lines on second segment
                     etc.
        :param satt: dictionary containing attributes for smoothed
                     transitions of bars in UTF-8 display mode. The values
                     are in the form:

                       (fg,bg) : attr

                     fg and bg are integers where 0 is the graph background,
                     1 is the first segment, 2 is the second, ...
                     fg > bg in all values.  attr is an attribute with a
                     foreground corresponding to fg and a background
                     corresponding to bg.

        If satt is not None and the bar graph is being displayed in
        a terminal using the UTF-8 encoding then the character cell
        that is shared between the segments specified will be smoothed
        with using the UTF-8 vertical eighth characters.

        eg: set_segment_attributes( ['no', ('unsure',"?"), 'yes'] )
        will use the attribute 'no' for the background (the area from
        the top of the graph to the top of the bar), question marks
        with the attribute 'unsure' will be used for the topmost
        segment of the bar, and the attribute 'yes' will be used for
        the bottom segment of the bar.
           z3attlist must include at least background and seg1: z must at least specify bg and fg! Nr   zsatt not in (fg,bg:attr) form: zfg not valid integer: zbg not valid integer: zfg (z) not > bg ())attrcharlenr1   
isinstancetupleappendr;   listitems
ValueErrorwith_traceback__traceback__intr<   )r   r:   r;   r<   arA   chifgbgexcs              r   r7   zBarGraph.set_segment_attributesC   s   F 		w<!"UV]U` abbw<! BCC 	%Aa'		  #		  %b		  &		  $	% 	<IIaL>DD$'6D	<D 
	BAx!"R$ b#&"G*<#&<RF$CDDb#&"G*<#&<RF$CDDRx#d2$l2$a$@AA
	B 	  x#&EaU$KL[[\_\m\mntwwxs   F	G#(GGc                \    |t        |dd d      }|||f| _        | j                          y)a  
        Store bar data, bargraph top and horizontal line positions.

        bardata -- a list of bar values.
        top -- maximum value for segments within bardata
        hlines -- None or a bar value marking horizontal line positions

        bar values are [ segment1, segment2, ... ] lists where top is
        the maximal value corresponding to the top of the bar graph and
        segment1, segment2, ... are the values for the top of each
        segment of this bar.  Simple bar graphs will only have one
        segment in each bar value.

        Eg: if top is 100 and there is a bar value of [ 80, 30 ] then
        the top of this bar will be at 80% of full height of the graph
        and it will have a second segment that starts at 30%.
        NTreverse)sorteddata_invalidate)r   bardatatophliness       r   r8   zBarGraph.set_data   s4    $ F1It4FS&(	r   c                    |\  }}| j                   \  }}}| j                  ||f|      }t        |      t        |      kD  r|dt        |       ||fS |||fS )a/  
        Return (bardata, top, hlines)

        This function is called by render to retrieve the data for
        the graph. It may be overloaded to create a dynamic bar graph.

        This implementation will truncate the bardata list returned
        if not all bars will fit within maxcol.
        N)rW   calculate_bar_widthsrC   )r   sizemaxcolmaxrowrY   rZ   r[   widthss           r   r   zBarGraph._get_data   sj      #yyf**FF+;WEw<#f+%=S[)366V##r   c                V    ||dk  rt        |      || _        | j                          y)z
        Set a preferred bar width for calculate_bar_widths to use.

        width -- width of bar or None for automatic width adjustment
        Nr   )rI   	bar_widthrX   )r   widths     r   r9   zBarGraph.set_bar_width   s.     !U##r   c                F   |\  }}| j                   0| j                   gt        t        |      || j                   z        z  S t        |      |k\  rdg|z  S g }|}t        |      }|D ]7  }t        t	        |      |z  dz         }	|j                  |	       ||	z  }|dz  }9 |S )z
        Return a list of bar widths, one for each bar in data.

        If self.bar_width is None this implementation will stretch
        the bars across the available space specified by maxcol.
        r         ?)rc   minrC   rL   floatrF   )
r   r^   rY   r_   _maxrowra   growremain_rowws
             r   r]   zBarGraph.calculate_bar_widths   s     !>>%NN#c#g,$..8P&QQQw<6!3<W 	DE$K&(3./AMM!AIDaKF		
 r   c                     yz
        Return False.
        Fr2   r   s    r   
selectablezBarGraph.selectable        r   c                8    | j                   xr t               dk(  S )Nutf8)r<   r   r   s    r   use_smoothedzBarGraph.use_smoothed   s    yy:.0F::r   c                   |\  }}| j                  ||f      \  }}}| j                  ||f|      }| j                         r#t        ||||dz        }| j	                  |      }nt        ||||      }|r| j                  ||||      }|S )z)
        Calculate display data.
        r5   )r   r]   rt   calculate_bargraph_displaysmooth_displayhlines_display)	r   r^   r_   r`   rY   rZ   r[   ra   disps	            r   calculate_displayzBarGraph.calculate_display   s      #}}ff-=>f**FF+;WE-gsFFQJOD&&t,D .gsFFKD&&tS&&ADr   c                     j                         rd}g d}nd}dg}|D cg c]!  }t        ||z
        |z  |z  |z
  x}dk\  s |# }	}g }
d}|	D ]>  }t        |      }||k(  r||z
  }|D ]  \  }}||k  s|
j                  ||f        n |}@  fd}g }d}d}|D ]  \  }}|t	        |
      k\  r|j                  ||f       (||z   }|t	        |
      k  ro|
|   d   |k  rd|
|   \  }}||z
  dkD  r|j                  ||z
  |f       |j                  d |||      f       |dz   }|dz  }|t	        |
      k  r|
|   d   |k  rd||k  s|j                  ||z
  |f       |} |S c c}w )a+  
        Add hlines to display structure represented as bar_type tuple
        values:
        (bg, 0-5)
        bg is the segment that has the hline on it
        0-5 is the hline graphic to use where 0 is a regular underscore
        and 1-5 are the UTF-8 horizontal scan line characters.
        r   ))g?r   )g?r>   )g333333?   )g?   )      ?   rf   )r~   r   c                    g }| D ]V  \  }}t        |t              r.t        j                        |kD  r|j	                  ||f|f       D|j	                  ||f       X |S r   )rD   rL   rC   r;   rF   )rowchnumroutbar_typerd   r   s        r   fill_rowz)BarGraph.hlines_display.<locals>.fill_row!  se    D#& /%h,TYY(1JKK(E!2E :;Xu-.	/
 Kr   r   )rt   rh   rL   rF   rC   )r   ry   rZ   r[   r`   shiftrrhrhrhlhrowslast_irO   fsplr   r   okrnumy_countr   	end_blocks   `                      r   rx   zBarGraph.hlines_display   s    FA FA
 "YaE#'NV,Cc,IF,R&RbWX%XrYY  		BBAF{QA 
Us7LL!U, F			   	!LGSCJ'3(wIc%j.U1Xa[9%< 85t8a<HHa$h_-!Xc51231uQ c%j.U1Xa[9%< i)d*C01 	!" ] Zs   !E*E*c                d   	 g d fd	d	fd}|D ]w  \  }}r*t        dz
  |      } |||       ||z  }|z  dz  |s2dk7  rt        |dkD  r!j                  |dz  dz  |f       |dz  }|scj                  ||f       |y D cg c]  \  }}|dz  |f c}}S c c}}w )a0  
        smooth (col, row*8) display into (col, row) display using
        UTF vertical eighth characters represented as bar_type
        tuple values:
        ( fg, bg, 1-7 )
        where fg is the lower segment, bg is the upper segment and
        1-7 is the vertical eighth character to use.
        r   c                   | |c\  }}\  }}||f||fk(  r||fd d fS t        ||      }d x}}||kD  r|||z
  f}n||kD  r|||z
  f}t        |t              r||f||fS ||f
j                  vr	dk  r||f||fS ||f||fS ||d	z
  f|f||fS )Nr}   r5   )rg   rD   rE   r<   )rM   bbt1w1bt2w2wminl1l2r   r   s            r   seg_combinez,BarGraph.smooth_display.<locals>.seg_combineL  s    #$a IS"yRRyS"I%Ry$,,r2;DNBBw27^b27^#u%T{B**Sz*q5;B..T{B**#q1u%t,b"44r   c                @   	d   \  }}|d d  }|d d  }g }|rp 
|j                  d      |j                  d            \  \  }}}}|r|d   d   |k(  r||d   d   |z   f|d<   n|j                  ||f       |r|g|}|r|g|}|rp|rt        |      || z   |f	d<   y )Nr   r   r   )poprF   r1   )countr   o_counto_rowwidget_listbtrm   r   r   r   r   s            r   row_combine_lastz1BarGraph.smooth_display.<locals>.row_combine_last^  s    rUNGUa&C!HEK"-eiilCGGAJ"GQR;r?1#5#;');r?1+=+A&BKO&&Aw/L%LE**C  #E**u_k2AbEr   r5      )r   rL   )rg   r1   rF   )
r   ry   r   r   r   r   yr   r   r   s
   `      @@@r   rw   zBarGraph.smooth_display@  s     	5$	3, ! 	LGSAE7+ ,5 U
QAv##{'Q,*C011HHgs^$A%	& /00(1ca000s   B,c                L   |\  }}| j                  ||f      }g }|D ]  \  }}g }	|D ]  \  }
}t        |
t              r_t        |
      dk(  r*|
\  }}}| j                  ||f   }| j
                  |   |z  }nH|
\  }}| j                  |   }| j                  |   |z  }n!| j                  |
   }| j                  |
   |z  }|	j                  ||f        t        |	      j                  |f      }|j                         dk7  rt        d      ||ddfg|z  z  } t        |      }|S )z"
        Render BarGraph.
        r|   r   zInvalid characters in BarGraph!NF)rz   rD   rE   rC   r<   eighthsr;   r[   rA   rB   rF   r   r    rowsr1   r   )r   r^   focusr_   r`   ry   combinelistr   r   r   r   rd   rP   rQ   r   rM   tccanvs                      r   r    zBarGraph.render  sQ     %%vv&67  	8LGSK#& +%h.8})$,	B IIb"f- LLOe3 !)A IIbM KKNU2		(+A		(+e3A""Aq6*+  [!((&3Avvx1}#$EFFQe,-77K+	8. [)r   )NN)returnNoner   rZ   rh   r   r   )r^   tuple[int, int])rd   z
int | Noner   zLiteral[False])r   bool)rZ   rL   r`   rL   F)r^   r   r   r   r   r   )r(   r)   r*   	frozensetr
   BOX_sizingignore_focusr	   VERTICALr   r[   r   r7   r8   r   r9   r]   rp   rt   rz   rx   rw   r    r,   r-   s   @r   r4   r4   1   sv    %GL""2A&GF!FP0$&	2;(GRG1R   r   r4   )	metaclassc                   t        |       t        |      k7  rt        t        |      }dg|z  }|fdd}d}d}| D ]q  }	||   }
|
dk  r|}t        |	||      }t	        t        |	      dz
  dd      D ]/  }||   }||k\  rt        |d      }||k  s |} ||dz   |||
       1 ||
z  }|dz  }s g }d}d|fg}|D ]  }||dz  }|r|j                  ||f       d}d}||   \  }}d}g }|D ])  \  }}}|||z   kD  r.|j                  ||f       |dz  }||z  }||   \  }}|||z   kD  r.||k(  r|j                  |||z
  f       n4||z
  dkD  r|j                  |||z
  f       |j                  |||z
  f       ||k(  rt        |      } n|||z   k\  r|dz  }||z  }||   \  }}|||z   k\  r||k7  r||z   |z
  }|}|d   \  }}|||z   |z
  z  }||f|d<   |dz  }||z  }||k(  r n/|t        |      k\  rt        t        ||f            ||   \  }}, |t        |      k  r|||fg||dz   d z  }|}|dz  } |r|j                  ||f       |S )a  
    Calculate a rendering of the bar graph described by data, bar_widths
    and height.

    bardata -- bar information with same structure as BarGraph.data
    top -- maximal value for bardata segments
    bar_widths -- list of integer column widths for each bar
    maxrow -- rows for display of bargraph

    Returns a structure as follows:
      [ ( y_count, [ ( bar_type, width), ... ] ), ... ]

    The outer tuples represent a set of identical rows. y_count is
    the number of rows in this set, the list contains the data to be
    displayed in the row repeated through the set.

    The inner tuple describes a run of width characters of bar_type.
    bar_type is an integer starting from 0 for the background, 1 for
    the 1st segment, 2 for the 2nd segment etc..

    This function should complete in approximately O(n+m) time, where
    n is the number of bars displayed and m is the number of rows.
    Nc                    ||   rB||   d   \  }}}||kD  r||k\  r||   d= n/|||f||   d<   n#|| k(  r||k(  r||||z   f||   d<   y ||   g ||<   ||   j                  | |||z   f       y )Nr   )rF   )seg_numcolr   rd   r   last_seglast_collast_ends           r   add_segmentz/calculate_bargraph_display.<locals>.add_segment  s    9+/9R=(Hh#~s?S	"%-x$=DIbMW$S!)8X5E FS	"#YDIS	'3e45r   r   r   r   )
r   rL   r   rL   r   rL   rd   rL   r   r   )	rC   r1   sumscale_bar_valuesrangemaxrF   rI   repr)rY   rZ   
bar_widthsr`   r_   r   r   r   barnumbarrd   tallestsegmentsr   srowsetsr   lastr   rO   lalnr   r   r   startendoaons                                r   rv   rv     s<   2 7|s:&_F 6F?DGK 6 CF 6"19#Cf5s3x!|R, 		2AAF{Aq	A7{AE351		2 	u!%, GGK=D :9qLGNNGT?+GaB#$ )	GUC!b&."b"QRaB	 !b&. W}"cAg'19q=HHb%!)_-'3;/0f}I R-QRaB R-
 W}Vc\ rUFB!b&3,BFAbEFAGAF{CI~ r6l!344!WFBS)	V s4y=2r(+T!a%']++A1u:x 'Nr   c                  l     e Zd Z eej
                  g      Zd fdZddZddZ		 d	 	 	 	 	 ddZ
 xZS )	GraphVScalec                F    t         |           | j                  ||       y)a]  
        GraphVScale( [(label1 position, label1 markup),...], top )
        label position -- 0 < position < top for the y position
        label markup -- text markup for this label
        top -- top y position

        This widget is a vertical scale for the BarGraph widget that
        can correspond to the BarGraph's horizontal lines
        N)r   r   	set_scale)r   labelsrZ   r'   s      r   r   zGraphVScale.__init__=  s     	vs#r   c                    t        |dd d      }g | _        g | _        |D ]D  \  }}| j                  j                  |       | j                  j                  t	        |             F || _        y)z
        set_scale( [(label1 position, label1 markup),...], top )
        label position -- 0 < position < top for the y position
        label markup -- text markup for this label
        top -- top y position
        NTrT   )rV   postxtrF   r   rZ   )r   r   rZ   r   markups        r   r   zGraphVScale.set_scaleJ  sb     q	40 	*IAvHHOOAHHOODL)	* r   c                     yro   r2   r   s    r   rp   zGraphVScale.selectable[  rq   r   c                   |\  }}t        | j                  | j                  |      }g }d}t        || j                        D ]w  \  }}	|dz  }||k\  r nh||k  r|	j                  |f      }
||kD  r"||z
  }t        |
      }
|
j                  |d       ||
j                         z  }|j                  |
ddf       y |st        d|d   |d         S t        |      }||z
  r|j                  d||z
         |S )z%
        Render GraphVScale.
        r   r   NFr?   )r   r   rZ   zipr   r    r   pad_trim_top_bottomr   rF   r   r   )r   r^   r   r_   r`   plr   r   pr   r   runcanvass                r   r    zGraphVScale.rendera  s     dhh&9DHH% 	1DAqFAF{4x&#A4x$h#A&%%c1-AFFHD4/0	1 sDGT!W55{+D=&&q&4-8r   r   r   r   )r^   r   r   r   r   zSolidCanvas | CompositeCanvas)r(   r)   r*   r   r
   r   r   r   r   rp   r    r,   r-   s   @r   r   r   :  sH    %G$"      
'	 r   r   c           	     h    | D cg c]"  }|t        t        |      |z  |z  dz         z
  $ c}S c c}w )zJ
    Return a list of bar values aliased to integer values of maxrow.
    rf   )rL   rh   )r   rZ   r`   vs       r   r   r     s7     BEEAFSqF*S03677EEEs   '/)rZ   rh   r   	list[int]r`   rL   )rZ   rh   r`   rL   r   r   )
__future__r   typingurwid.canvasr   r   r   
urwid.utilr   	constantsr	   r
   textr   widgetr   r   r   r   r   TYPE_CHECKINGtyping_extensionsr   r   r"   r1   r4   rv   r   r   r2   r   r   <module>r      sx    "  D D ( *  j j	)X: X(!	K 	xv xvK\G& GTFr   