52
52
53
53
import struct
54
54
if platform .system () != 'Windows' :
55
- import signal # Windows does not have job control
56
- import termios # Windows uses curses
57
- import fcntl # Windows uses curses
55
+ import signal # Windows does not have job control
56
+ import termios # Windows uses curses
57
+ import fcntl # Windows uses curses
58
58
import unicodedata
59
59
import errno
60
60
@@ -112,6 +112,7 @@ def calculate_screen_lines(tokens, width, cursor=0):
112
112
pos %= width
113
113
return lines
114
114
115
+
115
116
def forward_if_not_current (func ):
116
117
@functools .wraps (func )
117
118
def newfunc (self , * args , ** kwargs ):
@@ -202,7 +203,7 @@ def readline(self, size=-1):
202
203
# C-d
203
204
return ''
204
205
elif (key not in ('\n ' , '\r ' ) and
205
- (len (key ) > 1 or unicodedata .category (key ) == 'Cc' )):
206
+ (len (key ) > 1 or unicodedata .category (key ) == 'Cc' )):
206
207
continue
207
208
sys .stdout .write (key )
208
209
# Include the \n in the buffer - raw_input() seems to deal with trailing
@@ -281,7 +282,7 @@ def make_colors(config):
281
282
282
283
if platform .system () == 'Windows' :
283
284
c = dict (list (c .items ()) +
284
- [
285
+ [
285
286
('K' , 8 ),
286
287
('R' , 9 ),
287
288
('G' , 10 ),
@@ -290,8 +291,8 @@ def make_colors(config):
290
291
('M' , 13 ),
291
292
('C' , 14 ),
292
293
('W' , 15 ),
293
- ]
294
- )
294
+ ]
295
+ )
295
296
296
297
for i in range (63 ):
297
298
if i > 7 :
@@ -316,7 +317,6 @@ def confirm(self, q):
316
317
317
318
return reply .lower () in (_ ('y' ), _ ('yes' ))
318
319
319
-
320
320
def notify (self , s , n = 10 , wait_for_keypress = False ):
321
321
return self .statusbar .message (s , n )
322
322
@@ -350,6 +350,7 @@ def __init__(self, scr, interp, statusbar, config, idle=None):
350
350
351
351
def _get_cursor_offset (self ):
352
352
return len (self .s ) - self .cpos
353
+
353
354
def _set_cursor_offset (self , offset ):
354
355
self .cpos = len (self .s ) - offset
355
356
cursor_offset = property (_get_cursor_offset , _set_cursor_offset , None ,
@@ -427,7 +428,7 @@ def check(self):
427
428
it and force syntax highlighting."""
428
429
429
430
if (self .paste_mode
430
- and time .time () - self .last_key_press > self .config .paste_time ):
431
+ and time .time () - self .last_key_press > self .config .paste_time ):
431
432
self .paste_mode = False
432
433
self .print_line (self .s )
433
434
@@ -454,7 +455,7 @@ def complete(self, tab=False):
454
455
with tab
455
456
"""
456
457
if self .paste_mode :
457
- self .scr .touchwin () # TODO necessary?
458
+ self .scr .touchwin () # TODO necessary?
458
459
return
459
460
460
461
list_win_visible = repl .Repl .complete (self , tab )
@@ -489,6 +490,7 @@ def clrtobol(self):
489
490
490
491
def _get_current_line (self ):
491
492
return self .s
493
+
492
494
def _set_current_line (self , line ):
493
495
self .s = line
494
496
current_line = property (_get_current_line , _set_current_line , None ,
@@ -716,7 +718,7 @@ def mkargspec(self, topline, in_arg, down):
716
718
717
719
self .list_win .addstr ('\n ' )
718
720
self .list_win .addstr (fn ,
719
- get_colpair (self .config , 'name' ) | curses .A_BOLD )
721
+ get_colpair (self .config , 'name' ) | curses .A_BOLD )
720
722
self .list_win .addstr (': (' , get_colpair (self .config , 'name' ))
721
723
maxh = self .scr .getmaxyx ()[0 ]
722
724
@@ -765,7 +767,7 @@ def mkargspec(self, topline, in_arg, down):
765
767
if kw is not None :
766
768
self .list_win .addstr ('=' , punctuation_colpair )
767
769
self .list_win .addstr (kw , get_colpair (self .config , 'token' ))
768
- if k != len (args ) - 1 :
770
+ if k != len (args ) - 1 :
769
771
self .list_win .addstr (', ' , punctuation_colpair )
770
772
771
773
if _args :
@@ -854,7 +856,7 @@ def p_key(self, key):
854
856
key = '\n '
855
857
# Don't return; let it get handled
856
858
857
- if key == chr (27 ): # Escape Key
859
+ if key == chr (27 ): # Escape Key
858
860
return ''
859
861
860
862
if key in (BACKSP , 'KEY_BACKSPACE' ):
@@ -894,7 +896,7 @@ def p_key(self, key):
894
896
self .fwd ()
895
897
return ''
896
898
897
- elif key in ("KEY_LEFT" ,' ^B' , chr (2 )): # Cursor Left or ^B
899
+ elif key in ("KEY_LEFT" , ' ^B' , chr (2 )): # Cursor Left or ^B
898
900
self .mvc (1 )
899
901
# Redraw (as there might have been highlighted parens)
900
902
self .print_line (self .s )
@@ -914,11 +916,11 @@ def p_key(self, key):
914
916
# Redraw (as there might have been highlighted parens)
915
917
self .print_line (self .s )
916
918
917
- elif key in ("KEY_NPAGE" , '\T' ): # page_down or \T
919
+ elif key in ("KEY_NPAGE" , '\T' ): # page_down or \T
918
920
self .hend ()
919
921
self .print_line (self .s )
920
922
921
- elif key in ("KEY_PPAGE" , '\S' ): # page_up or \S
923
+ elif key in ("KEY_PPAGE" , '\S' ): # page_up or \S
922
924
self .hbegin ()
923
925
self .print_line (self .s )
924
926
@@ -1065,7 +1067,8 @@ def print_line(self, s, clr=False, newline=False):
1065
1067
def prompt (self , more ):
1066
1068
"""Show the appropriate Python prompt"""
1067
1069
if not more :
1068
- self .echo ("\x01 %s\x03 %s" % (self .config .color_scheme ['prompt' ], self .ps1 ))
1070
+ self .echo ("\x01 %s\x03 %s" %
1071
+ (self .config .color_scheme ['prompt' ], self .ps1 ))
1069
1072
if py3 :
1070
1073
self .stdout_hist += self .ps1
1071
1074
else :
@@ -1079,7 +1082,8 @@ def prompt(self, more):
1079
1082
self .stdout_hist += self .ps2
1080
1083
else :
1081
1084
self .stdout_hist += self .ps2 .encode (getpreferredencoding ())
1082
- self .s_hist .append ('\x01 %s\x03 %s\x04 ' % (prompt_more_color , self .ps2 ))
1085
+ self .s_hist .append ('\x01 %s\x03 %s\x04 ' %
1086
+ (prompt_more_color , self .ps2 ))
1083
1087
1084
1088
def push (self , s , insert_into_history = True ):
1085
1089
# curses.raw(True) prevents C-c from causing a SIGINT
@@ -1103,7 +1107,7 @@ def redraw(self):
1103
1107
self .iy , self .ix = self .scr .getyx ()
1104
1108
for i in s .split ('\x04 ' ):
1105
1109
self .echo (i , redraw = False )
1106
- if k < len (self .s_hist ) - 1 :
1110
+ if k < len (self .s_hist ) - 1 :
1107
1111
self .scr .addstr ('\n ' )
1108
1112
self .iy , self .ix = self .scr .getyx ()
1109
1113
self .print_line (self .s )
@@ -1186,14 +1190,12 @@ def resize(self):
1186
1190
self .statusbar .resize (refresh = False )
1187
1191
self .redraw ()
1188
1192
1189
-
1190
1193
def getstdout (self ):
1191
1194
"""This method returns the 'spoofed' stdout buffer, for writing to a
1192
1195
file or sending to a pastebin or whatever."""
1193
1196
1194
1197
return self .stdout_hist + '\n '
1195
1198
1196
-
1197
1199
def reevaluate (self ):
1198
1200
"""Clear the buffer, redraw the screen and re-evaluate the history"""
1199
1201
@@ -1234,7 +1236,7 @@ def reevaluate(self):
1234
1236
1235
1237
self .evaluating = False
1236
1238
#map(self.push, self.history)
1237
- #^-- That's how simple this method was at first :(
1239
+ # ^-- That's how simple this method was at first :(
1238
1240
1239
1241
def write (self , s ):
1240
1242
"""For overriding stdout defaults"""
@@ -1258,7 +1260,6 @@ def write(self, s):
1258
1260
self .echo (s )
1259
1261
self .s_hist .append (s .rstrip ())
1260
1262
1261
-
1262
1263
def show_list (self , items , arg_pos , topline = None , formatter = None , current_item = None ):
1263
1264
1264
1265
shared = Struct ()
@@ -1345,7 +1346,7 @@ def lsize():
1345
1346
self .list_win .resize (rows + 2 , w )
1346
1347
else :
1347
1348
docstring = self .format_docstring (self .docstring , max_w - 2 ,
1348
- max_h - height_offset )
1349
+ max_h - height_offset )
1349
1350
docstring_string = '' .join (docstring )
1350
1351
rows += len (docstring )
1351
1352
self .list_win .resize (rows , max_w )
@@ -1445,7 +1446,7 @@ def tab(self, back=False):
1445
1446
1446
1447
# 3. check to see if we can expand the current word
1447
1448
if self .matches_iter .is_cseq ():
1448
- #TODO resolve this error-prone situation:
1449
+ # TODO resolve this error-prone situation:
1449
1450
# can't assign at same time to self.s and self.cursor_offset
1450
1451
# because for cursor_offset
1451
1452
# property to work correctly, self.s must already be set
@@ -1458,7 +1459,7 @@ def tab(self, back=False):
1458
1459
# 4. swap current word for a match list item
1459
1460
elif self .matches_iter .matches :
1460
1461
current_match = back and self .matches_iter .previous () \
1461
- or next (self .matches_iter )
1462
+ or next (self .matches_iter )
1462
1463
try :
1463
1464
self .show_list (self .matches_iter .matches , self .arg_pos ,
1464
1465
topline = self .funcprops ,
@@ -1500,7 +1501,7 @@ def send_current_line_to_editor(self):
1500
1501
return ''
1501
1502
1502
1503
self .f_string = ''
1503
- self .cpos = - 1 # Set cursor position to -1 to prevent paren matching
1504
+ self .cpos = - 1 # Set cursor position to -1 to prevent paren matching
1504
1505
1505
1506
self .iy , self .ix = self .scr .getyx ()
1506
1507
self .evaluating = True
@@ -1531,6 +1532,7 @@ def send_current_line_to_editor(self):
1531
1532
self .scr .redrawwin ()
1532
1533
return ''
1533
1534
1535
+
1534
1536
class Statusbar (object ):
1535
1537
"""This class provides the status bar at the bottom of the screen.
1536
1538
It has message() and prompt() methods for user interactivity, as
@@ -1687,7 +1689,7 @@ def clear(self):
1687
1689
def init_wins (scr , config ):
1688
1690
"""Initialise the two windows (the main repl interface and the little
1689
1691
status bar at the bottom with some stuff in it)"""
1690
- #TODO: Document better what stuff is on the status bar.
1692
+ # TODO: Document better what stuff is on the status bar.
1691
1693
1692
1694
background = get_colpair (config , 'background' )
1693
1695
h , w = gethw ()
@@ -1719,11 +1721,13 @@ def sigwinch(unused_scr):
1719
1721
global DO_RESIZE
1720
1722
DO_RESIZE = True
1721
1723
1724
+
1722
1725
def sigcont (unused_scr ):
1723
1726
sigwinch (unused_scr )
1724
1727
# Forces the redraw
1725
1728
curses .ungetch ('\x00 ' )
1726
1729
1730
+
1727
1731
def gethw ():
1728
1732
"""I found this code on a usenet post, and snipped out the bit I needed,
1729
1733
so thanks to whoever wrote that, sorry I forgot your name, I'm sure you're
@@ -1761,7 +1765,8 @@ def gethw():
1761
1765
sizex = right - left + 1
1762
1766
sizey = bottom - top + 1
1763
1767
else :
1764
- sizex , sizey = stdscr .getmaxyx ()# can't determine actual size - return default values
1768
+ # can't determine actual size - return default values
1769
+ sizex , sizey = stdscr .getmaxyx ()
1765
1770
1766
1771
h , w = sizey , sizex
1767
1772
return h , w
@@ -1796,7 +1801,7 @@ def do_resize(caller):
1796
1801
global DO_RESIZE
1797
1802
h , w = gethw ()
1798
1803
if not h :
1799
- # Hopefully this shouldn't happen. :)
1804
+ # Hopefully this shouldn't happen. :)
1800
1805
return
1801
1806
1802
1807
curses .endwin ()
@@ -1878,7 +1883,8 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
1878
1883
old_sigwinch_handler = signal .signal (signal .SIGWINCH ,
1879
1884
lambda * _ : sigwinch (scr ))
1880
1885
# redraw window after being suspended
1881
- old_sigcont_handler = signal .signal (signal .SIGCONT , lambda * _ : sigcont (scr ))
1886
+ old_sigcont_handler = signal .signal (
1887
+ signal .SIGCONT , lambda * _ : sigcont (scr ))
1882
1888
1883
1889
stdscr = scr
1884
1890
try :
@@ -1951,7 +1957,6 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
1951
1957
def main (args = None , locals_ = None , banner = None ):
1952
1958
translations .init ()
1953
1959
1954
-
1955
1960
config , options , exec_args = argsparse (args )
1956
1961
1957
1962
# Save stdin, stdout and stderr for later restoration
@@ -1975,6 +1980,7 @@ def main(args=None, locals_=None, banner=None):
1975
1980
sys .stdout .flush ()
1976
1981
return repl .extract_exit_value (exit_value )
1977
1982
1983
+
1978
1984
if __name__ == '__main__' :
1979
1985
sys .exit (main ())
1980
1986
0 commit comments