Skip to content

Commit 6f708ed

Browse files
slight refactoring of the unix reader to start cleaning up some messes
1 parent 24a8cea commit 6f708ed

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

pyrepl/unix_console.py

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _my_getstr(cap, optional=0):
6363

6464
# at this point, can we say: AAAAAAAAAAAAAAAAAAAAAARGH!
6565
def maybe_add_baudrate(dict, rate):
66-
name = 'B%d'%rate
66+
name = 'B%d' % rate
6767
if hasattr(termios, name):
6868
dict[getattr(termios, name)] = rate
6969

@@ -89,12 +89,18 @@ def register(self, fd, flag):
8989
self.fd = fd
9090

9191
def poll(self, timeout=None):
92-
r, w, e = select.select([self.fd],[],[],timeout)
92+
r, w, e = select.select([self.fd], [], [], timeout)
9393
return r
9494

9595
POLLIN = getattr(select, "POLLIN", None)
9696

9797

98+
required_curses_tistrings = 'bel clear cup el'
99+
optional_curses_tistrings = (
100+
'civis cnorm cub cub1 cud cud1 cud cud1 cuf '
101+
'cuf1 cuu cuu1 dch dch1 hpa ich ich1 ind pad ri rmkx smkx')
102+
103+
98104
class UnixConsole(Console):
99105
def __init__(self, f_in=0, f_out=1, term=None, encoding=None):
100106
if encoding is None:
@@ -117,33 +123,15 @@ def __init__(self, f_in=0, f_out=1, term=None, encoding=None):
117123
curses.setupterm(term, self.output_fd)
118124
self.term = term
119125

120-
self._bel = _my_getstr("bel")
121-
self._civis = _my_getstr("civis", optional=1)
122-
self._clear = _my_getstr("clear")
123-
self._cnorm = _my_getstr("cnorm", optional=1)
124-
self._cub = _my_getstr("cub", optional=1)
125-
self._cub1 = _my_getstr("cub1", 1)
126-
self._cud = _my_getstr("cud", 1)
127-
self._cud1 = _my_getstr("cud1", 1)
128-
self._cuf = _my_getstr("cuf", 1)
129-
self._cuf1 = _my_getstr("cuf1", 1)
130-
self._cup = _my_getstr("cup")
131-
self._cuu = _my_getstr("cuu", 1)
132-
self._cuu1 = _my_getstr("cuu1", 1)
133-
self._dch1 = _my_getstr("dch1", 1)
134-
self._dch = _my_getstr("dch", 1)
135-
self._el = _my_getstr("el")
136-
self._hpa = _my_getstr("hpa", 1)
137-
self._ich = _my_getstr("ich", 1)
138-
self._ich1 = _my_getstr("ich1", 1)
139-
self._ind = _my_getstr("ind", 1)
140-
self._pad = _my_getstr("pad", 1)
141-
self._ri = _my_getstr("ri", 1)
142-
self._rmkx = _my_getstr("rmkx", 1)
143-
self._smkx = _my_getstr("smkx", 1)
144-
126+
for name in required_curses_tistrings.split():
127+
setattr(self, '_' + name, _my_getstr(name))
128+
129+
for name in optional_curses_tistrings.split():
130+
setattr(self, '_' + name, _my_getstr(name, optional=1))
131+
145132
## work out how we're going to sling the cursor around
146-
if 0 and self._hpa: # hpa don't work in windows telnet :-(
133+
# hpa don't work in windows telnet :-(
134+
if 0 and self._hpa:
147135
self.__move_x = self.__move_x_hpa
148136
elif self._cub and self._cuf:
149137
self.__move_x = self.__move_x_cub_cuf
@@ -264,11 +252,12 @@ def __write_changed_line(self, y, oldline, newline, px):
264252
# reuse the oldline as much as possible, but stop as soon as we
265253
# encounter an ESCAPE, because it might be the start of an escape
266254
# sequene
255+
#XXX unicode check!
267256
while x < minlen and oldline[x] == newline[x] and newline[x] != '\x1b':
268257
x += 1
269258
if oldline[x:] == newline[x+1:] and self.ich1:
270-
if ( y == self.__posxy[1] and x > self.__posxy[0]
271-
and oldline[px:x] == newline[px+1:x+1] ):
259+
if (y == self.__posxy[1] and x > self.__posxy[0] and
260+
oldline[px:x] == newline[px+1:x+1]):
272261
x = px
273262
self.__move(x, y)
274263
self.__write_code(self.ich1)
@@ -297,6 +286,7 @@ def __write_changed_line(self, y, oldline, newline, px):
297286
self.__write(newline[x:])
298287
self.__posxy = len(newline), y
299288

289+
#XXX: check for unicode mess
300290
if '\x1b' in newline:
301291
# ANSI escape characters are present, so we can't assume
302292
# anything about the position of the cursor. Moving the cursor
@@ -367,12 +357,12 @@ def prepare(self):
367357
self.__svtermstate = tcgetattr(self.input_fd)
368358
raw = self.__svtermstate.copy()
369359
raw.iflag |= termios.ICRNL
370-
raw.iflag &=~ (termios.BRKINT | termios.INPCK |
360+
raw.iflag &= ~(termios.BRKINT | termios.INPCK |
371361
termios.ISTRIP | termios.IXON)
372-
raw.oflag &=~ (termios.OPOST)
373-
raw.cflag &=~ (termios.CSIZE | termios.PARENB)
374-
raw.cflag |= (termios.CS8)
375-
raw.lflag &=~ (termios.ICANON | termios.ECHO|
362+
raw.oflag &= ~termios.OPOST
363+
raw.cflag &= ~(termios.CSIZE | termios.PARENB)
364+
raw.cflag |= (termios.CS8)
365+
raw.lflag &= ~(termios.ICANON | termios.ECHO |
376366
termios.IEXTEN | (termios.ISIG * 1))
377367
raw.cc[termios.VMIN] = 1
378368
raw.cc[termios.VTIME] = 0
@@ -414,7 +404,7 @@ def push_char(self, char):
414404

415405
def get_event(self, block=1):
416406
while self.event_queue.empty():
417-
while 1:
407+
while 1:
418408
# All hail Unix!
419409
try:
420410
self.push_char(os.read(self.input_fd, 1))
@@ -541,7 +531,9 @@ def getpending(self):
541531

542532
amount = struct.unpack(
543533
"i", ioctl(self.input_fd, FIONREAD, "\0\0\0\0"))[0]
544-
raw = unicode(os.read(self.input_fd, amount), self.encoding, 'replace')
534+
data = os.read(self.input_fd, amount)
535+
raw = unicode(data, self.encoding, 'replace')
536+
#XXX: something is wrong here
545537
e.data += raw
546538
e.raw += raw
547539
return e
@@ -553,9 +545,11 @@ def getpending(self):
553545
e2 = self.event_queue.get()
554546
e.data += e2.data
555547
e.raw += e.raw
556-
548+
557549
amount = 10000
558-
raw = unicode(os.read(self.input_fd, amount), self.encoding, 'replace')
550+
data = os.read(self.input_fd, amount)
551+
raw = unicode(data, self.encoding, 'replace')
552+
#XXX: something is wrong here
559553
e.data += raw
560554
e.raw += raw
561555
return e
@@ -566,4 +560,3 @@ def clear(self):
566560
self.__move = self.__move_tall
567561
self.__posxy = 0, 0
568562
self.screen = []
569-

0 commit comments

Comments
 (0)