WcdZddlZddlZdZGddZedkr)dZejeZe de edSdS) z:Simple textbox editing widget with Emacs-like keybindings.Nc|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) vlinecurses ACS_VLINEhline ACS_HLINEaddch ACS_ULCORNER ACS_URCORNER ACS_LRCORNER ACS_LLCORNER)winulyulxlrylrxs '/usr/lib64/python3.12/curses/textpad.py rectanglers IIc!eS&**C#IM::IIc3q5&**C#IM::IIc3q5&**C#IM::IIc!eS&**C#IM::IIc3++,,IIc3++,,IIc3++,,IIc3++,,,,c@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 Fc||_||_|jd|_d|_|j ddSNr)r insert_mode_update_max_yx stripspaceslastcmdkeypad)selfrrs r__init__zTextbox.__init__+s@&   1 rcj|jj\}}|dz |_|dz |_dSr)rgetmaxyxmaxymaxx)r r$r%s rrzTextbox._update_max_yx3s2XX&&( d1H 1H rcJ|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)rr%rasciirinchSPmin)r ylasts r _end_of_linezTextbox._end_of_line8s yy||!!$((--4"89V\\__LL499d1f-  !8D rc|j|jj\}}d}||jks||jkr|j r|jj } |jj||j r!tjjsnH|}|jj\}}|||f}||jkr||jkr||jj|dSdS#tj$rYwxYwN) rrgetyxr$r%rr(r rerrorr'isprintmove)r chr+xbackyxoldchs r_insert_printable_charzTextbox._insert_printable_charFs0 !A$))mmq499}}  r""###6<<+?+?+F+FBXX^^%FQ~A!$))mmq499}}$   DHHMM6 " " " "  <<    s:D++EEcj |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)$rrr0rrr'r2r$r%r8SOHr3STXKEY_LEFTBS KEY_BACKSPACErr-delchEOTENQACK KEY_RIGHTBELNLVTdeletelnclrtoeolFFrefreshSOKEY_DOWNSIinsertlnDLEKEY_UP)r r4r+r5s 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<>qrc d}|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.rr ) rranger$rr3r-rr%chrrr'r()r resultr+stopr5s rgatherzTextbox.gathers tyy{# ' 'A HHMM!Q  $$Q'DqyyT---499Q;' O O###DE#fll&8&8q!9L&M"NNyy1}}$ rNc |jj}|r ||}|s*|j|s |j S|jji)z2Edit in the widget window and collect the results.)rgetchrQrJrY)r validater4s reditz Textbox.editse!BxXb\2??2&&{{} HH    r)Fr/) __name__ __module__ __qualname____doc__r!rr-r8rQrYr]rrrrs.   ###2???B"      rr__main__c d\}}d\}}|j|dz |dtj||||}t||dz |dz ||z||z|j t |j S)N) )zUse Ctrl-G to end editing.r)addstrrnewwinrrJrr])stdscrncolsnlinesrrrs r test_editboxros vS c!eS">??mmFE34&#a%Qf cEkBBs|  ""rzContents of text box:) rar curses.asciirrr^rowrapperstrprintreprrbrrrus@@  - - -iiiiiiiV z### &.. &C E !449----r