
    jjuH                    ,   d dl mZ d dlZd dlZd dlmZ ddlmZ ddlm	Z	m
Z
mZ ddl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 ddlmZmZmZmZ ej:                  rd dlmZm Z m!Z! d dl"m#Z#  G d de      Z$ G d de      Z% G d dee   ee      Z&y)    )annotationsN)remove_defaults   )Columns)AlignSizing
WHSettings) WidgetContainerListContentsMixinWidgetContainerMixin)Divider)MonitoredFocusListMonitoredList)Padding)Pile)WidgetWidgetErrorWidgetWarning
WidgetWrap)IterableIteratorSequence)Literalc                      e Zd ZdZy)GridFlowErrorzGridFlow specific error.N__name__
__module____qualname____doc__     _/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/widget/grid_flow.pyr   r      s    "r!   r   c                      e Zd ZdZy)GridFlowWarningzGridFlow specific warning.Nr   r    r!   r"   r$   r$      s    $r!   r$   c                      e Zd ZdZd%dZ	 d&	 	 	 	 	 	 	 	 	 	 	 	 	 d' fdZd( fdZd) fdZd*dZd+dZ	d, fd	Z
	 	 	 	 	 	 d-d
Zed        Zej                  d.d       Zed+d       Zej                  d/d       Zed0d       Zej                  d        Zej$                  df	 	 	 	 	 d1dZd2dZed3d       Zd Zed        Zej                  d4d       Zed5d       Zej                  d6d       Zd7dZd8dZd8dZd,dZ	 	 	 	 	 	 d9 fdZ	 	 d:	 	 	 	 	 d; fdZd<d= fdZ	 d<	 	 	 d> fd Z d? fd!Z!d@ fd"Z"	 	 	 	 	 	 	 	 	 	 	 	 	 	 dA fd#Z#dB fd$Z$ xZ%S )CGridFlowz
    The GridFlow widget is a flow widget that renders all the widgets it contains the same width,
    and it arranges them from left to right and top to bottom.
    c                    | r)t        t        j                  t        j                  f      S t        t        j                  f      S )znWidget sizing.

        ..note:: Empty widget sizing is limited to the FLOW due to no data for width.
        )	frozensetr   FLOWFIXEDselfs    r"   sizingzGridFlow.sizing%   s0    
 fkk6<<899&++((r!   Nc                    g }d}t        |      D ]J  \  }	}
|j                  |
t        j                  |ff       |dk  s/||
|	hv s|8|
j	                         sI|	}L t        |d      }t        ||       _         j                  j                   j                          j                  j                   fd        j                  j                   j                         | _        | _        | _        | _         j#                  d       _        t&         Q   j+                   j$                  f             y)a  
        :param cells: iterable of flow widgets to display
        :param cell_width: column width for each cell
        :param h_sep: blank columns between each cell horizontally
        :param v_sep: blank rows between cells vertically
            (if more than one row is required to display all the cells)
        :param align: horizontal alignment of cells, one of:
            'left', 'center', 'right', ('relative', percentage 0=left 100=right)
        :param focus: widget index or widget instance to focus on
        r   Nfocusc                $    j                         S N)_invalidate)fr,   s    r"   <lambda>z#GridFlow.__init__.<locals>.<lambda>O   s    D<L<L<N r!   r    )	enumerateappendr	   GIVEN
selectablemaxr   	_contentsset_modified_callbackr4   set_focus_changed_callbackset_validate_contents_modified_contents_modified_cell_widthh_sepv_sepalign_get_maxcol_cache_maxcolsuper__init__generate_display_widget)r,   cells
cell_widthrB   rC   rD   r1   prepared_contentsfocus_positionidxwidget	__class__s   `          r"   rH   zGridFlow.__init__.   s&   & Y[ $U+ 	%KC$$fz/?/?.L%MN!u'=%-TZTeTeTg!$	%
 ^Q/cu^d
 	,,T-=-=>112NO55d6M6MN%


!--b155t7I7I6KLMr!   c                    t        | j                        dkD  rdt        | j                         d}n| j                  rd}nd}g t        |          |S )Nr   (z items)z(1 item)z())lencontentsrG   _repr_words)r,   contents_stringrP   s     r"   rU   zGridFlow._repr_wordsX   sU    t}}! !#dmm"4!5W=O]](O"O8$&888r!   c                    i t         |          | j                  | j                  | j                  | j
                  t        | j                        dkD  r| j                  nd d}t        |t        j                        S )Nr   )rK   rB   rC   rD   r1   )rG   _repr_attrsrK   rB   rC   rD   rS   r<   rM   r   r&   rH   )r,   attrsrP   s     r"   rX   zGridFlow._repr_attrsa   sk    
g!#
//ZZZZZZ,/,?!,CT((
 uh&7&788r!   c              #     K   d| j                   D cg c]  \  }}|	 c}}f d| j                  f d| j                  f d| j                  f d| j                  f d| j
                  f y c c}}w w)NrJ   rK   rB   rC   rD   r1   )rT   rK   rB   rC   rD   rM   )r,   rO   _s      r"   __rich_repr__zGridFlow.__rich_repr__l   sv     >961>>>DOO++tzz!!tzz!!tzz!!t**** ?s   A=A7AA=c                ,    t        | j                        S r3   )rS   r<   r+   s    r"   __len__zGridFlow.__len__t   s    4>>""r!   c                0    d | _         t        | 	          y r3   )rF   rG   r4   )r,   rP   s    r"   r4   zGridFlow._invalidatew   s    !r!   c                    |D ],  }	 |\  }\  }}|t         j                  k7  rt        d|      . y # t        t        f$ r-}t        d|      j                  |j                        |d }~ww xY w)Nzadded content invalid )r	   r9   r   	TypeError
ValueErrorwith_traceback__traceback__)r,   _slc	new_itemsitem_wt_nexcs           r"   r@   zGridFlow._contents_modified{   s    
  	rDr"GQ
((('*@(IJJ )	r
 z* r#&<TH$EFUUVYVgVghnqqrs   )3A/(A**A/c                     t        j                  dt        d       t        d  j                  D               fd}j                  |       S )z
        A list of the widgets in this GridFlow

        .. note:: only for backwards compatibility. You should use the new
            standard container property :attr:`contents` to modify GridFlow
            contents.
        only for backwards compatibility.You should use the new standard container property `contents` to modify GridFlow.API will be removed in version 5.0.   
stacklevelc              3  &   K   | ]	  \  }}|  y wr3   r    ).0wri   s      r"   	<genexpr>z!GridFlow.cells.<locals>.<genexpr>   s     7A17s   c                      _         y r3   )rJ   )mlr,   s   r"   user_modifiedz%GridFlow.cells.<locals>.user_modified   s
    DJr!   )warningswarnDeprecationWarningr   rT   r=   )r,   rw   rv   s   ` @r"   rJ   zGridFlow.cells   sN     	2 	
 777	 	  /	r!   c                    t        j                  dt        d       | j                  }|D cg c]   }|t        j
                  | j                  ff" c}| _        |t        |      k  r|| _        y y c c}w )Nrm   rn   ro   )	rx   ry   rz   rM   r	   r9   rA   rT   rS   )r,   widgetsrM   news       r"   rJ   zGridFlow.cells   so    2 	
 ,,PWX#
 0 0$2B2BCDXCL("0D ) Ys   %A1c                    | j                   S )zg
        The width of each cell in the GridFlow. Setting this value affects
        all cells.
        )rA   r+   s    r"   rK   zGridFlow.cell_width   s     r!   c                    | j                   }| j                  D cg c]  \  }}|t        j                  |ff c}}| _        || _         || _        y c c}}w r3   )rM   rT   r	   r9   rA   )r,   widthrM   rs   optionss        r"   rK   zGridFlow.cell_width   sM    ,,LPMMZLQ!j..67Z,  [s   Ac                    | j                   S )a  
        The contents of this GridFlow as a list of (widget, options)
        tuples.

        options is currently a tuple in the form `('fixed', number)`.
        number is the number of screen columns to allocate to this cell.
        'fixed' is the only type accepted at this time.

        This list may be modified like a normal list and the GridFlow
        widget will update automatically.

        .. seealso:: Create new options tuples with the :meth:`options` method.
        r<   r+   s    r"   rT   zGridFlow.contents   s     ~~r!   c                "    || j                   d d  y r3   r   )r,   cs     r"   rT   zGridFlow.contents   s    qr!   c                z    |t         j                  k7  rt        d|      || j                  }t        |      |fS )z
        Return a new options tuple for use in a GridFlow's .contents list.

        width_type -- 'given' is the only value accepted
        width_amount -- None to use the default cell_width for this GridFlow
        zinvalid width_type: )r	   r9   r   rA   )r,   
width_typewidth_amounts      r"   r   zGridFlow.options   sF     )))"6zn EFF++L:&55r!   c                   t        j                  dt        d       t        |t              r?	 |dk  s|t        | j                        k\  rt        d|       	 || j                  _        yt        | j                        D ]  \  }\  }}||k(  s|| _         y t        d|      # t        $ r-}t        d|       j                  |j                        |d}~ww xY w)a7  
        Set the cell in focus, for backwards compatibility.

        .. note:: only for backwards compatibility. You may also use the new
            standard container property :attr:`focus_position` to get the focus.

        :param cell: contained element to focus
        :type cell: Widget or int
        zonly for backwards compatibility.You may also use the new standard container property `focus_position` to set the focus.API will be removed in version 5.0.rn   ro   r   %No GridFlow child widget at position N'Widget not found in GridFlow contents: )rx   ry   rz   
isinstanceintrS   rT   
IndexErrorra   rc   rd   r1   r7   rM   rb   )r,   cellrk   irs   _optionss         r"   	set_focuszGridFlow.set_focus   s     	2 	
 dC !8ts4=='99$'LTF%STT  : #'DMM )$-- 8 	A}8qy&'#	 B4(KLL   #H!OP__%%s   +B( (	C1(CCc                T    | j                   sy| j                   | j                     d   S )z8the child widget in focus or None when GridFlow is emptyNr   )rT   rM   r+   s    r"   r1   zGridFlow.focus  s(     }}}}T001!44r!   c                    t        j                  dt        d       | j                  sy| j                  | j                     d   S )z
        Return the widget in focus, for backwards compatibility.

        .. note:: only for backwards compatibility. You may also use the new
            standard container property :attr:`focus` to get the focus.
        zonly for backwards compatibility.You may also use the new standard container property `focus` to get the focus.API will be removed in version 5.0.rn   ro   Nr   )rx   ry   rz   rT   rM   r+   s    r"   	get_focuszGridFlow.get_focus  sC     	2 	
 }}}}T001!44r!   c                R    t        j                  dt        d       | j                  S )Nonly for backwards compatibility.You may also use the new standard container property`focus` to get the focus and `focus_position` to get/set the cell in focus by index.API will be removed in version 5.0.rn   ro   )rx   ry   rz   r1   r+   s    r"   
focus_cellzGridFlow.focus_cell  s'    2 	
 zzr!   c                    t        j                  dt        d       t        | j                        D ]  \  }\  }}||k(  s|| _         y  t        d|      )Nr   rn   ro   r   )rx   ry   rz   r7   rT   rM   rb   )r,   r   r   rs   r   s        r"   r   zGridFlow.focus_cell*  se    2 	
 !*$-- 8 	A}8qy&'#	 B4(KLLr!   c                \    | j                   st        d      | j                   j                  S )z
        index of child widget in focus.
        Raises :exc:`IndexError` if read when GridFlow is empty, or when set to an invalid index.
        z$No focus_position, GridFlow is empty)rT   r   r1   r+   s    r"   rM   zGridFlow.focus_position:  s'     }}CDD}}"""r!   c                    	 |dk  s|t        | j                        k\  rt        d|       	 || j                  _        y# t        $ r-}t        d|       j	                  |j
                        |d}~ww xY w)zf
        Set the widget in focus.

        position -- index of child widget to be made focus
        r   r   N)rS   rT   r   ra   rc   rd   r1   )r,   positionrk   s      r"   rM   zGridFlow.focus_positionD  s    	!|x3t}}+== #H
!STT  > '	  	DXJOP__!!	s   +A   	A6	(A11A6c                   |rD|\  }| r<|| j                   k  r-t        j                  d|d| j                   dt        d       |S | r6t	        |       | j                   z  t	        |       dz
  | j
                  z  z   }|S d}|S )Nz!Size is smaller than cell width (z < )   ro   r   r   )rK   rx   ry   r$   rS   rB   r,   sizemaxcols      r"   rE   zGridFlow._get_maxcolT  s    IV07zT__DWWXY#  	 Y0CIMTZZ3OOF  Fr!   c                    | j                  |      }| j                  |k(  r| j                  S || _        | j                  |f      | _        | j                  S )z
        Arrange the cells into columns (and possibly a pile) for
        display, input or to calculate rows, and update the display
        widget.
        )rE   rF   rh   rI   r   s      r"   get_display_widgetzGridFlow.get_display_widgetc  sR     !!$' '77N#..y9wwr!   c                   | j                  |      }t               }| j                  s|S | j                  dkD  r| j                  dz
  |_        d}t        g       }d}t        | j                        D ]  \  }\  }\  }	}
|||z
  |
k  r| j                  r+|j                  j                  ||j                         f       t        g | j                        }d}t        || j                        }||_        |j                  j                  ||j                         f       |j                  j                  ||j                  t        j                  t!        |
|            f       || j"                  k(  ss/|j%                         rt'        |j                        dz
  |_        d}|| j"                  k(  rt'        |j                        dz
  |_        t)        d |j                  D              | j                  t'        |j                        z  z   }|| j                  z
  _         | j                  r|j                  dd= |S |j-                          |S )zC
        Actually generate display widget (ignoring cache)
        r   Nr   FTc              3  ,   K   | ]  }|d    d      yw)r   Nr    )rr   xs     r"   rt   z3GridFlow.generate_display_widget.<locals>.<genexpr>  s     9QqT!W9s   )rE   r   rT   rC   topr   r7   r8   r   r   rB   r   rD   first_positionr	   r9   minrM   r:   rS   sumr   r@   )r,   r   r   dividerr   p
used_spacer   rs   _width_typer   column_focusedpads                r"   rI   z GridFlow.generate_display_widgett  s    !!$')}}N::>**q.GKH
3<T]]3K 	0/A/.KyFZ/,>::JJ%%w		&<=B

+!&a,%&"

!!3		"45 JJq!))J,<,<c,PV>W"XYZT(((.Q\\^#&qzz?Q#6 !%D'''#&qzz?Q#6 9ajj99DJJQZZ<XXJ"TZZ/CI+	0. ::

2A2
    "r!   c                    | j                   j                  }|sy|j                  }|j                  r|j                  }nd}|j                  |z   | _        y)zK
        Set the focus to the item in focus in the display widget.
        Nr   )rh   r1   base_widgetrM   r   )r,   
pile_focusr   col_focus_positions       r"   _set_focus_from_display_widgetz'GridFlow._set_focus_from_display_widget  sO     WW]]
""77!"!1!1!"(77:LLr!   c                p    | j                  |       t        | 	  ||      x}|S | j                          y)z_
        Pass keypress to display widget for handling.
        Captures focus changes.
        N)r   rG   keypressr   )r,   r   keyrP   s      r"   r   zGridFlow.keypress  s>     	%7#D#..C;J++-r!   c                    |rt         |   ||      S | r5t        |       | j                  z  t        |       dz
  | j                  z  z   }nd}|| j                  |f|      fS )Nr   r   )rG   packrS   rK   rB   rows)r,   r   r1   colsrP   s       r"   r   zGridFlow.pack  sa    
 7<e,,t9t.#d)a-4::1MMDDTYYw...r!   c                H    | j                  |       t        | 	  ||      S )Nr0   )r   rG   r   r,   r   r1   rP   s      r"   r   zGridFlow.rows  s$    %w|D|..r!   c                F    | j                  |       t        | 	  ||      S r3   )r   rG   renderr   s      r"   r   zGridFlow.render  s$    
 	%w~dE**r!   c                D    | j                  |       t        | 	  |      S )zGet cursor from display widget.)r   rG   get_cursor_coordsr,   r   rP   s     r"   r   zGridFlow.get_cursor_coords  s!    %w(..r!   c                l    | j                  |       t        | 	  |||      }| j                          |S )z/Set the widget in focus based on the col + row.)r   rG   move_cursor_to_coordsr   )r,   r   colrowrvalrP   s        r"   r   zGridFlow.move_cursor_to_coords  s5    %w,T3<++-r!   c                p    | j                  |       t        | 	  ||||||       | j                          y)NT)r   rG   mouse_eventr   )r,   r   eventbuttonr   r   r1   rP   s          r"   r   zGridFlow.mouse_event  s9     	%D%c5A++-r!   c                D    | j                  |       t        | 	  |      S )z$Return pref col from display widget.)r   rG   get_pref_colr   s     r"   r   zGridFlow.get_pref_col  s!    %w#D))r!   )returnzfrozenset[Sizing]r3   )rJ   zIterable[Widget]rK   r   rB   r   rC   r   rD   zaLiteral['left', 'center', 'right'] | Align | tuple[Literal['relative', WHSettings.RELATIVE], int]r1   zint | Widget | Noner   None)r   z	list[str])r   zdict[str, typing.Any])r   z4Iterator[tuple[str | None, typing.Any] | typing.Any])r   r   )r   r   )re   ztuple[int, int, int]rf   zGIterable[tuple[Widget, tuple[Literal['given', WHSettings.GIVEN], int]]]r   r   )r|   zSequence[Widget])r   r   r   r   )r   zHMonitoredFocusList[tuple[Widget, tuple[Literal[WHSettings.GIVEN], int]]])r   z"Literal['given', WHSettings.GIVEN]r   
int | Noner   z%tuple[Literal[WHSettings.GIVEN], int])r   zWidget | intr   r   )r   zWidget | None)r   r   r   r   )r   r   )r   r   r   r   )r   tuple[int] | tuple[()]r   r   )r   r   r   zDivider | Pile)r   r   r   strr   z
str | None)r    F)r   r   r1   boolr   tuple[int, int])F)r   z
tuple[int]r1   r   r   r   )r   r   r1   r   )r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r1   r   r   zLiteral[True])r   r   )&r   r   r   r   r-   rH   rU   rX   r\   r^   r4   r@   propertyrJ   setterrK   rT   r	   r9   r   r   r1   r   r   rM   rE   r   rI   r   r   r   r   r   r   r   r   r   __classcell__)rP   s   @r"   r&   r&      s   
)  &*(N(N (N 	(N
 (N q(N #(N 
(NT9	9+#r"r [r 
	r  . \\1 1     ! !    __ 
 :D9I9I#'666 !6 
/	6" MD 5 55$ 	 	 M M # # ' '"0dM0$  
	& (*/$/ / 
	// +$+ +/
$  	
    
* *r!   r&   )'
__future__r   typingrx   urwid.split_reprr   columnsr   	constantsr   r   r	   	containerr
   r   r   r   monitored_listr   r   paddingr   piler   rO   r   r   r   r   TYPE_CHECKINGcollections.abcr   r   r   typing_extensionsr   r   r$   r&   r    r!   r"   <module>r      sr    "   ,  0 0 M  =   B B	<<)#K #%m %f*z$!57W f*r!   