19
19
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20
20
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
21
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
24
31
from fcntl import ioctl
25
32
from . import curses
26
33
from .fancy_termios import tcgetattr , tcsetattr
27
34
from .console import Console , Event
28
35
from .unix_eventqueue import EventQueue
29
36
from .trace import trace
30
37
38
+
31
39
class InvalidTerminal (RuntimeError ):
32
40
pass
33
41
@@ -44,13 +52,15 @@ class InvalidTerminal(RuntimeError):
44
52
FIONREAD = getattr (termios , "FIONREAD" , None )
45
53
TIOCGWINSZ = getattr (termios , "TIOCGWINSZ" , None )
46
54
55
+
47
56
def _my_getstr (cap , optional = 0 ):
48
57
r = curses .tigetstr (cap )
49
58
if not optional and r is None :
50
59
raise InvalidTerminal (
51
- "terminal doesn't have the required '%s' capability" % cap )
60
+ "terminal doesn't have the required '%s' capability" % cap )
52
61
return r
53
62
63
+
54
64
# at this point, can we say: AAAAAAAAAAAAAAAAAAAAAARGH!
55
65
def maybe_add_baudrate (dict , rate ):
56
66
name = 'B%d' % rate
@@ -74,19 +84,22 @@ def maybe_add_baudrate(dict, rate):
74
84
class poll :
75
85
def __init__ (self ):
76
86
pass
87
+
77
88
def register (self , fd , flag ):
78
89
self .fd = fd
90
+
79
91
def poll (self , timeout = None ):
80
- r ,w , e = select .select ([self .fd ],[],[],timeout )
92
+ r , w , e = select .select ([self .fd ],[],[],timeout )
81
93
return r
82
94
83
95
POLLIN = getattr (select , "POLLIN" , None )
84
96
97
+
85
98
class UnixConsole (Console ):
86
99
def __init__ (self , f_in = 0 , f_out = 1 , term = None , encoding = None ):
87
100
if encoding is None :
88
101
encoding = sys .getdefaultencoding ()
89
-
102
+
90
103
self .encoding = encoding
91
104
92
105
if isinstance (f_in , int ):
@@ -98,13 +111,12 @@ def __init__(self, f_in=0, f_out=1, term=None, encoding=None):
98
111
self .output_fd = f_out
99
112
else :
100
113
self .output_fd = f_out .fileno ()
101
-
102
114
103
115
self .pollob = poll ()
104
116
self .pollob .register (self .input_fd , POLLIN )
105
117
curses .setupterm (term , self .output_fd )
106
118
self .term = term
107
-
119
+
108
120
self ._bel = _my_getstr ("bel" )
109
121
self ._civis = _my_getstr ("civis" , optional = 1 )
110
122
self ._clear = _my_getstr ("clear" )
@@ -166,9 +178,6 @@ def __init__(self, f_in=0, f_out=1, term=None, encoding=None):
166
178
self .event_queue = EventQueue (self .input_fd , self .encoding )
167
179
self .cursor_visible = 1
168
180
169
- def change_encoding (self , encoding ):
170
- self .encoding = encoding
171
-
172
181
def refresh (self , screen , c_xy ):
173
182
# this function is still too long (over 90 lines)
174
183
cx , cy = c_xy
@@ -181,7 +190,7 @@ def refresh(self, screen, c_xy):
181
190
self .screen .append ("" )
182
191
else :
183
192
while len (self .screen ) < len (screen ):
184
- self .screen .append ("" )
193
+ self .screen .append ("" )
185
194
186
195
if len (screen ) > self .height :
187
196
self .__gone_tall = 1
@@ -191,7 +200,6 @@ def refresh(self, screen, c_xy):
191
200
old_offset = offset = self .__offset
192
201
height = self .height
193
202
194
-
195
203
# we make sure the cursor is on the screen, and that we're
196
204
# using all of the screen if we can
197
205
if cy < offset :
@@ -230,7 +238,7 @@ def refresh(self, screen, c_xy):
230
238
newscr ):
231
239
if oldline != newline :
232
240
self .__write_changed_line (y , oldline , newline , px )
233
-
241
+
234
242
y = len (newscr )
235
243
while y < len (oldscr ):
236
244
self .__hide_cursor ()
@@ -240,7 +248,7 @@ def refresh(self, screen, c_xy):
240
248
y += 1
241
249
242
250
self .__show_cursor ()
243
-
251
+
244
252
self .screen = screen
245
253
self .move_cursor (cx , cy )
246
254
self .flushoutput ()
@@ -288,7 +296,7 @@ def __write_changed_line(self, y, oldline, newline, px):
288
296
self .__write_code (self ._el )
289
297
self .__write (newline [x :])
290
298
self .__posxy = len (newline ), y
291
-
299
+
292
300
if '\x1b ' in newline :
293
301
# ANSI escape characters are present, so we can't assume
294
302
# anything about the position of the cursor. Moving the cursor
@@ -362,10 +370,10 @@ def prepare(self):
362
370
raw .iflag &= ~ (termios .BRKINT | termios .INPCK |
363
371
termios .ISTRIP | termios .IXON )
364
372
raw .oflag &= ~ (termios .OPOST )
365
- raw .cflag &= ~ (termios .CSIZE | termios .PARENB )
373
+ raw .cflag &= ~ (termios .CSIZE | termios .PARENB )
366
374
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 ))
369
377
raw .cc [termios .VMIN ] = 1
370
378
raw .cc [termios .VTIME ] = 0
371
379
tcsetattr (self .input_fd , termios .TCSADRAIN , raw )
@@ -374,7 +382,7 @@ def prepare(self):
374
382
self .height , self .width = self .getheightwidth ()
375
383
376
384
self .__buffer = []
377
-
385
+
378
386
self .__posxy = 0 , 0
379
387
self .__gone_tall = 0
380
388
self .__move = self .__move_short
@@ -403,10 +411,11 @@ def __sigwinch(self, signum, frame):
403
411
def push_char (self , char ):
404
412
trace ('push char {char!r}' , char = char )
405
413
self .event_queue .push (char )
406
-
414
+
407
415
def get_event (self , block = 1 ):
408
416
while self .event_queue .empty ():
409
- while 1 : # All hail Unix!
417
+ while 1 :
418
+ # All hail Unix!
410
419
try :
411
420
self .push_char (os .read (self .input_fd , 1 ))
412
421
except (IOError , OSError ) as err :
@@ -461,7 +470,8 @@ def getheightwidth(self):
461
470
except KeyError :
462
471
height , width = struct .unpack (
463
472
"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
465
475
return height , width
466
476
else :
467
477
def getheightwidth (self ):
@@ -528,7 +538,7 @@ def getpending(self):
528
538
e2 = self .event_queue .get ()
529
539
e .data += e2 .data
530
540
e .raw += e .raw
531
-
541
+
532
542
amount = struct .unpack (
533
543
"i" , ioctl (self .input_fd , FIONREAD , "\0 \0 \0 \0 " ))[0 ]
534
544
raw = unicode (os .read (self .input_fd , amount ), self .encoding , 'replace' )
0 commit comments