30
30
unichr = chr
31
31
basestring = bytes , str
32
32
33
+
33
34
def _make_unctrl_map ():
34
35
uc_map = {}
35
36
for c in map (unichr , range (256 )):
@@ -38,54 +39,47 @@ def _make_unctrl_map():
38
39
for i in range (32 ):
39
40
c = unichr (i )
40
41
uc_map [c ] = '^' + unichr (ord ('A' ) + i - 1 )
41
- uc_map [b'\t ' ] = ' ' # display TABs as 4 characters
42
+ uc_map [b'\t ' ] = ' ' # display TABs as 4 characters
42
43
uc_map [b'\177 ' ] = unicode ('^?' )
43
44
for i in range (256 ):
44
45
c = unichr (i )
45
46
if c not in uc_map :
46
- uc_map [c ] = unicode ('\\ %03o' )% i
47
+ uc_map [c ] = unicode ('\\ %03o' ) % i
47
48
return uc_map
48
49
49
- # disp_str proved to be a bottleneck for large inputs, so it's been
50
- # rewritten in C; it's not required though.
51
- try :
52
- raise ImportError # currently it's borked by the unicode support
53
-
54
- from _pyrepl_utils import disp_str , init_unctrl_map
55
50
56
- init_unctrl_map (_make_unctrl_map ())
57
-
58
- del init_unctrl_map
59
- except ImportError :
60
- def _my_unctrl (c , u = _make_unctrl_map ()):
61
- if c in u :
62
- return u [c ]
51
+ def _my_unctrl (c , u = _make_unctrl_map ()):
52
+ if c in u :
53
+ return u [c ]
54
+ else :
55
+ if unicodedata .category (c ).startswith ('C' ):
56
+ return b'\u%04x' % ord (c )
63
57
else :
64
- if unicodedata .category (c ).startswith ('C' ):
65
- return b'\u%04x' % (ord (c ))
66
- else :
67
- return c
58
+ return c
68
59
69
- def disp_str (buffer , join = '' .join , uc = _my_unctrl ):
70
- """ disp_str(buffer:string) -> (string, [int])
71
60
72
- Return the string that should be the printed represenation of
73
- |buffer| and a list detailing where the characters of |buffer|
74
- get used up. E.g.:
61
+ def disp_str (buffer , join = '' .join , uc = _my_unctrl ):
62
+ """ disp_str(buffer:string) -> (string, [int])
75
63
76
- >>> disp_str(chr(3))
77
- ('^C', [1, 0])
64
+ Return the string that should be the printed represenation of
65
+ |buffer| and a list detailing where the characters of |buffer|
66
+ get used up. E.g.:
78
67
79
- the list always contains 0s or 1s at present; it could conceivably
80
- go higher as and when unicode support happens."""
81
- s = [uc (x ) for x in buffer ]
82
- b = [] #XXX: bytearray
83
- for x in s :
84
- b .append (1 )
85
- b .extend ([0 ]* (len (x )- 1 ))
86
- return join (s ), b
68
+ >>> disp_str(chr(3))
69
+ ('^C', [1, 0])
87
70
88
- del _my_unctrl
71
+ the list always contains 0s or 1s at present; it could conceivably
72
+ go higher as and when unicode support happens."""
73
+ # disp_str proved to be a bottleneck for large inputs,
74
+ # so it needs to be rewritten in C; it's not required though.
75
+ s = [uc (x ) for x in buffer ]
76
+ b = [] # XXX: bytearray
77
+ for x in s :
78
+ b .append (1 )
79
+ b .extend ([0 ] * (len (x ) - 1 ))
80
+ return join (s ), b
81
+
82
+ del _my_unctrl
89
83
90
84
del _make_unctrl_map
91
85
@@ -95,6 +89,7 @@ def disp_str(buffer, join=''.join, uc=_my_unctrl):
95
89
SYNTAX_WORD ,
96
90
SYNTAX_SYMBOL ] = range (3 )
97
91
92
+
98
93
def make_default_syntax_table ():
99
94
# XXX perhaps should use some unicodedata here?
100
95
st = {}
@@ -164,13 +159,14 @@ def make_default_syntax_table():
164
159
(r'\<end>' , 'end-of-line' ), # was 'end'
165
160
(r'\<home>' , 'beginning-of-line' ), # was 'home'
166
161
(r'\<f1>' , 'help' ),
167
- (r'\EOF' , 'end' ), # the entries in the terminfo database for xterms
168
- (r'\EOH' , 'home' ), # seem to be wrong. this is a less than ideal
169
- # workaround
162
+ (r'\EOF' , 'end' ), # the entries in the terminfo database for xterms
163
+ (r'\EOH' , 'home' ), # seem to be wrong. this is a less than ideal
164
+ # workaround
170
165
])
171
166
172
167
if 'c' in globals (): # only on python 2.x
173
- del c # from the listcomps
168
+ del c # from the listcomps
169
+
174
170
175
171
class Reader (object ):
176
172
"""The Reader class implements the bare bones of a command reader,
@@ -248,9 +244,9 @@ def __init__(self, console):
248
244
self .commands = {}
249
245
self .msg = ''
250
246
for v in vars (commands ).values ():
251
- if ( isinstance (v , type )
252
- and issubclass (v , commands .Command )
253
- and v .__name__ [0 ].islower () ):
247
+ if ( isinstance (v , type )
248
+ and issubclass (v , commands .Command )
249
+ and v .__name__ [0 ].islower ()):
254
250
self .commands [v .__name__ ] = v
255
251
self .commands [v .__name__ .replace ('_' , '-' )] = v
256
252
self .syntax_table = make_default_syntax_table ()
@@ -294,15 +290,15 @@ def calc_screen(self):
294
290
wrapcount = (len (l ) + lp ) // w
295
291
if wrapcount == 0 :
296
292
screen .append (prompt + l )
297
- screeninfo .append ((lp , l2 + [1 ]))
293
+ screeninfo .append ((lp , l2 + [1 ]))
298
294
else :
299
- screen .append (prompt + l [:w - lp ] + "\\ " )
300
- screeninfo .append ((lp , l2 [:w - lp ]))
301
- for i in range (- lp + w , - lp + wrapcount * w , w ):
302
- screen .append (l [i :i + w ] + "\\ " )
295
+ screen .append (prompt + l [:w - lp ] + "\\ " )
296
+ screeninfo .append ((lp , l2 [:w - lp ]))
297
+ for i in range (- lp + w , - lp + wrapcount * w , w ):
298
+ screen .append (l [i :i + w ] + "\\ " )
303
299
screeninfo .append ((0 , l2 [i :i + w ]))
304
- screen .append (l [wrapcount * w - lp :])
305
- screeninfo .append ((0 , l2 [wrapcount * w - lp :]+ [1 ]))
300
+ screen .append (l [wrapcount * w - lp :])
301
+ screeninfo .append ((0 , l2 [wrapcount * w - lp :] + [1 ]))
306
302
self .screeninfo = screeninfo
307
303
self .cxy = self .pos2xy (self .pos )
308
304
if self .msg and self .msg_at_bottom :
@@ -330,9 +326,9 @@ def process_prompt(self, prompt):
330
326
if e == - 1 :
331
327
break
332
328
# Found start and end brackets, subtract from string length
333
- l = l - (e - s + 1 )
334
- out_prompt += prompt [pos :s ] + prompt [s + 1 :e ]
335
- pos = e + 1
329
+ l = l - (e - s + 1 )
330
+ out_prompt += prompt [pos :s ] + prompt [s + 1 :e ]
331
+ pos = e + 1
336
332
out_prompt += prompt [pos :]
337
333
return out_prompt , l
338
334
@@ -408,7 +404,7 @@ def get_prompt(self, lineno, cursor_on_line):
408
404
"""Return what should be in the left-hand margin for line
409
405
`lineno'."""
410
406
if self .arg is not None and cursor_on_line :
411
- return "(arg: %s) " % self .arg
407
+ return "(arg: %s) " % self .arg
412
408
if "\n " in self .buffer :
413
409
if lineno == 0 :
414
410
res = self .ps2
@@ -521,17 +517,18 @@ def refresh(self):
521
517
# this call sets up self.cxy, so call it first.
522
518
screen = self .calc_screen ()
523
519
self .console .refresh (screen , self .cxy )
524
- self .dirty = 0 # forgot this for a while (blush)
520
+ self .dirty = 0 # forgot this for a while (blush)
525
521
526
522
def do_cmd (self , cmd ):
527
523
#print cmd
528
- if isinstance (cmd [0 ], basestring ): #XXX: unify to text
524
+ if isinstance (cmd [0 ], basestring ):
525
+ #XXX: unify to text
529
526
cmd = self .commands .get (cmd [0 ],
530
527
commands .invalid_command )(self , * cmd )
531
528
elif isinstance (cmd [0 ], type ):
532
529
cmd = cmd [0 ](self , cmd )
533
530
else :
534
- return # nothing to do
531
+ return # nothing to do
535
532
536
533
cmd .do ()
537
534
@@ -561,7 +558,7 @@ def handle1(self, block=1):
561
558
562
559
while 1 :
563
560
event = self .console .get_event (block )
564
- if not event : # can only happen if we're not blocking
561
+ if not event : # can only happen if we're not blocking
565
562
return None
566
563
567
564
translate = True
@@ -626,6 +623,7 @@ def get_unicode(self):
626
623
"""Return the current buffer as a unicode string."""
627
624
return unicode ('' ).join (self .buffer )
628
625
626
+
629
627
def test ():
630
628
from pyrepl .unix_console import UnixConsole
631
629
reader = Reader (UnixConsole ())
@@ -636,5 +634,5 @@ def test():
636
634
while reader .readline ():
637
635
pass
638
636
639
- if __name__ == '__main__' :
637
+ if __name__ == '__main__' :
640
638
test ()
0 commit comments