Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b9d0bc

Browse files
committed
Added two modules for ASCII characters and a simple editing form (ESR)
1 parent 289d9d4 commit 2b9d0bc

2 files changed

Lines changed: 261 additions & 0 deletions

File tree

Lib/curses/ascii.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#
2+
# ascii.py -- constants and memembership tests for ASCII characters
3+
#
4+
5+
NUL = 0x00 # ^@
6+
SOH = 0x01 # ^A
7+
STX = 0x02 # ^B
8+
ETX = 0x03 # ^C
9+
EOT = 0x04 # ^D
10+
ENQ = 0x05 # ^E
11+
ACK = 0x06 # ^F
12+
BEL = 0x07 # ^G
13+
BS = 0x08 # ^H
14+
TAB = 0x09 # ^I
15+
HT = 0x09 # ^I
16+
LF = 0x0a # ^J
17+
NL = 0x0a # ^J
18+
VT = 0x0b # ^K
19+
FF = 0x0c # ^L
20+
CR = 0x0d # ^M
21+
SO = 0x0e # ^N
22+
SI = 0x0f # ^O
23+
DLE = 0x10 # ^P
24+
DC1 = 0x11 # ^Q
25+
DC2 = 0x12 # ^R
26+
DC3 = 0x13 # ^S
27+
DC4 = 0x14 # ^T
28+
NAK = 0x15 # ^U
29+
SYN = 0x16 # ^V
30+
ETB = 0x17 # ^W
31+
CAN = 0x18 # ^X
32+
EM = 0x19 # ^Y
33+
SUB = 0x1a # ^Z
34+
ESC = 0x1b # ^[
35+
FS = 0x1c # ^\
36+
GS = 0x1d # ^]
37+
RS = 0x1e # ^^
38+
US = 0x1f # ^_
39+
SP = 0x20 # space
40+
DEL = 0x7f # delete
41+
42+
controlnames = [
43+
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
44+
"BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
45+
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
46+
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
47+
"SP"
48+
]
49+
50+
def _ctoi(c):
51+
if type(c) == type(""):
52+
return ord(c)
53+
else:
54+
return c
55+
56+
def isalnum(c): return isalpha(c) or isdigit(c)
57+
def isalpha(c): return isupper(c) or islower(c)
58+
def isascii(c): return _ctoi(c) <= 127 # ?
59+
def isblank(c): return _ctoi(c) in (8,32)
60+
def iscntrl(c): return _ctoi(c) <= 31
61+
def isdigit(c): return _ctoi(c) >= 48 and _ctoi(c) <= 57
62+
def isgraph(c): return _ctoi(c) >= 33 and _ctoi(c) <= 126
63+
def islower(c): return _ctoi(c) >= 97 and _ctoi(c) <= 122
64+
def isprint(c): return _ctoi(c) >= 32 and _ctoi(c) <= 126
65+
def ispunct(c): return _ctoi(c) != 32 and not isalnum(c)
66+
def isspace(c): return _ctoi(c) in (12, 10, 13, 9, 11)
67+
def isupper(c): return _ctoi(c) >= 65 and _ctoi(c) <= 90
68+
def isxdigit(c): return isdigit(c) or \
69+
(_ctoi(c) >= 65 and _ctoi(c) <= 70) or (_ctoi(c) >= 97 and _ctoi(c) <= 102)
70+
def isctrl(c): return _ctoi(c) < 32
71+
def ismeta(c): return _ctoi(c) > 127
72+
73+
def ascii(c):
74+
if type(c) == type(""):
75+
return chr(_ctoi(c) & 0x7f)
76+
else:
77+
return _ctoi(c) & 0x7f
78+
79+
def ctrl(c):
80+
if type(c) == type(""):
81+
return chr(_ctoi(c) & 0x1f)
82+
else:
83+
return _ctoi(c) & 0x1f
84+
85+
def alt(c):
86+
if type(c) == type(""):
87+
return chr(_ctoi(c) | 0x80)
88+
else:
89+
return _ctoi(c) | 0x80
90+
91+
def unctrl(c):
92+
bits = _ctoi(c)
93+
if bits == 0x7f:
94+
rep = "^?"
95+
elif bits & 0x20:
96+
rep = chr((bits & 0x7f) | 0x20)
97+
else:
98+
rep = "^" + chr(((bits & 0x7f) | 0x20) + 0x20)
99+
if bits & 0x80:
100+
return "!" + rep
101+
return rep
102+

Lib/curses/textpad.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
"""curses.textpad
2+
3+
"""
4+
5+
import sys, curses, ascii
6+
7+
def rectangle(win, uly, ulx, lry, lrx):
8+
"Draw a rectangle."
9+
win.vline(uly+1, ulx, curses.ACS_VLINE, lry - uly - 1)
10+
win.hline(uly, ulx+1, curses.ACS_HLINE, lrx - ulx - 1)
11+
win.hline(lry, ulx+1, curses.ACS_HLINE, lrx - ulx - 1)
12+
win.vline(uly+1, lrx, curses.ACS_VLINE, lry - uly - 1)
13+
win.addch(uly, ulx, curses.ACS_ULCORNER)
14+
win.addch(uly, lrx, curses.ACS_URCORNER)
15+
win.addch(lry, lrx, curses.ACS_LRCORNER)
16+
win.addch(lry, ulx, curses.ACS_LLCORNER)
17+
18+
class textbox:
19+
"""Editing widget using the interior of a window object.
20+
Supports the following Emacs-like key bindings:
21+
22+
Ctrl-A Go to left edge of window.
23+
Ctrl-B Cursor left, wrapping to previous line if appropriate.
24+
Ctrl-D Delete character under cursor.
25+
Ctrl-E Go to right edge (nospaces off) or end of line (nospaces on).
26+
Ctrl-F Cursor right, wrapping to next line when appropriate.
27+
Ctrl-G Terminate, returning the window contents.
28+
Ctrl-J Terminate if the window is 1 line, otherwise insert newline.
29+
Ctrl-K If line is blank, delete it, otherwise clear to end of line.
30+
Ctrl-L Refresh screen
31+
Ctrl-N Cursor down; move down one line.
32+
Ctrl-O Insert a blank line at cursor location.
33+
Ctrl-P Cursor up; move up one line.
34+
35+
Move operations do nothing if the cursor is at an edge where the movement
36+
is not possible. The following synonyms are supported where possible:
37+
38+
KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N
39+
"""
40+
def __init__(self, win):
41+
self.win = win
42+
(self.maxy, self.maxx) = win.getmaxyx()
43+
self.maxy = self.maxy - 1
44+
self.maxx = self.maxx - 1
45+
self.stripspaces = 1
46+
win.keypad(1)
47+
48+
def firstblank(self, y):
49+
"Go to the location of the first blank on the given line."
50+
(oldy, oldx) = self.win.getyx()
51+
self.win.move(y, self.maxx-1)
52+
last = self.maxx-1
53+
while 1:
54+
if last == 0:
55+
break
56+
if ascii.ascii(self.win.inch(y, last)) != ascii.SP:
57+
last = last + 1
58+
break
59+
last = last - 1
60+
self.win.move(oldy, oldx)
61+
return last
62+
63+
def do_command(self, ch):
64+
"Process a single editing command."
65+
(y, x) = self.win.getyx()
66+
if ascii.isprint(ch):
67+
if y < self.maxy or x < self.maxx:
68+
# The try-catch ignores the error we trigger from some curses
69+
# versions by trying to write into the lowest-rightmost spot
70+
# in the self.window.
71+
try:
72+
self.win.addch(ch)
73+
except ERR:
74+
pass
75+
elif ch == ascii.SOH: # Ctrl-a
76+
self.win.move(y, 0)
77+
elif ch in (ascii.STX, curses.KEY_LEFT): # Ctrl-b
78+
if x > 0:
79+
self.win.move(y, x-1)
80+
elif y == 0:
81+
pass
82+
elif self.stripspaces:
83+
self.win.move(y-1, self.firstblank(y-1))
84+
else:
85+
self.win.move(y-1, self.maxx)
86+
elif ch == ascii.EOT: # Ctrl-d
87+
self.win.delch()
88+
elif ch == ascii.ENQ: # Ctrl-e
89+
if self.stripspaces:
90+
self.win.move(y, self.firstblank(y, maxx))
91+
else:
92+
self.win.move(y, self.maxx)
93+
elif ch in (ascii.ACK, curses.KEY_RIGHT): # Ctrl-f
94+
if x < self.maxx:
95+
self.win.move(y, x+1)
96+
elif y == self.maxx:
97+
pass
98+
else:
99+
self.win.move(y+1, 0)
100+
elif ch == ascii.BEL: # Ctrl-g
101+
return 0
102+
elif ch == ascii.NL: # Ctrl-j
103+
if self.maxy == 0:
104+
return 0
105+
elif y < self.maxy:
106+
self.win.move(y+1, 0)
107+
elif ch == ascii.VT: # Ctrl-k
108+
if x == 0 and self.firstblank(y) == 0:
109+
self.win.deleteln()
110+
else:
111+
self.win.clrtoeol()
112+
elif ch == ascii.FF: # Ctrl-l
113+
self.win.refresh()
114+
elif ch in (ascii.SO, curses.KEY_DOWN): # Ctrl-n
115+
if y < self.maxy:
116+
self.win.move(y+1, x)
117+
elif ch == ascii.SI: # Ctrl-o
118+
self.win.insertln()
119+
elif ch in (ascii.DLE, curses.KEY_UP): # Ctrl-p
120+
if y > 0:
121+
self.win.move(y-1, x)
122+
self.win.refresh()
123+
return 1
124+
125+
def gather(self):
126+
"Collect and return the contents of the window."
127+
result = ""
128+
for y in range(self.maxy+1):
129+
self.win.move(y, 0)
130+
stop = self.firstblank(y)
131+
if stop == 0 and self.stripspaces:
132+
continue
133+
for x in range(self.maxx+1):
134+
if self.stripspaces and x == stop:
135+
break
136+
result = result + chr(ascii.ascii(self.win.inch(y, x)))
137+
if self.maxy > 0:
138+
result = result + "\n"
139+
return result
140+
141+
def edit(self, validate=None):
142+
"Edit in the widget window and collect the results."
143+
while 1:
144+
ch = self.win.getch()
145+
if validate:
146+
ch = validate(ch)
147+
if not self.do_command(ch):
148+
break
149+
return self.gather()
150+
151+
if __name__ == '__main__':
152+
def test_editbox(stdscr):
153+
win = curses.newwin(4, 9, 15, 20)
154+
rectangle(stdscr, 14, 19, 19, 29)
155+
stdscr.refresh()
156+
return textbox(win).edit()
157+
158+
str = curses.wrapper(test_editbox)
159+
print str

0 commit comments

Comments
 (0)