- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtextpad.py
201 lines (186 loc) · 7.48 KB
/
textpad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Simple textbox editing widget with Emacs-like keybindings."""
importcurses
importcurses.ascii
defrectangle(win, uly, ulx, lry, lrx):
"""Draw a rectangle with corners at the provided upper-left
and lower-right coordinates.
"""
win.vline(uly+1, ulx, curses.ACS_VLINE, lry-uly-1)
win.hline(uly, ulx+1, curses.ACS_HLINE, lrx-ulx-1)
win.hline(lry, ulx+1, curses.ACS_HLINE, lrx-ulx-1)
win.vline(uly+1, lrx, curses.ACS_VLINE, lry-uly-1)
win.addch(uly, ulx, curses.ACS_ULCORNER)
win.addch(uly, lrx, curses.ACS_URCORNER)
win.addch(lry, lrx, curses.ACS_LRCORNER)
win.addch(lry, ulx, curses.ACS_LLCORNER)
classTextbox:
"""Editing 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
"""
def__init__(self, win, insert_mode=False):
self.win=win
self.insert_mode=insert_mode
self._update_max_yx()
self.stripspaces=1
self.lastcmd=None
win.keypad(1)
def_update_max_yx(self):
maxy, maxx=self.win.getmaxyx()
self.maxy=maxy-1
self.maxx=maxx-1
def_end_of_line(self, y):
"""Go to the location of the first blank on the given line,
returning the index of the last non-blank character."""
self._update_max_yx()
last=self.maxx
whileTrue:
ifcurses.ascii.ascii(self.win.inch(y, last)) !=curses.ascii.SP:
last=min(self.maxx, last+1)
break
eliflast==0:
break
last=last-1
returnlast
def_insert_printable_char(self, ch):
self._update_max_yx()
(y, x) =self.win.getyx()
backyx=None
whiley<self.maxyorx<self.maxx:
ifself.insert_mode:
oldch=self.win.inch()
# The try-catch ignores the error we trigger from some curses
# versions by trying to write into the lowest-rightmost spot
# in the window.
try:
self.win.addch(ch)
exceptcurses.error:
pass
ifnotself.insert_modeornotcurses.ascii.isprint(oldch):
break
ch=oldch
(y, x) =self.win.getyx()
# Remember where to put the cursor back since we are in insert_mode
ifbackyxisNone:
backyx=y, x
ifbackyxisnotNone:
self.win.move(*backyx)
defdo_command(self, ch):
"Process a single editing command."
self._update_max_yx()
(y, x) =self.win.getyx()
self.lastcmd=ch
ifcurses.ascii.isprint(ch):
ify<self.maxyorx<self.maxx:
self._insert_printable_char(ch)
elifch==curses.ascii.SOH: # ^a
self.win.move(y, 0)
elifchin (curses.ascii.STX,curses.KEY_LEFT, curses.ascii.BS,curses.KEY_BACKSPACE):
ifx>0:
self.win.move(y, x-1)
elify==0:
pass
elifself.stripspaces:
self.win.move(y-1, self._end_of_line(y-1))
else:
self.win.move(y-1, self.maxx)
ifchin (curses.ascii.BS, curses.KEY_BACKSPACE):
self.win.delch()
elifch==curses.ascii.EOT: # ^d
self.win.delch()
elifch==curses.ascii.ENQ: # ^e
ifself.stripspaces:
self.win.move(y, self._end_of_line(y))
else:
self.win.move(y, self.maxx)
elifchin (curses.ascii.ACK, curses.KEY_RIGHT): # ^f
ifx<self.maxx:
self.win.move(y, x+1)
elify==self.maxy:
pass
else:
self.win.move(y+1, 0)
elifch==curses.ascii.BEL: # ^g
return0
elifch==curses.ascii.NL: # ^j
ifself.maxy==0:
return0
elify<self.maxy:
self.win.move(y+1, 0)
elifch==curses.ascii.VT: # ^k
ifx==0andself._end_of_line(y) ==0:
self.win.deleteln()
else:
# first undo the effect of self._end_of_line
self.win.move(y, x)
self.win.clrtoeol()
elifch==curses.ascii.FF: # ^l
self.win.refresh()
elifchin (curses.ascii.SO, curses.KEY_DOWN): # ^n
ify<self.maxy:
self.win.move(y+1, x)
ifx>self._end_of_line(y+1):
self.win.move(y+1, self._end_of_line(y+1))
elifch==curses.ascii.SI: # ^o
self.win.insertln()
elifchin (curses.ascii.DLE, curses.KEY_UP): # ^p
ify>0:
self.win.move(y-1, x)
ifx>self._end_of_line(y-1):
self.win.move(y-1, self._end_of_line(y-1))
return1
defgather(self):
"Collect and return the contents of the window."
result=""
self._update_max_yx()
foryinrange(self.maxy+1):
self.win.move(y, 0)
stop=self._end_of_line(y)
ifstop==0andself.stripspaces:
continue
forxinrange(self.maxx+1):
ifself.stripspacesandx>stop:
break
result=result+chr(curses.ascii.ascii(self.win.inch(y, x)))
ifself.maxy>0:
result=result+"\n"
returnresult
defedit(self, validate=None):
"Edit in the widget window and collect the results."
while1:
ch=self.win.getch()
ifvalidate:
ch=validate(ch)
ifnotch:
continue
ifnotself.do_command(ch):
break
self.win.refresh()
returnself.gather()
if__name__=='__main__':
deftest_editbox(stdscr):
ncols, nlines=9, 4
uly, ulx=15, 20
stdscr.addstr(uly-2, ulx, "Use Ctrl-G to end editing.")
win=curses.newwin(nlines, ncols, uly, ulx)
rectangle(stdscr, uly-1, ulx-1, uly+nlines, ulx+ncols)
stdscr.refresh()
returnTextbox(win).edit()
str=curses.wrapper(test_editbox)
print('Contents of text box:', repr(str))