1919# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
2020# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2121
22- import termios , select , os , struct , errno
23- import signal , re , time , sys
22+ import termios
23+ import select
24+ import os
25+ import struct
26+ import errno
27+ import signal
28+ import re
29+ import time
30+ import sys
2431from fcntl import ioctl
2532from . import curses
2633from .fancy_termios import tcgetattr , tcsetattr
2734from .console import Console , Event
2835from .unix_eventqueue import EventQueue
2936from .trace import trace
3037
38+
3139class InvalidTerminal (RuntimeError ):
3240 pass
3341
@@ -44,13 +52,15 @@ class InvalidTerminal(RuntimeError):
4452FIONREAD = getattr (termios , "FIONREAD" , None )
4553TIOCGWINSZ = getattr (termios , "TIOCGWINSZ" , None )
4654
55+
4756def _my_getstr (cap , optional = 0 ):
4857 r = curses .tigetstr (cap )
4958 if not optional and r is None :
5059 raise InvalidTerminal (
51- "terminal doesn't have the required '%s' capability" % cap )
60+ "terminal doesn't have the required '%s' capability" % cap )
5261 return r
5362
63+
5464# at this point, can we say: AAAAAAAAAAAAAAAAAAAAAARGH!
5565def maybe_add_baudrate (dict , rate ):
5666 name = 'B%d' % rate
@@ -74,19 +84,22 @@ def maybe_add_baudrate(dict, rate):
7484 class poll :
7585 def __init__ (self ):
7686 pass
87+
7788 def register (self , fd , flag ):
7889 self .fd = fd
90+
7991 def poll (self , timeout = None ):
80- r ,w , e = select .select ([self .fd ],[],[],timeout )
92+ r , w , e = select .select ([self .fd ],[],[],timeout )
8193 return r
8294
8395POLLIN = getattr (select , "POLLIN" , None )
8496
97+
8598class UnixConsole (Console ):
8699 def __init__ (self , f_in = 0 , f_out = 1 , term = None , encoding = None ):
87100 if encoding is None :
88101 encoding = sys .getdefaultencoding ()
89-
102+
90103 self .encoding = encoding
91104
92105 if isinstance (f_in , int ):
@@ -98,13 +111,12 @@ def __init__(self, f_in=0, f_out=1, term=None, encoding=None):
98111 self .output_fd = f_out
99112 else :
100113 self .output_fd = f_out .fileno ()
101-
102114
103115 self .pollob = poll ()
104116 self .pollob .register (self .input_fd , POLLIN )
105117 curses .setupterm (term , self .output_fd )
106118 self .term = term
107-
119+
108120 self ._bel = _my_getstr ("bel" )
109121 self ._civis = _my_getstr ("civis" , optional = 1 )
110122 self ._clear = _my_getstr ("clear" )
@@ -166,9 +178,6 @@ def __init__(self, f_in=0, f_out=1, term=None, encoding=None):
166178 self .event_queue = EventQueue (self .input_fd , self .encoding )
167179 self .cursor_visible = 1
168180
169- def change_encoding (self , encoding ):
170- self .encoding = encoding
171-
172181 def refresh (self , screen , c_xy ):
173182 # this function is still too long (over 90 lines)
174183 cx , cy = c_xy
@@ -181,7 +190,7 @@ def refresh(self, screen, c_xy):
181190 self .screen .append ("" )
182191 else :
183192 while len (self .screen ) < len (screen ):
184- self .screen .append ("" )
193+ self .screen .append ("" )
185194
186195 if len (screen ) > self .height :
187196 self .__gone_tall = 1
@@ -191,7 +200,6 @@ def refresh(self, screen, c_xy):
191200 old_offset = offset = self .__offset
192201 height = self .height
193202
194-
195203 # we make sure the cursor is on the screen, and that we're
196204 # using all of the screen if we can
197205 if cy < offset :
@@ -230,7 +238,7 @@ def refresh(self, screen, c_xy):
230238 newscr ):
231239 if oldline != newline :
232240 self .__write_changed_line (y , oldline , newline , px )
233-
241+
234242 y = len (newscr )
235243 while y < len (oldscr ):
236244 self .__hide_cursor ()
@@ -240,7 +248,7 @@ def refresh(self, screen, c_xy):
240248 y += 1
241249
242250 self .__show_cursor ()
243-
251+
244252 self .screen = screen
245253 self .move_cursor (cx , cy )
246254 self .flushoutput ()
@@ -288,7 +296,7 @@ def __write_changed_line(self, y, oldline, newline, px):
288296 self .__write_code (self ._el )
289297 self .__write (newline [x :])
290298 self .__posxy = len (newline ), y
291-
299+
292300 if '\x1b ' in newline :
293301 # ANSI escape characters are present, so we can't assume
294302 # anything about the position of the cursor. Moving the cursor
@@ -362,10 +370,10 @@ def prepare(self):
362370 raw .iflag &= ~ (termios .BRKINT | termios .INPCK |
363371 termios .ISTRIP | termios .IXON )
364372 raw .oflag &= ~ (termios .OPOST )
365- raw .cflag &= ~ (termios .CSIZE | termios .PARENB )
373+ raw .cflag &= ~ (termios .CSIZE | termios .PARENB )
366374 raw .cflag |= (termios .CS8 )
367- raw .lflag &= ~ (termios .ICANON | termios .ECHO |
368- termios .IEXTEN | (termios .ISIG * 1 ))
375+ raw .lflag &= ~ (termios .ICANON | termios .ECHO |
376+ termios .IEXTEN | (termios .ISIG * 1 ))
369377 raw .cc [termios .VMIN ] = 1
370378 raw .cc [termios .VTIME ] = 0
371379 tcsetattr (self .input_fd , termios .TCSADRAIN , raw )
@@ -374,7 +382,7 @@ def prepare(self):
374382 self .height , self .width = self .getheightwidth ()
375383
376384 self .__buffer = []
377-
385+
378386 self .__posxy = 0 , 0
379387 self .__gone_tall = 0
380388 self .__move = self .__move_short
@@ -403,10 +411,11 @@ def __sigwinch(self, signum, frame):
403411 def push_char (self , char ):
404412 trace ('push char {char!r}' , char = char )
405413 self .event_queue .push (char )
406-
414+
407415 def get_event (self , block = 1 ):
408416 while self .event_queue .empty ():
409- while 1 : # All hail Unix!
417+ while 1 :
418+ # All hail Unix!
410419 try :
411420 self .push_char (os .read (self .input_fd , 1 ))
412421 except (IOError , OSError ) as err :
@@ -461,7 +470,8 @@ def getheightwidth(self):
461470 except KeyError :
462471 height , width = struct .unpack (
463472 "hhhh" , ioctl (self .input_fd , TIOCGWINSZ , "\000 " * 8 ))[0 :2 ]
464- if not height : return 25 , 80
473+ if not height :
474+ return 25 , 80
465475 return height , width
466476 else :
467477 def getheightwidth (self ):
@@ -528,7 +538,7 @@ def getpending(self):
528538 e2 = self .event_queue .get ()
529539 e .data += e2 .data
530540 e .raw += e .raw
531-
541+
532542 amount = struct .unpack (
533543 "i" , ioctl (self .input_fd , FIONREAD , "\0 \0 \0 \0 " ))[0 ]
534544 raw = unicode (os .read (self .input_fd , amount ), self .encoding , 'replace' )
0 commit comments