1 | n/a | """Simple textbox editing widget with Emacs-like keybindings.""" |
---|
2 | n/a | |
---|
3 | n/a | import curses |
---|
4 | n/a | import curses.ascii |
---|
5 | n/a | |
---|
6 | n/a | def rectangle(win, uly, ulx, lry, lrx): |
---|
7 | n/a | """Draw a rectangle with corners at the provided upper-left |
---|
8 | n/a | and lower-right coordinates. |
---|
9 | n/a | """ |
---|
10 | n/a | win.vline(uly+1, ulx, curses.ACS_VLINE, lry - uly - 1) |
---|
11 | n/a | win.hline(uly, ulx+1, curses.ACS_HLINE, lrx - ulx - 1) |
---|
12 | n/a | win.hline(lry, ulx+1, curses.ACS_HLINE, lrx - ulx - 1) |
---|
13 | n/a | win.vline(uly+1, lrx, curses.ACS_VLINE, lry - uly - 1) |
---|
14 | n/a | win.addch(uly, ulx, curses.ACS_ULCORNER) |
---|
15 | n/a | win.addch(uly, lrx, curses.ACS_URCORNER) |
---|
16 | n/a | win.addch(lry, lrx, curses.ACS_LRCORNER) |
---|
17 | n/a | win.addch(lry, ulx, curses.ACS_LLCORNER) |
---|
18 | n/a | |
---|
19 | n/a | class Textbox: |
---|
20 | n/a | """Editing widget using the interior of a window object. |
---|
21 | n/a | Supports the following Emacs-like key bindings: |
---|
22 | n/a | |
---|
23 | n/a | Ctrl-A Go to left edge of window. |
---|
24 | n/a | Ctrl-B Cursor left, wrapping to previous line if appropriate. |
---|
25 | n/a | Ctrl-D Delete character under cursor. |
---|
26 | n/a | Ctrl-E Go to right edge (stripspaces off) or end of line (stripspaces on). |
---|
27 | n/a | Ctrl-F Cursor right, wrapping to next line when appropriate. |
---|
28 | n/a | Ctrl-G Terminate, returning the window contents. |
---|
29 | n/a | Ctrl-H Delete character backward. |
---|
30 | n/a | Ctrl-J Terminate if the window is 1 line, otherwise insert newline. |
---|
31 | n/a | Ctrl-K If line is blank, delete it, otherwise clear to end of line. |
---|
32 | n/a | Ctrl-L Refresh screen. |
---|
33 | n/a | Ctrl-N Cursor down; move down one line. |
---|
34 | n/a | Ctrl-O Insert a blank line at cursor location. |
---|
35 | n/a | Ctrl-P Cursor up; move up one line. |
---|
36 | n/a | |
---|
37 | n/a | Move operations do nothing if the cursor is at an edge where the movement |
---|
38 | n/a | is not possible. The following synonyms are supported where possible: |
---|
39 | n/a | |
---|
40 | n/a | KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N |
---|
41 | n/a | KEY_BACKSPACE = Ctrl-h |
---|
42 | n/a | """ |
---|
43 | n/a | def __init__(self, win, insert_mode=False): |
---|
44 | n/a | self.win = win |
---|
45 | n/a | self.insert_mode = insert_mode |
---|
46 | n/a | self._update_max_yx() |
---|
47 | n/a | self.stripspaces = 1 |
---|
48 | n/a | self.lastcmd = None |
---|
49 | n/a | win.keypad(1) |
---|
50 | n/a | |
---|
51 | n/a | def _update_max_yx(self): |
---|
52 | n/a | maxy, maxx = self.win.getmaxyx() |
---|
53 | n/a | self.maxy = maxy - 1 |
---|
54 | n/a | self.maxx = maxx - 1 |
---|
55 | n/a | |
---|
56 | n/a | def _end_of_line(self, y): |
---|
57 | n/a | """Go to the location of the first blank on the given line, |
---|
58 | n/a | returning the index of the last non-blank character.""" |
---|
59 | n/a | self._update_max_yx() |
---|
60 | n/a | last = self.maxx |
---|
61 | n/a | while True: |
---|
62 | n/a | if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP: |
---|
63 | n/a | last = min(self.maxx, last+1) |
---|
64 | n/a | break |
---|
65 | n/a | elif last == 0: |
---|
66 | n/a | break |
---|
67 | n/a | last = last - 1 |
---|
68 | n/a | return last |
---|
69 | n/a | |
---|
70 | n/a | def _insert_printable_char(self, ch): |
---|
71 | n/a | self._update_max_yx() |
---|
72 | n/a | (y, x) = self.win.getyx() |
---|
73 | n/a | backyx = None |
---|
74 | n/a | while y < self.maxy or x < self.maxx: |
---|
75 | n/a | if self.insert_mode: |
---|
76 | n/a | oldch = self.win.inch() |
---|
77 | n/a | # The try-catch ignores the error we trigger from some curses |
---|
78 | n/a | # versions by trying to write into the lowest-rightmost spot |
---|
79 | n/a | # in the window. |
---|
80 | n/a | try: |
---|
81 | n/a | self.win.addch(ch) |
---|
82 | n/a | except curses.error: |
---|
83 | n/a | pass |
---|
84 | n/a | if not self.insert_mode or not curses.ascii.isprint(oldch): |
---|
85 | n/a | break |
---|
86 | n/a | ch = oldch |
---|
87 | n/a | (y, x) = self.win.getyx() |
---|
88 | n/a | # Remember where to put the cursor back since we are in insert_mode |
---|
89 | n/a | if backyx is None: |
---|
90 | n/a | backyx = y, x |
---|
91 | n/a | |
---|
92 | n/a | if backyx is not None: |
---|
93 | n/a | self.win.move(*backyx) |
---|
94 | n/a | |
---|
95 | n/a | def do_command(self, ch): |
---|
96 | n/a | "Process a single editing command." |
---|
97 | n/a | self._update_max_yx() |
---|
98 | n/a | (y, x) = self.win.getyx() |
---|
99 | n/a | self.lastcmd = ch |
---|
100 | n/a | if curses.ascii.isprint(ch): |
---|
101 | n/a | if y < self.maxy or x < self.maxx: |
---|
102 | n/a | self._insert_printable_char(ch) |
---|
103 | n/a | elif ch == curses.ascii.SOH: # ^a |
---|
104 | n/a | self.win.move(y, 0) |
---|
105 | n/a | elif ch in (curses.ascii.STX,curses.KEY_LEFT, curses.ascii.BS,curses.KEY_BACKSPACE): |
---|
106 | n/a | if x > 0: |
---|
107 | n/a | self.win.move(y, x-1) |
---|
108 | n/a | elif y == 0: |
---|
109 | n/a | pass |
---|
110 | n/a | elif self.stripspaces: |
---|
111 | n/a | self.win.move(y-1, self._end_of_line(y-1)) |
---|
112 | n/a | else: |
---|
113 | n/a | self.win.move(y-1, self.maxx) |
---|
114 | n/a | if ch in (curses.ascii.BS, curses.KEY_BACKSPACE): |
---|
115 | n/a | self.win.delch() |
---|
116 | n/a | elif ch == curses.ascii.EOT: # ^d |
---|
117 | n/a | self.win.delch() |
---|
118 | n/a | elif ch == curses.ascii.ENQ: # ^e |
---|
119 | n/a | if self.stripspaces: |
---|
120 | n/a | self.win.move(y, self._end_of_line(y)) |
---|
121 | n/a | else: |
---|
122 | n/a | self.win.move(y, self.maxx) |
---|
123 | n/a | elif ch in (curses.ascii.ACK, curses.KEY_RIGHT): # ^f |
---|
124 | n/a | if x < self.maxx: |
---|
125 | n/a | self.win.move(y, x+1) |
---|
126 | n/a | elif y == self.maxy: |
---|
127 | n/a | pass |
---|
128 | n/a | else: |
---|
129 | n/a | self.win.move(y+1, 0) |
---|
130 | n/a | elif ch == curses.ascii.BEL: # ^g |
---|
131 | n/a | return 0 |
---|
132 | n/a | elif ch == curses.ascii.NL: # ^j |
---|
133 | n/a | if self.maxy == 0: |
---|
134 | n/a | return 0 |
---|
135 | n/a | elif y < self.maxy: |
---|
136 | n/a | self.win.move(y+1, 0) |
---|
137 | n/a | elif ch == curses.ascii.VT: # ^k |
---|
138 | n/a | if x == 0 and self._end_of_line(y) == 0: |
---|
139 | n/a | self.win.deleteln() |
---|
140 | n/a | else: |
---|
141 | n/a | # first undo the effect of self._end_of_line |
---|
142 | n/a | self.win.move(y, x) |
---|
143 | n/a | self.win.clrtoeol() |
---|
144 | n/a | elif ch == curses.ascii.FF: # ^l |
---|
145 | n/a | self.win.refresh() |
---|
146 | n/a | elif ch in (curses.ascii.SO, curses.KEY_DOWN): # ^n |
---|
147 | n/a | if y < self.maxy: |
---|
148 | n/a | self.win.move(y+1, x) |
---|
149 | n/a | if x > self._end_of_line(y+1): |
---|
150 | n/a | self.win.move(y+1, self._end_of_line(y+1)) |
---|
151 | n/a | elif ch == curses.ascii.SI: # ^o |
---|
152 | n/a | self.win.insertln() |
---|
153 | n/a | elif ch in (curses.ascii.DLE, curses.KEY_UP): # ^p |
---|
154 | n/a | if y > 0: |
---|
155 | n/a | self.win.move(y-1, x) |
---|
156 | n/a | if x > self._end_of_line(y-1): |
---|
157 | n/a | self.win.move(y-1, self._end_of_line(y-1)) |
---|
158 | n/a | return 1 |
---|
159 | n/a | |
---|
160 | n/a | def gather(self): |
---|
161 | n/a | "Collect and return the contents of the window." |
---|
162 | n/a | result = "" |
---|
163 | n/a | self._update_max_yx() |
---|
164 | n/a | for y in range(self.maxy+1): |
---|
165 | n/a | self.win.move(y, 0) |
---|
166 | n/a | stop = self._end_of_line(y) |
---|
167 | n/a | if stop == 0 and self.stripspaces: |
---|
168 | n/a | continue |
---|
169 | n/a | for x in range(self.maxx+1): |
---|
170 | n/a | if self.stripspaces and x > stop: |
---|
171 | n/a | break |
---|
172 | n/a | result = result + chr(curses.ascii.ascii(self.win.inch(y, x))) |
---|
173 | n/a | if self.maxy > 0: |
---|
174 | n/a | result = result + "\n" |
---|
175 | n/a | return result |
---|
176 | n/a | |
---|
177 | n/a | def edit(self, validate=None): |
---|
178 | n/a | "Edit in the widget window and collect the results." |
---|
179 | n/a | while 1: |
---|
180 | n/a | ch = self.win.getch() |
---|
181 | n/a | if validate: |
---|
182 | n/a | ch = validate(ch) |
---|
183 | n/a | if not ch: |
---|
184 | n/a | continue |
---|
185 | n/a | if not self.do_command(ch): |
---|
186 | n/a | break |
---|
187 | n/a | self.win.refresh() |
---|
188 | n/a | return self.gather() |
---|
189 | n/a | |
---|
190 | n/a | if __name__ == '__main__': |
---|
191 | n/a | def test_editbox(stdscr): |
---|
192 | n/a | ncols, nlines = 9, 4 |
---|
193 | n/a | uly, ulx = 15, 20 |
---|
194 | n/a | stdscr.addstr(uly-2, ulx, "Use Ctrl-G to end editing.") |
---|
195 | n/a | win = curses.newwin(nlines, ncols, uly, ulx) |
---|
196 | n/a | rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols) |
---|
197 | n/a | stdscr.refresh() |
---|
198 | n/a | return Textbox(win).edit() |
---|
199 | n/a | |
---|
200 | n/a | str = curses.wrapper(test_editbox) |
---|
201 | n/a | print('Contents of text box:', repr(str)) |
---|