B °-_éã@sVdZddlZddlZdd„ZGdd„dƒZedkrRdd „Ze e¡Ze d e eƒƒdS) z:Simple textbox editing widget with Emacs-like keybindings.éNcCs¼| |d|tj||d¡| ||dtj||d¡| ||dtj||d¡| |d|tj||d¡| ||tj¡| ||tj¡| ||tj¡| ||tj ¡dS)z^Draw a rectangle with corners at the provided upper-left and lower-right coordinates. éN) ZvlineÚcursesZ ACS_VLINEZhlineZ ACS_HLINEÚaddchZ ACS_ULCORNERZ ACS_URCORNERZ ACS_LRCORNERZ ACS_LLCORNER)ÚwinÚulyÚulxZlryZlrx©rú$/usr/lib/python3.7/curses/textpad.pyÚ rectanglesr c@sLeZdZdZddd„Zdd„Zdd„Zd d „Zd d „Zd d„Z ddd„Z dS)Ú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 FcCs.||_||_| ¡d|_d|_| d¡dS)Nr)rÚ insert_modeÚ_update_max_yxÚ stripspacesÚlastcmdZkeypad)Úselfrr rrr Ú__init__+s zTextbox.__init__cCs&|j ¡\}}|d|_|d|_dS)Nr)rZgetmaxyxÚmaxyÚmaxx)rrrrrr r 3s zTextbox._update_max_yxcCs\| ¡|j}xHtj |j ||¡¡tjjkrBt|j|dƒ}Pn |dkrLP|d}qW|S)zuGo to the location of the first blank on the given line, returning the index of the last non-blank character.rr)r rrÚasciirÚinchZSPÚmin)rÚyZlastrrr Ú _end_of_line8s zTextbox._end_of_linecCsº| ¡|j ¡\}}d}x†||jks0||jkr |jr@|j ¡}y|j |¡Wntj k rfYnX|jrztj   |¡s|P|}|j ¡\}}|dkr||f}qW|dk r¶|jj |ŽdS)N) r rÚgetyxrrr rrrÚerrorrÚisprintÚmove)rÚchrÚxZbackyxZoldchrrr Ú_insert_printable_charFs$  zTextbox._insert_printable_charcCsV| ¡|j ¡\}}||_tj |¡rJ||jks<||jkrF|  |¡n|tjj krh|j  |d¡nê|tjj tj tjjtjfkr|dkr¢|j  ||d¡nB|dkr¬n8|jrÐ|j  |d| |d¡¡n|j  |d|j¡|tjjtjfkrR|j ¡nL|tjjkr"|j ¡n0|tjjkrb|jrN|j  || |¡¡n|j  ||j¡nð|tjjtjfkrº||jkr–|j  ||d¡n ||jkr¤n|j  |dd¡n˜|tjjkrÌdS|tjjkr |jdkrêdS||jkrR|j  |dd¡nF|tjjkrZ|dkr@| |¡dkr@|j ¡n|j  ||¡|j ¡nø|tjjkrt|j ¡nÞ|tjjtjfkrØ||jkrR|j  |d|¡|| |d¡krR|j  |d| |d¡¡nz|tjj krò|j !¡n`|tjj"tj#fkrR|dkrR|j  |d|¡|| |d¡krR|j  |d| |d¡¡dS)z!Process a single editing command.rr)$r rrrrrrrrrZSOHrZSTXZKEY_LEFTZBSZ KEY_BACKSPACErrZdelchZEOTZENQZACKZ KEY_RIGHTZBELÚNLZVTZdeletelnZclrtoeolZFFÚrefreshZSOZKEY_DOWNZSIZinsertlnZDLEZKEY_UP)rrrrrrr Ú do_command_sr            zTextbox.do_commandc Cs¨d}| ¡x–t|jdƒD]„}|j |d¡| |¡}|dkrH|jrHqxDt|jdƒD]2}|jrl||krlP|tt j   |j  ||¡¡ƒ}qXW|jdkr|d}qW|S)z.Collect and return the contents of the window.ÚrrÚ ) r ÚrangerrrrrrÚchrrrr)rÚresultrÚstoprrrr Úgather s "  zTextbox.gatherNcCs@x6|j ¡}|r||ƒ}|sq| |¡s*P|j ¡qW| ¡S)z2Edit in the widget window and collect the results.)rZgetchr"r!r))rZvalidaterrrr Úedit±s  z Textbox.edit)F)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__rr rrr"r)r*rrrr r s Ar Ú__main__cCsfd\}}d\}}| |d|d¡t ||||¡}t||d|d||||ƒ| ¡t|ƒ ¡S)N)é é)ééézUse Ctrl-G to end editing.r)ZaddstrrZnewwinr r!r r*)ZstdscrZncolsZnlinesrrrrrr Ú test_editbox¿s r5zContents of text box:) r.rZ curses.asciir r r+r5ÚwrapperÚstrÚprintÚreprrrrr Ús ,