WccsdZddlZddlZdZGddZedkr)dZejeZe de edSdS) z:Simple textbox editing widget with Emacs-like keybindings.Ncs|j|dz|tj||z dz |j||dztj||z dz |j||dztj||z dz |j|dz|tj||z dz |j ||tj |j ||tj|j ||tj|j ||tjdS)z^Draw a rectangle with corners at the provided upper-left and lower-right coordinates. N) ZvlinecursesZ ACS_VLINEZhlineZ ACS_HLINEaddchZ ACS_ULCORNERZ ACS_URCORNERZ ACS_LRCORNERZ ACS_LLCORNER)winulyulxZlryZlrx '/usr/lib64/python3.12/curses/textpad.py rectangler s IIc!eS&**C#IM::IIc3q5&**C#IM::IIc3q5&**C#IM::IIc!eS&**C#IM::IIc3++,,IIc3++,,IIc3++,,IIc3++,,,,cs@eZdZdZd dZdZdZdZdZdZ d d Z d S) TextboxadEditing widget using the interior of a window object. Supports the following Emacs-like key bindings: Ctrl-A Go to left edge of window. Ctrl-B Cursor left, wrapping to previous line if appropriate. Ctrl-D Delete character under cursor. Ctrl-E Go to right edge (stripspaces off) or end of line (stripspaces on). Ctrl-F Cursor right, wrapping to next line when appropriate. Ctrl-G Terminate, returning the window contents. Ctrl-H Delete character backward. Ctrl-J Terminate if the window is 1 line, otherwise insert newline. Ctrl-K If line is blank, delete it, otherwise clear to end of line. Ctrl-L Refresh screen. Ctrl-N Cursor down; move down one line. Ctrl-O Insert a blank line at cursor location. Ctrl-P Cursor up; move up one line. Move operations do nothing if the cursor is at an edge where the movement is not possible. The following synonyms are supported where possible: KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N KEY_BACKSPACE = Ctrl-h Fcs||_||_|jd|_d|_|j ddSNr)r insert_mode_update_max_yx stripspaceslastcmdZkeypad)selfrr  r__init__zTextbox.__init__+s@&   1 r csj|jj\}}|dz |_|dz |_dSr )rZgetmaxyxmaxymaxx)rrrrrrzTextbox._update_max_yx3s2XX&&( d1H 1H r csJ|j|j} tjj|jj ||tjj krt|j|dz} |S|dkr |S|dz })zuGo to the location of the first blank on the given line, returning the index of the last non-blank character.Trr)rrrasciirinchZSPZmin)ryZlastrr _end_of_linezTextbox._end_of_line8s yy||!!$((--4"89V\\__LL499d1f-  !8D r cs|j|jj\}}d}||jks||jkr|j r|jj } |jj||j r!tjjsnH|}|jj\}}|||f}||jkr||jkr||jj|dSdS#tj$rYwxYwN) rrgetyxrrr rrrZerrorrisprintmove)rchrxZbackyxZoldch r_insert_printable_charzTextbox._insert_printable_charFs0 !A$))mmq499}}  r""###6<<+?+?+F+FBXX^^%FQ~A!$))mmq499}}$   DHHMM6 " " " "  <<    s:D++EEcsj |j|jj\}}||_tj j |r4||jks||jkr|j|dS|tj jkr|jj|ddS|tj jtjtj jtjfvr|dkr!|jj||dz nq|dkrnj|j r4|jj|dz |j#|dz n*|jj|dz |j|tj jtjfvr|jj%dS|tj j&kr|jj%dS|tj j(krd|j r/|jj||j#|dS|jj||jdS|tj j*tj,fvrg||jkr"|jj||dzdS||jkr dS|jj|dzddS|tj j.krdS|tj j0krD|jdkrdS||jkr |jj|dzddS|tj j2krs|dkr3|j#|dkr|jj5dS|jj|||jj7dS|tj j8kr|jj;dS|tj j<tj>fvr~||jkrl|jj|dz|||j#|dzkr3|jj|dz|j#|dzdS|tj j@kr|jjCdS|tj jDtjFfvrr|dkrl|jj|dz |||j#|dz kr3|jj|dz |j#|dz dS)z!Process a single editing command.rr)$rrrrrrrrrr!ZSOHrZSTXZKEY_LEFTZBSZ KEY_BACKSPACErrZdelchZEOTZENQZACKZ KEY_RIGHTZBELZNLZVTZdeletelnZclrtoeolZFFrefreshZSOZKEY_DOWNZSIZinsertlnZDLEZKEY_UP)rrrrs r do_commandzTextbox.do_command_s% !A <<   # #499}}DII ++B//pqo6<<## # # HHMM!Q  lqkFLL$$V__flloofFZFZ[ [ [1uu a1%%%a!!! ac4#4#4QqS#9::: ac499--flloov';';<<<  VqU6<<## # # HHNN  RqQ6<<## # # a!2!21!566LqI a++HqGFLL$$f&6&67 7 7499}} a1%%BqAdii>q; ac1%%:q96<<## # #1 6<>q6<>qr c sd}|jt|jdzD]}|jj |d|j |}|dkr |j rEt|jdzD]`}|j r||krnK|ttjj|jj||z}b|jdks|dz}|S)z.Collect and return the contents of the window.Zrrz ) rZrangerrrrrrZchrrrr)rZresultrZstoprrrgatherzTextbox.gathers tyy{# ' 'A HHMM!Q  $$Q'DqyyT---499Q;' O O###DE#fll&8&8q!9L&M"NNyy1}}$ r Ncs |jj}|r ||}|s*|j|s |j S|jji)z2Edit in the widget window and collect the results.)rZgetchr#r"r$)rZvalidaterrreditz Textbox.editse!BxXb\2??2&&{{} HH    r )Fr) __name__Z __module__Z __qualname____doc__rrrr!r#r$r%r rr r s.   ###2???B"      r r Z__main__cs d\}}d\}}|j|dz |dtj||||}t||dz |dz ||z||z|j t |j S)N)i i)iiizUse Ctrl-G to end editing.r)ZaddstrrZnewwinr r"r r%)ZstdscrZncolsZnlinesrrrr r test_editboxr)s vS c!eS">??mmFE34&#a%Qf cEkBBs|  ""r zContents of text box:) r'rZ curses.asciir r r&r)ZwrapperZstrZprintZreprr(r rr*s@@  - - -iiiiiiiV z### &.. &C E !449----r