
    jj\                       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	 d dl
mZ d dlmZmZmZ d dlmZ d	d
lmZmZmZ d	dlmZmZ ej2                  rd dlmZ d dlmZ d dlmZ  G d de      Z G d de      Z  G d de       Z!y)    )annotationsN)text_layout)CompositeCanvas)Command)remove_defaults)is_wide_charmove_next_charmove_prev_char)decompose_tagmarkup   )AlignSizingWrapMode)Text	TextError)Hashable)Literal)
TextCanvasc                      e Zd Zy)	EditErrorN)__name__
__module____qualname__     Z/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/widget/edit.pyr   r      s    r   r   c            	          e Zd ZU dZ eej                  g      ZdZdZ	ddgZ
ded<   d&dZd	d	dej                  ej                   dd
d
d
f		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d' fdZd( fdZd) fdZd*dZd+dZd,dZd-dZed.d       Zd/dZ ed ed      Zd0dZd1dZd.dZ eeed      Zd1dZ d2dZ!d3dZ"	 	 	 	 	 	 d4dZ#	 	 	 	 	 	 	 	 d5dZ$	 	 	 	 	 	 	 	 	 	 	 	 	 	 d6d Z%d7d!Z&	 d8	 	 	 	 	 d9 fd"Z'	 d:	 	 	 	 	 d; fd#Z(d<d$Z)d=d%Z* xZ+S )>Edita  
    Text editing widget implements cursor movement, text insertion and
    deletion.  A caption may prefix the editing area.  Uses text class
    for text layout.

    Users of this class may listen for ``"change"`` or ``"postchange"``
    events.  See :func:``connect_signal``.

    * ``"change"`` is sent just before the value of edit_text changes.
      It receives the new text as an argument.  Note that ``"change"`` cannot
      change the text in question as edit_text changes the text afterwards.
    * ``"postchange"`` is sent after the value of edit_text changes.
      It receives the old value of the text as an argument and thus is
      appropriate for changing the text.  It is possible for a ``"postchange"``
      event handler to get into a loop of changing the text and then being
      called when the event is re-emitted.  It is up to the event
      handler to guard against this case (for instance, by not changing the
      text if it is signaled for text that it has already changed once).
    TFchange
postchangeztyping.ClassVar[list[str]]signalsc                Z    t        |d      xs t        |      dk(  xr t        |      dk\  S )z
        Filter for text that may be entered into this widget by the user

        :param ch: character to be inserted
        :type ch: str

        This implementation returns True for all printable characters.
        r   r       )r   lenordselfchs     r   
valid_charzEdit.valid_char7   s+     B"Fs2w!|'EB2Fr    Nc
                .   t         
|   d|||       || _        || _        d| _        t        |      \  | _        | _        d| _        d| _	        | j                  |       |t        |      }| j                  |       | j                  |	       d| _        y)a  
        :param caption: markup for caption preceding edit_text, see
                        :class:`Text` for description of text markup.
        :type caption: text markup
        :param edit_text: initial text for editing, type (bytes or unicode)
                          must match the text in the caption
        :type edit_text: bytes or unicode
        :param multiline: True: 'enter' inserts newline  False: return it
        :type multiline: bool
        :param align: typically 'left', 'center' or 'right'
        :type align: text alignment mode
        :param wrap: typically 'space', 'any' or 'clip'
        :type wrap: text wrapping mode
        :param allow_tab: True: 'tab' inserts 1-8 spaces  False: return it
        :type allow_tab: bool
        :param edit_pos: initial position for cursor, None:end of edit_text
        :type edit_pos: int
        :param layout: defaults to a shared :class:`StandardTextLayout` instance
        :type layout: text layout instance
        :param mask: hide text entered with this character, None:disable mask
        :type mask: bytes or unicode

        >>> Edit()
        <Edit selectable flow widget '' edit_pos=0>
        >>> Edit("Y/n? ", "yes")
        <Edit selectable flow widget 'yes' caption='Y/n? ' edit_pos=3>
        >>> Edit("Name ", "Smith", edit_pos=1)
        <Edit selectable flow widget 'Smith' caption='Name ' edit_pos=1>
        >>> Edit("", "3.14", align="right")
        <Edit selectable flow widget '3.14' align='right' edit_pos=4>
        r*   r   NF)super__init__	multiline	allow_tab	_edit_posr   _caption_attrib
_edit_text	highlightset_edit_textr$   set_edit_posset_mask_shift_view_to_cursor)r'   caption	edit_textr.   alignwrapr/   edit_poslayoutmask	__class__s             r   r-   zEdit.__init__B   s    X 	UD&1""&9'&B#t|159%9~H(#d%*"r   c                    t         |          d d t        | j                        gz   d| j                  gt        | j                        z  z   dg| j                  du z  z   S )Nzcaption=r.   T)r,   _repr_wordsreprr3   r1   boolr.   )r'   r@   s    r   rC   zEdit._repr_words|   sn    G!#2&DOO$%&$--*+,tDMM/BBC mt~~567	
r   c                t    i t         |          d| j                  i}t        |t        j
                        S )Nr=   )r,   _repr_attrsr0   r   r   r-   )r'   attrsr@   s     r   rG   zEdit._repr_attrs   s1    E57&(E*dnnEudmm44r   c                    | j                   %| j                  | j                  z   | j                  fS | j                  | j                   t	        | j                        z  z   | j                  fS )a  
        Returns ``(text, display attributes)``. See :meth:`Text.get_text`
        for details.

        Text returned includes the caption and edit_text, possibly masked.

        >>> Edit("What? ", "oh, nothing.").get_text()
        ('What? oh, nothing.', [])
        >>> Edit(("bright", "user@host:~$ "), "ls").get_text()
        ('user@host:~$ ls', [('bright', 13)])
        >>> Edit("password:", "seekrit", mask="*").get_text()
        ('password:*******', [])
        )_maskr1   r3   r2   r$   r'   s    r   get_textzEdit.get_text   sT     ::==4??2DLL@@}}

S-A ABDLLPPr   c                J    t        | d      s|dk(  rd| _        yt        d      )z
        Not supported by Edit widget.

        >>> Edit().set_text("test")
        Traceback (most recent call last):
        EditError: set_text() not supported.  Use set_caption() or set_edit_text() instead.
        _textr*   NzHset_text() not supported.  Use set_caption() or set_edit_text() instead.)hasattrrN   r   )r'   markups     r   set_textzEdit.set_text   s*     tW%&B,DJbccr   c                `    |\  }| j                   \  }}||k7  r| j                  |f      d   S |S )a  
        Return the preferred column for the cursor, or the
        current cursor x value.  May also return ``'left'`` or ``'right'``
        to indicate the leftmost or rightmost column available.

        This method is used internally and by other widgets when
        moving the cursor up or down between widgets so that the
        column selected is one that the user would expect.

        >>> size = (10,)
        >>> Edit().get_pref_col(size)
        0
        >>> e = Edit("", "word")
        >>> e.get_pref_col(size)
        4
        >>> e.keypress(size, "left")
        >>> e.get_pref_col(size)
        3
        >>> e.keypress(size, "end")
        >>> e.get_pref_col(size)
        <Align.RIGHT: 'right'>
        >>> e = Edit("", "2\nwords")
        >>> e.keypress(size, "left")
        >>> e.keypress(size, "up")
        >>> e.get_pref_col(size)
        4
        >>> e.keypress(size, "left")
        >>> e.get_pref_col(size)
        0
        r   )pref_col_maxcolget_cursor_coords)r'   sizemaxcolpref_colthen_maxcols        r   get_pref_colzEdit.get_pref_col   s@    > 	 $ 4 4+& ))6)4Q77r   c                T    t        |      \  | _        | _        | j                          y)au  
        Set the caption markup for this widget.

        :param caption: markup for caption preceding edit_text, see
                        :meth:`Text.__init__` for description of text markup.

        >>> e = Edit("")
        >>> e.set_caption("cap1")
        >>> print(e.caption)
        cap1
        >>> e.set_caption(("bold", "cap2"))
        >>> print(e.caption)
        cap2
        >>> e.attrib
        [('bold', 4)]
        >>> e.caption = "cap3"  # not supported because caption stores text but set_caption() takes markup
        Traceback (most recent call last):
        AttributeError: can't set attribute
        N)r   r1   r2   _invalidate)r'   r9   s     r   set_captionzEdit.set_caption   s#    ( ':'&B#t|r   c                    | j                   S )zK
        Read-only property returning the caption for this widget.
        )r1   rK   s    r   r9   zEdit.caption   s    
 }}r   c                    t        t        |d      t        | j                              }d| _        d| _        || _        | j                          y)a  
        Set the cursor position with a self.edit_text offset.
        Clips pos to [0, len(edit_text)].

        :param pos: cursor position
        :type pos: int

        >>> e = Edit("", "word")
        >>> e.edit_pos
        4
        >>> e.set_edit_pos(2)
        >>> e.edit_pos
        2
        >>> e.edit_pos = -1  # Urwid 0.9.9 or later
        >>> e.edit_pos
        0
        >>> e.edit_pos = 20
        >>> e.edit_pos
        4
        r   NNN)minmaxr$   r3   r4   rS   r0   r[   )r'   poss     r   r6   zEdit.set_edit_pos   sA    * #c1+s4??34)r   c                    | j                   S N)r0   rK   s    r   <lambda>zEdit.<lambda>  s
    T^^ r   zI
        Property controlling the edit position for this widget.
        )docc                2    || _         | j                          y)z
        Set the character for masking text away.

        :param mask: hide text entered with this character, None:disable mask
        :type mask: bytes or unicode
        N)rJ   r[   )r'   r?   s     r   r7   zEdit.set_mask  s     
r   c                
   | j                  |      }d| _        | j                  d|       | j                  }|| _        t	        | j
                  t        |            | _        | j                  d|       | j                          y)a  
        Set the edit text for this widget.

        :param text: text for editing, type (bytes or unicode)
                     must match the text in the caption
        :type text: bytes or unicode

        >>> e = Edit()
        >>> e.set_edit_text("yes")
        >>> print(e.edit_text)
        yes
        >>> e
        <Edit selectable flow widget 'yes' edit_pos=0>
        >>> e.edit_text = "no"  # Urwid 0.9.9 or later
        >>> print(e.edit_text)
        no
        Nr   r    )_normalize_to_captionr4   _emitr3   r`   r=   r$   r[   )r'   textold_texts      r   r5   zEdit.set_edit_text   si    $ ))$/

8T"??DMM3t95

<*r   c                    | j                   S )z
        Return the edit text for this widget.

        >>> e = Edit("What? ", "oh, nothing.")
        >>> print(e.get_edit_text())
        oh, nothing.
        >>> print(e.edit_text)
        oh, nothing.
        )r3   rK   s    r   get_edit_textzEdit.get_edit_text<  s     r   zE
        Property controlling the edit text for this widget.
        c                    | j                  |      }| j                  |      \  }}| j                  |       | j                  |       d| _        y)aI  
        Insert text at the cursor position and update cursor.
        This method is used by the keypress() method when inserting
        one or more characters into edit_text.

        :param text: text for inserting, type (bytes or unicode)
                     must match the text in the caption
        :type text: bytes or unicode

        >>> e = Edit("", "42")
        >>> e.insert_text(".5")
        >>> e
        <Edit selectable flow widget '42.5' edit_pos=4>
        >>> e.set_edit_pos(2)
        >>> e.insert_text("a")
        >>> print(e.edit_text)
        42a.5
        N)ri   insert_text_resultr5   r6   r4   )r'   rk   result_text
result_poss       r   insert_textzEdit.insert_textP  sL    & ))$/"&"9"9$"?Z;'*%r   c                    t        |t              }t        | j                  t              }||k(  r|S |r|j                  d      S |j	                  d      S )zIReturn text converted to the same type as self.caption (bytes or unicode)ascii)
isinstancestrr1   encodedecode)r'   rk   tucus       r   ri   zEdit._normalize_to_captioni  sK    c"s+8K;;w''{{7##r   c                   | j                  |      }| j                  r5| j                  \  }}| j                  d| | j                  |d }}||z   }|}n| j                  }| j                  }	 |d| |z   ||d z   }|t        |      z  }||fS # t        t
        f$ r@}t        t        | j                  ||f            j                  |j                        |d}~ww xY w)a!  
        Return result of insert_text(text) without actually performing the
        insertion.  Handy for pre-validation.

        :param text: text for inserting, type (bytes or unicode)
                     must match the text in the caption
        :type text: bytes or unicode
        N)ri   r4   r:   r=   
IndexError	TypeError
ValueErrorrD   with_traceback__traceback__r$   )	r'   rk   startstopbtextetextrq   rr   excs	            r   rp   zEdit.insert_text_results  s     ))$/>>..KE4>>&514>>$%3H5E%-KJ..KJ	s%kz2T9K
<TTK 	c$i
Z((	 I& 	sT4>>;"EFGVVWZWhWhiorr	ss   ,B C;CCc                   | j                   }| j                  |      rMt        |t              r+t        | j                  t              s|j                  d      }| j                  |       y|dk(  r3| j                  r'dd| j                   dz  z
  z  }| j                  |       y|dk(  r | j                  rd}| j                  |       y| j                  |   t        j                  k(  r0|dk(  r|S t        | j                  d|      }| j                  |       y| j                  |   t        j                  k(  rV|t!        | j                        k\  r|S t#        | j                  |t!        | j                              }| j                  |       y| j                  |   t        j$                  t        j&                  hv rzd| _        | j+                  |      \  }}| j-                  |      }|t/        |      | j                  |   t        j$                  k(  r|d	z  }n|d	z  }| j1                  |||      s|S y|d
k(  rd| _        | j5                         sh|dk(  r|S t        | j                  d|      }| j7                  | j                  d| | j                  | j                   d z          | j                  |       yy|dk(  rd| _        | j5                         s}|t!        | j                        k\  r|S t#        | j                  |t!        | j                              }| j7                  | j                  d| j                    | j                  |d z          yy| j                  |   t        j8                  t        j:                  hv rd| _        d| _        | j+                  |      \  }}| j                  |   t        j8                  k(  r"| j1                  |t<        j                  |       y| j1                  |t<        j                  |       y|S )a  
        Handle editing keystrokes, return others.

        >>> e, size = Edit(), (20,)
        >>> e.keypress(size, "x")
        >>> e.keypress(size, "left")
        >>> e.keypress(size, "1")
        >>> print(e.edit_text)
        1x
        >>> e.keypress(size, "backspace")
        >>> e.keypress(size, "end")
        >>> e.keypress(size, "2")
        >>> print(e.edit_text)
        x2
        >>> e.keypress(size, "shift f1")
        'shift f1'
        zutf-8Ntab    enter
r   r   	backspacer_   delete)r=   r)   rv   rw   r1   rx   rs   r/   r.   _command_mapr   LEFTr
   r:   r6   RIGHTr$   r	   UPDOWNr4   rT   rY   r   move_cursor_to_coordsrS   _delete_highlightedr5   MAX_LEFT	MAX_RIGHTr   )r'   rU   keyrb   _xyrW   s          r   keypresszEdit.keypress  s   , mm??3#s#Jt}}c,Jjj)S!%<DNNdmma/01CS!'>dnnCS!S!W\\1ax
 C8Cc"S!W]]2c$..))
 c$..6IJCc"S!gjj',,%??!DN**40EB((.H **
   %3QQ--dHa@
+#-D ++-!8J$T^^Q<""4>>$3#7$..:Y#YZ!!#&(?#-D ++-#dnn--J$T^^S#dnn:MN""4>>/DMM#BT^^TWTXEY#YZS!g&6&68I8I%JJ!DN#-D **40EB  %)9)99**4Q?  **4a@ 
r   c                   |\  }| j                  |      }| j                  |d      \  }}||k  s|t        |      k\  ryt        j                  | j                         d   |||      }t        t        |t        | j                        z
  d      t        | j                              }	|	| _
        ||f| _        | j                          y)a  
        Set the cursor position with (x,y) coordinates.
        Returns True if move succeeded, False otherwise.

        >>> size = (10,)
        >>> e = Edit("", "edit\ntext")
        >>> e.move_cursor_to_coords(size, 5, 0)
        True
        >>> e.edit_pos
        4
        >>> e.move_cursor_to_coords(size, 5, 3)
        False
        >>> e.move_cursor_to_coords(size, 0, 1)
        True
        >>> e.edit_pos
        5
        r   FT)get_line_translationposition_coordsr$   r   calc_posrL   r`   ra   r9   r:   r=   rS   r[   )
r'   rU   xr   rV   trans_top_xtop_yrb   e_poss
             r   r   zEdit.move_cursor_to_coords  s    . 	))&1,,VQ7u9SZ""4==?1#5uaCCc$,,//3S5HI &yr   c                4    |dk(  r| j                  |||      S y)z
        Move the cursor to the location clicked for button 1.

        >>> size = (20,)
        >>> e = Edit("", "words here")
        >>> e.mouse_event(size, "mouse press", 1, 2, 0, True)
        True
        >>> e.edit_pos
        2
        r   F)r   )r'   rU   eventbuttoncolrowfocuss          r   mouse_eventzEdit.mouse_event!  s#    & Q;--dC==r   c                    | j                   sy| j                   \  }}| j                  d| | j                  |d }}| j                  ||z          || _        d| _         y)zm
        Delete all highlighted text and update cursor position, if any
        text is highlighted.
        FNT)r4   r:   r5   r=   )r'   r   r   r   r   s        r   r   zEdit._delete_highlighted8  s]    
 ~~nnt~~fu-t~~de/Du55=)r   c                    t        |      | _        t        |   ||      }|r!t	        |      }| j                  |      |_        |S )a  
        Render edit widget and return canvas.  Include cursor when in
        focus.

        >>> edit = Edit("? ", "yes")
        >>> c = edit.render((10,), focus=True)
        >>> c.text
        [b'? yes     ']
        >>> c.cursor
        (5, 0)
        )rE   r8   r,   renderr   rT   cursor)r'   rU   r   canvr@   s       r   r   zEdit.renderF  sG      &*%["-2W^D%-H"4(D006DK r   c                   t         |   ||      }| j                  s|S | j                         \  }}t	        j
                  ||| j                  t        | j                        z         \  }}|dk  r*g |d | t	        j                  ||   |       g||dz   d  S ||k\  r0g |d | t	        j                  ||   ||z
  dz          g||dz   d  S |S )Nr   r   )
r,   r   r8   rL   r   calc_coordsr=   r$   r9   
shift_line)	r'   rV   tar   rk   _ignorer   r   r@   s	           r   r   zEdit.get_line_translationc  s   
 ,VR8))Lg&&tUDMMCDU4UV1q5r((qA267 q1uw  ;r((qQZ!^3DEF q1uw  r   c                P    |\  }d| _         | j                  || j                        S )z
        Return the (*x*, *y*) coordinates of cursor within widget.

        >>> Edit("? ", "yes").get_cursor_coords((10,))
        (5, 0)
        T)r8   r   r=   )r'   rU   rV   s      r   rT   zEdit.get_cursor_coords~  s*     	%)"##FDMM::r   c                    |t        | j                        z   }| j                  |      }t        j                  | j                         d   ||      \  }}||fS )zR
        Return (*x*, *y*) coordinates for an offset into self.edit_text.
        r   )r$   r9   r   r   r   rL   )r'   rV   rb   pr   r   r   s          r   r   zEdit.position_coords  sS    
 #dll##))&1&&t}}q'95!D1!tr   r(   rw   returnrE   )r9   =str | tuple[Hashable, str] | list[str | tuple[Hashable, str]]r:   rw   r.   rE   r;   z*Literal['left', 'center', 'right'] | Alignr<   z6Literal['space', 'any', 'clip', 'ellipsis'] | WrapModer/   rE   r=   z
int | Noner>   ztext_layout.TextLayoutr?   
str | Noner   None)r   z	list[str])r   zdict[str, typing.Any])r   z.tuple[str | bytes, list[tuple[Hashable, int]]])rP   r   r   r   )rU   
tuple[int]r   int)r9   r   r   r   )r   rw   )rb   r   r   r   )r?   r   r   r   )rk   rw   r   r   )rk   str | bytesr   r   )rk   rw   r   ztuple[str | bytes, int]rU   r   r   rw   r   r   )rU   r   r   z&int | Literal[Align.LEFT, Align.RIGHT]r   r   r   rE   )rU   r   r   rw   r   r   r   r   r   r   r   rE   r   zbool | None)r   rE   )F)rU   r   r   rE   r   zTextCanvas | CompositeCanvasrd   )rV   r   r   z5tuple[str | bytes, list[tuple[Hashable, int]]] | Noner   zAlist[list[tuple[int, int, int | bytes] | tuple[int, int | None]]])rU   r   r   tuple[int, int])rV   r   rb   r   r   r   ),r   r   r   __doc__	frozensetr   FLOW_sizing_selectableignore_focusr!   __annotations__r)   r   r   r   SPACEr-   rC   rG   rL   rQ   rY   r\   propertyr9   r6   r=   r7   r5   rn   r:   rs   ri   rp   r   r   r   r   r   r   rT   r   __classcell__r@   s   @r   r   r      sK   ( &GKL+3\*BG'B	G RT<AJJGO~~#)-8+N8+ 8+ 	8+
 :8+ E8+ 8+ 8+ '8+ 8+ 
8+t
5Q(d$$L.  6 #H	8
 I2$):kk k 
	kZ"" 2" 	"
 
"H  	
    
."   
&	@ EI B 
K	6
;r   r   c                  N     e Zd ZdZddZdd fdZ	 	 	 	 	 	 d	 fdZd
dZ xZS )IntEditzEdit widget for integer valuesc                F    t        |      dk(  xr |t        j                  v S )z1
        Return true for decimal digits.
        r   )r$   stringdigitsr&   s     r   r)   zIntEdit.valid_char  s      2w!|3fmm 33r   c                F    |t        |      }nd}t        | 	  ||       y)z
        caption -- caption markup
        default -- default edit value

        >>> IntEdit("", 42)
        <IntEdit selectable flow widget '42' edit_pos=2>
        Nr*   )rw   r,   r-   )r'   r9   defaultvalr@   s       r   r-   zIntEdit.__init__  s(     g,CC#&r   c                ,   t         |   ||      x}r|S | j                  dkD  rp| j                  dd dk(  r^| j	                  | j                  dz
         | j                  | j                  dd        | j                  dkD  r| j                  dd dk(  r^y)a7  
        Handle editing keystrokes.  Remove leading zeros.

        >>> e, size = IntEdit("", 5002), (10,)
        >>> e.keypress(size, "home")
        >>> e.keypress(size, "delete")
        >>> print(e.edit_text)
        002
        >>> e.keypress(size, "end")
        >>> print(e.edit_text)
        2
        r   Nr   0)r,   r   r=   r:   r6   r5   )r'   rU   r   	unhandledr@   s       r   r   zIntEdit.keypress  s    " (s3393 mmaDNN2A$6#$=dmma/0t~~ab12 mmaDNN2A$6#$= r   c                F    | j                   rt        | j                         S y)z
        Return the numeric value of self.edit_text.

        >>> e, size = IntEdit(), (10,)
        >>> e.keypress(size, "5")
        >>> e.keypress(size, "1")
        >>> e.value() == 51
        True
        r   )r:   r   rK   s    r   valuezIntEdit.value  s     >>t~~&&r   r   )r*   N)r   zint | str | Noner   r   r   )r   r   )	r   r   r   r   r)   r-   r   r   r   r   s   @r   r   r     s6    (4'  
	6r   r   )"
__future__r   r   typingurwidr   urwid.canvasr   urwid.command_mapr   urwid.split_reprr   urwid.str_utilr   r	   r
   
urwid.utilr   	constantsr   r   r   rk   r   r   TYPE_CHECKINGcollections.abcr   typing_extensionsr   r   r   r   r   r   r   r   <module>r      se    "    ( % , G G * . . !	()'		 	v	4 v	r?d ?r   