[@s~dZddlZddlZddZGdddZedkrzdd ZejeZe d e endS) z:Simple textbox editing widget with Emacs-like keybindings.NcCs|j|d|tj||d|j||dtj||d|j||dtj||d|j|d|tj||d|j||tj|j||tj|j||tj|j||tj dS)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)winulyulxZlryZlrxr$/usr/lib/python3.4/curses/textpad.py rectangles%%%%r c@sdeZdZdZdddZddZddZd d Zd d Zd ddZ 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 FcCsm||_||_|j\|_|_|jd|_|jd|_d|_d|_|jddS)Nr)r insert_modeZgetmaxyxmaxymaxx stripspaceslastcmdZkeypad)selfrr rrr __init__+s    zTextbox.__init__cCsu|j}xetjj|jj||tjjkrSt|j|d}Pn|dkrcPn|d}q W|S)zuGo to the location of the first blank on the given line, returning the index of the last non-blank character.rr)rrasciirinchZSPmin)rylastrrr _end_of_line5s - zTextbox._end_of_linec Cs|jj\}}||jks3||jkr|jrN|jj}ny|jj|Wntjk ryYnX|jr|jj\}}tj j |r|j ||jj ||qqndS)N) rgetyxr rr rrrerrorrisprint_insert_printable_charmove)rchrxZoldchZbackyZbackxrrr rBs   zTextbox._insert_printable_charcCse|jj\}}||_tjj|ra||jksN||jkra|j|qan|tjj kr|jj |dn|tjj tj tjj tjfkr`|dkr|jj ||dnY|dkrnJ|jr|jj |d|j|dn|jj |d|j|tjj tjfkra|jjqan|tjjkr|jjn|tjjkr|jr|jj ||j|qa|jj ||jn|tjjtjfkrE||jkr|jj ||dqa||jkr+qa|jj |ddn|tjjkr[dS|tjjkr|jdkrdS||jkra|jj |ddqan|tjjkr|dkr|j|dkr|jjqa|jj |||jjnO|tjjkr4|jjn-|tjjtjfkr||jkra|jj |d|||j|dkr|jj |d|j|dqqan|tjjkr|jj n|tjj!tj"fkra|dkra|jj |d|||j|dkr^|jj |d|j|dq^qandS)z!Process a single editing command.rr)#rrrrrrr rrZSOHrZSTXZKEY_LEFTZBSZ KEY_BACKSPACErrZdelchZEOTZENQZACKZ KEY_RIGHTZBELNLZVTZdeletelnZclrtoeolZFFrefreshZSOZKEY_DOWNZSIZinsertlnZDLEZKEY_UP)rrrrrrr do_commandTsp *   ' !- -zTextbox.do_commandcCsd}xt|jdD]}|jj|d|j|}|dkr]|jr]qnx_t|jdD]J}|jr||krPn|ttj j |jj ||}qqW|jdkr|d}qqW|S)z.Collect and return the contents of the window.rr ) ranger rrrrrchrrrr)rresultrstoprrrr gathers/zTextbox.gatherNcCsaxT|jj}|r'||}n|s3qn|j|sFPn|jjqW|jS)z2Edit in the widget window and collect the results.)rZgetchr"r!r))rZvalidaterrrr editsz Textbox.edit) __name__ __module__ __qualname____doc__rrrr"r)r*rrrr r s   @ r __main__cCsd\}}d \}}|j|d|dtj||||}t||d|d|||||jt|jS) N zUse Ctrl-G to end editing.r)r0r1)r2r3)ZaddstrrZnewwinr r!r r*)ZstdscrZncolsZnlinesrrrrrr test_editboxs  & r5zContents of text box:) r.rZ curses.asciir r r+r5wrapperstrprintreprrrrr s