| 1 |
"""Simple textbox editing widget with Emacs-like keybindings.""" |
| 2 |
|
| 3 |
import curses |
| 4 |
import curses.ascii |
| 5 |
|
| 6 |
def rectangle(win, uly, ulx, lry, lrx): |
| 7 |
"""Draw a rectangle with corners at the provided upper-left |
| 8 |
and lower-right coordinates. |
| 9 |
""" |
| 10 |
win.vline(uly+1, ulx, curses.ACS_VLINE, lry - uly - 1) |
| 11 |
win.hline(uly, ulx+1, curses.ACS_HLINE, lrx - ulx - 1) |
| 12 |
win.hline(lry, ulx+1, curses.ACS_HLINE, lrx - ulx - 1) |
| 13 |
win.vline(uly+1, lrx, curses.ACS_VLINE, lry - uly - 1) |
| 14 |
win.addch(uly, ulx, curses.ACS_ULCORNER) |
| 15 |
win.addch(uly, lrx, curses.ACS_URCORNER) |
| 16 |
win.addch(lry, lrx, curses.ACS_LRCORNER) |
| 17 |
win.addch(lry, ulx, curses.ACS_LLCORNER) |
| 18 |
|
| 19 |
class Textbox: |
| 20 |
"""Editing widget using the interior of a window object. |
| 21 |
Supports the following Emacs-like key bindings: |
| 22 |
|
| 23 |
Ctrl-A Go to left edge of window. |
| 24 |
Ctrl-B Cursor left, wrapping to previous line if appropriate. |
| 25 |
Ctrl-D Delete character under cursor. |
| 26 |
Ctrl-E Go to right edge (stripspaces off) or end of line |
| 27 |
(stripspaces on). |
| 28 |
Ctrl-F Cursor right, wrapping to next line when appropriate. |
| 29 |
Ctrl-G Terminate, returning the window contents. |
| 30 |
Ctrl-H Delete character backward. |
| 31 |
Ctrl-J Terminate if the window is 1 line, otherwise move to start |
| 32 |
of next line. |
| 33 |
Ctrl-K If line is blank, delete it, otherwise clear to end of line. |
| 34 |
Ctrl-L Refresh screen. |
| 35 |
Ctrl-N Cursor down; move down one line. |
| 36 |
Ctrl-O Insert a blank line at cursor location. |
| 37 |
Ctrl-P Cursor up; move up one line. |
| 38 |
|
| 39 |
Move operations do nothing if the cursor is at an edge where the |
| 40 |
movement is not possible. The following synonyms are supported where |
| 41 |
possible: |
| 42 |
|
| 43 |
KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, |
| 44 |
KEY_DOWN = Ctrl-N, KEY_BACKSPACE = Ctrl-h |
| 45 |
""" |
| 46 |
def __init__(self, win, insert_mode=False): |
| 47 |
self.win = win |
| 48 |
self.insert_mode = insert_mode |
| 49 |
self._update_max_yx() |
| 50 |
self.stripspaces = 1 |
| 51 |
self.lastcmd = None |
| 52 |
win.keypad(1) |
| 53 |
|
| 54 |
def _update_max_yx(self): |
| 55 |
maxy, maxx = self.win.getmaxyx() |
| 56 |
self.maxy = maxy - 1 |
| 57 |
self.maxx = maxx - 1 |
| 58 |
|
| 59 |
def _decode(self, ch): |
| 60 |
# The text of a chtype cell or input byte, decoded with the window's |
| 61 |
# encoding. A_CHARTEXT keeps the character byte, dropping the attributes. |
| 62 |
return bytes([ch & curses.A_CHARTEXT]).decode(self.win.encoding, 'replace') |
| 63 |
|
| 64 |
def _char_at(self, *yx): |
| 65 |
# The text of the cell at the given position (default: the cursor). |
| 66 |
# instr() re-encodes it to the window's encoding; inch() cannot |
| 67 |
# represent a non-ASCII 8-bit-locale character on a wide build. |
| 68 |
return self.win.instr(*yx, 1).decode(self.win.encoding, 'replace') |
| 69 |
|
| 70 |
def _cell_at(self, *yx): |
| 71 |
# The cell at the given position (default: the cursor) as a chtype |
| 72 |
# addch() can write back with its rendition. inch() mangles a non-ASCII |
| 73 |
# character on a wide build, so take the byte from instr() and the |
| 74 |
# attributes from inch(). |
| 75 |
return self.win.instr(*yx, 1)[0] | self.win.inch(*yx) & curses.A_ATTRIBUTES |
| 76 |
|
| 77 |
def _isprint(self, cell): |
| 78 |
# Whether a chtype cell holds a printable character; _decode() drops the |
| 79 |
# attribute bits. |
| 80 |
return self._decode(cell).isprintable() |
| 81 |
|
| 82 |
def _printable_key(self, ch): |
| 83 |
# Whether the integer keystroke is a printable character, not a key |
| 84 |
# code. 0..255 are character bytes (decoded with the window's encoding); |
| 85 |
# larger values are function and navigation keys. |
| 86 |
return ch <= 0xff and self._decode(ch).isprintable() |
| 87 |
|
| 88 |
def _end_of_line(self, y): |
| 89 |
"""Go to the location of the first blank on the given line, |
| 90 |
returning the index of the last non-blank character.""" |
| 91 |
self._update_max_yx() |
| 92 |
last = self.maxx |
| 93 |
while True: |
| 94 |
if self._char_at(y, last) != ' ': |
| 95 |
last = min(self.maxx, last+1) |
| 96 |
break |
| 97 |
elif last == 0: |
| 98 |
break |
| 99 |
last = last - 1 |
| 100 |
return last |
| 101 |
|
| 102 |
def _insert_printable_char(self, ch): |
| 103 |
self._update_max_yx() |
| 104 |
(y, x) = self.win.getyx() |
| 105 |
backyx = None |
| 106 |
while True: |
| 107 |
if self.insert_mode: |
| 108 |
oldch = self._cell_at() |
| 109 |
if y >= self.maxy and x >= self.maxx: |
| 110 |
# Use insch() in the lower-right cell: addch() there would move |
| 111 |
# the cursor out of the window, raising an error and scrolling |
| 112 |
# a scrollable window. Pass it as text: insch() does not decode |
| 113 |
# an int byte through the locale on a wide build. |
| 114 |
self.win.insch(self._decode(ch), ch & curses.A_ATTRIBUTES) |
| 115 |
break |
| 116 |
self.win.addch(ch) |
| 117 |
if not self.insert_mode or not self._isprint(oldch): |
| 118 |
break |
| 119 |
ch = oldch |
| 120 |
(y, x) = self.win.getyx() |
| 121 |
# Remember where to put the cursor back since we are in insert_mode |
| 122 |
if backyx is None: |
| 123 |
backyx = y, x |
| 124 |
|
| 125 |
if backyx is not None: |
| 126 |
self.win.move(*backyx) |
| 127 |
|
| 128 |
def do_command(self, ch): |
| 129 |
"Process a single editing command." |
| 130 |
self._update_max_yx() |
| 131 |
(y, x) = self.win.getyx() |
| 132 |
self.lastcmd = ch |
| 133 |
if self._printable_key(ch): |
| 134 |
self._insert_printable_char(ch) |
| 135 |
elif ch == curses.ascii.SOH: # ^a |
| 136 |
self.win.move(y, 0) |
| 137 |
elif ch in (curses.ascii.STX,curses.KEY_LEFT, |
| 138 |
curses.ascii.BS, |
| 139 |
curses.KEY_BACKSPACE, |
| 140 |
curses.ascii.DEL): |
| 141 |
if x > 0: |
| 142 |
self.win.move(y, x-1) |
| 143 |
elif y == 0: |
| 144 |
pass |
| 145 |
elif self.stripspaces: |
| 146 |
self.win.move(y-1, self._end_of_line(y-1)) |
| 147 |
else: |
| 148 |
self.win.move(y-1, self.maxx) |
| 149 |
if ch in (curses.ascii.BS, curses.KEY_BACKSPACE, curses.ascii.DEL): |
| 150 |
self.win.delch() |
| 151 |
elif ch == curses.ascii.EOT: # ^d |
| 152 |
self.win.delch() |
| 153 |
elif ch == curses.ascii.ENQ: # ^e |
| 154 |
if self.stripspaces: |
| 155 |
self.win.move(y, self._end_of_line(y)) |
| 156 |
else: |
| 157 |
self.win.move(y, self.maxx) |
| 158 |
elif ch in (curses.ascii.ACK, curses.KEY_RIGHT): # ^f |
| 159 |
if x < self.maxx: |
| 160 |
self.win.move(y, x+1) |
| 161 |
elif y == self.maxy: |
| 162 |
pass |
| 163 |
else: |
| 164 |
self.win.move(y+1, 0) |
| 165 |
elif ch == curses.ascii.BEL: # ^g |
| 166 |
return 0 |
| 167 |
elif ch == curses.ascii.NL: # ^j |
| 168 |
if self.maxy == 0: |
| 169 |
return 0 |
| 170 |
elif y < self.maxy: |
| 171 |
self.win.move(y+1, 0) |
| 172 |
elif ch == curses.ascii.VT: # ^k |
| 173 |
if x == 0 and self._end_of_line(y) == 0: |
| 174 |
self.win.deleteln() |
| 175 |
else: |
| 176 |
# first undo the effect of self._end_of_line |
| 177 |
self.win.move(y, x) |
| 178 |
self.win.clrtoeol() |
| 179 |
elif ch == curses.ascii.FF: # ^l |
| 180 |
self.win.refresh() |
| 181 |
elif ch in (curses.ascii.SO, curses.KEY_DOWN): # ^n |
| 182 |
if y < self.maxy: |
| 183 |
self.win.move(y+1, x) |
| 184 |
if x > self._end_of_line(y+1): |
| 185 |
self.win.move(y+1, self._end_of_line(y+1)) |
| 186 |
elif ch == curses.ascii.SI: # ^o |
| 187 |
self.win.insertln() |
| 188 |
elif ch in (curses.ascii.DLE, curses.KEY_UP): # ^p |
| 189 |
if y > 0: |
| 190 |
self.win.move(y-1, x) |
| 191 |
if x > self._end_of_line(y-1): |
| 192 |
self.win.move(y-1, self._end_of_line(y-1)) |
| 193 |
return 1 |
| 194 |
|
| 195 |
def gather(self): |
| 196 |
"Collect and return the contents of the window." |
| 197 |
result = "" |
| 198 |
self._update_max_yx() |
| 199 |
for y in range(self.maxy+1): |
| 200 |
self.win.move(y, 0) |
| 201 |
stop = self._end_of_line(y) |
| 202 |
if stop == 0 and self.stripspaces: |
| 203 |
continue |
| 204 |
for x in range(self.maxx+1): |
| 205 |
if self.stripspaces and x > stop: |
| 206 |
break |
| 207 |
result = result + self._char_at(y, x) |
| 208 |
if self.maxy > 0: |
| 209 |
result = result + "\n" |
| 210 |
return result |
| 211 |
|
| 212 |
def edit(self, validate=None): |
| 213 |
"Edit in the widget window and collect the results." |
| 214 |
while 1: |
| 215 |
ch = self.win.getch() |
| 216 |
if validate: |
| 217 |
ch = validate(ch) |
| 218 |
if not ch: |
| 219 |
continue |
| 220 |
if not self.do_command(ch): |
| 221 |
break |
| 222 |
self.win.refresh() |
| 223 |
return self.gather() |
| 224 |
|
| 225 |
if __name__ == '__main__': |
| 226 |
def test_editbox(stdscr): |
| 227 |
ncols, nlines = 9, 4 |
| 228 |
uly, ulx = 15, 20 |
| 229 |
stdscr.addstr(uly-2, ulx, "Use Ctrl-G to end editing.") |
| 230 |
win = curses.newwin(nlines, ncols, uly, ulx) |
| 231 |
rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols) |
| 232 |
stdscr.refresh() |
| 233 |
return Textbox(win).edit() |
| 234 |
|
| 235 |
str = curses.wrapper(test_editbox) |
| 236 |
print('Contents of text box:', repr(str)) |