@@ -237,7 +237,7 @@ def make_colors(config):
237237class CLIRepl (Repl ):
238238
239239 def __init__ (self , scr , interp , statusbar , config , idle = None ):
240- Repl .__init__ (self , interp , config , idle )
240+ Repl .__init__ (self , interp , config )
241241 interp .writetb = self .writetb
242242 self .scr = scr
243243 self .list_win = newwin (get_colpair (config , 'background' ), 1 , 1 , 1 , 1 )
@@ -250,6 +250,7 @@ def __init__(self, scr, interp, statusbar, config, idle=None):
250250 self .last_key_press = time .time ()
251251 self .s = ''
252252 self .statusbar = statusbar
253+ self .formatter = BPythonFormatter (config .color_scheme )
253254
254255 def addstr (self , s ):
255256 """Add a string to the current input line and figure out
@@ -833,6 +834,10 @@ def p_key(self, key):
833834 elif key == 'KEY_BTAB' :
834835 return self .tab (back = True )
835836
837+ elif key in key_dispatch [config .suspend_key ]:
838+ self .suspend ()
839+ return ''
840+
836841 elif len (key ) == 1 and not unicodedata .category (key ) == 'Cc' :
837842 self .addstr (key )
838843 self .print_line (self .s )
@@ -855,8 +860,7 @@ def print_line(self, s, clr=False, newline=False):
855860 self .highlighted_paren = None
856861
857862 if self .config .syntax and (not self .paste_mode or newline ):
858- o = format (self .tokenize (s , newline ),
859- BPythonFormatter (self .config .color_scheme ))
863+ o = format (self .tokenize (s , newline ), self .formatter )
860864 else :
861865 o = s
862866
@@ -1096,20 +1100,24 @@ def lsize():
10961100 if v_items :
10971101 self .list_win .addstr ('\n ' )
10981102
1103+ if not py3 :
1104+ encoding = getpreferredencoding ()
10991105 for ix , i in enumerate (v_items ):
11001106 padding = (wl - len (i )) * ' '
11011107 if i == current_item :
11021108 color = get_colpair (self .config , 'operator' )
11031109 else :
11041110 color = get_colpair (self .config , 'main' )
11051111 if not py3 :
1106- i = i .encode (getpreferredencoding () )
1112+ i = i .encode (encoding )
11071113 self .list_win .addstr (i + padding , color )
11081114 if ((cols == 1 or (ix and not (ix + 1 ) % cols ))
11091115 and ix + 1 < len (v_items )):
11101116 self .list_win .addstr ('\n ' )
11111117
11121118 if self .docstring is not None :
1119+ if not py3 :
1120+ docstring_string = docstring_string .encode (encoding , 'ignore' )
11131121 self .list_win .addstr ('\n ' + docstring_string ,
11141122 get_colpair (self .config , 'comment' ))
11151123# XXX: After all the trouble I had with sizing the list box (I'm not very good
@@ -1142,6 +1150,11 @@ def size(self):
11421150 self .h = h - 1
11431151 self .x = 0
11441152
1153+ def suspend (self ):
1154+ """Suspend the current process for shell job control."""
1155+ curses .endwin ()
1156+ os .kill (os .getpid (), signal .SIGSTOP )
1157+
11451158 def tab (self , back = False ):
11461159 """Process the tab key being hit. If there's only whitespace
11471160 in the line or the line is blank then process a normal tab,
@@ -1403,6 +1416,10 @@ def sigwinch(unused_scr):
14031416 global DO_RESIZE
14041417 DO_RESIZE = True
14051418
1419+ def sigcont (unused_scr ):
1420+ sigwinch (unused_scr )
1421+ # Forces the redraw
1422+ curses .ungetch ('' )
14061423
14071424def gethw ():
14081425 """I found this code on a usenet post, and snipped out the bit I needed,
@@ -1522,9 +1539,10 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
15221539 global colors
15231540 DO_RESIZE = False
15241541
1525- # FIXME: Handle window resize without signals
1526- #old_sigwinch_handler = signal.signal(signal.SIGWINCH,
1527- # lambda *_: sigwinch(scr))
1542+ old_sigwinch_handler = signal .signal (signal .SIGWINCH ,
1543+ lambda * _ : sigwinch (scr ))
1544+ # redraw window after being suspended
1545+ old_sigcont_handler = signal .signal (signal .SIGCONT , lambda * _ : sigcont (scr ))
15281546
15291547 stdscr = scr
15301548 try :
@@ -1577,9 +1595,9 @@ def main_curses(scr, args, config, interactive=True, locals_=None,
15771595 statusbar .win .refresh ()
15781596 curses .raw (False )
15791597
1580- # Restore SIGWINCH handler
1581- # FIXME: handle window resizes without signals
1582- # signal.signal(signal.SIGWINCH, old_sigwinch_handler )
1598+ # Restore signal handlers
1599+ signal . signal ( signal . SIGWINCH , old_sigwinch_handler )
1600+ signal .signal (signal .SIGCONT , old_sigcont_handler )
15831601
15841602 return repl .getstdout ()
15851603
@@ -1612,6 +1630,7 @@ def main(args=None, locals_=None, banner=None):
16121630
16131631
16141632if __name__ == '__main__' :
1633+ from bpython .cli import main
16151634 main ()
16161635
16171636# vim: sw=4 ts=4 sts=4 ai et
0 commit comments