33import errno
44import greenlet
55import logging
6+ import os
67import re
78import signal
89import sys
@@ -150,14 +151,14 @@ class Repl(BpythonRepl):
150151
151152 ## initialization, cleanup
152153 def __init__ (self , locals_ = None , config = None ,
153- request_refresh = lambda : None , get_term_wh = lambda :(50 , 10 ),
154- get_cursor_vertical_diff = lambda : 0 , banner = None , interp = None ):
154+ request_refresh = lambda : None , get_term_hw = lambda :(50 , 10 ),
155+ get_cursor_vertical_diff = lambda : 0 , banner = None , interp = None , interactive = True ):
155156 """
156157 locals_ is a mapping of locals to pass into the interpreter
157158 config is a bpython config.Struct with config attributes
158159 request_refresh is a function that will be called when the Repl
159160 wants to refresh the display, but wants control returned to it afterwards
160- get_term_wh is a function that returns the current width and height
161+ get_term_hw is a function that returns the current width and height
161162 of the terminal
162163 get_cursor_vertical_diff is a function that returns how the cursor moved
163164 due to a window size change
@@ -190,7 +191,7 @@ def smarter_request_refresh():
190191 else :
191192 request_refresh ()
192193 self .request_refresh = smarter_request_refresh
193- self .get_term_wh = get_term_wh
194+ self .get_term_hw = get_term_hw
194195 self .get_cursor_vertical_diff = get_cursor_vertical_diff
195196
196197 self .status_bar = StatusBar (banner if config .curtsies_fill_terminal else '' , _ (
@@ -201,6 +202,9 @@ def smarter_request_refresh():
201202 self .rl_char_sequences = get_updated_char_sequences (key_dispatch , config )
202203 logging .debug ("starting parent init" )
203204 super (Repl , self ).__init__ (interp , config )
205+ #TODO bring together all interactive stuff - including current directory in path?
206+ if interactive :
207+ self .startup ()
204208 self .formatter = BPythonFormatter (config .color_scheme )
205209 self .interact = self .status_bar # overwriting what bpython.Repl put there
206210 # interact is called to interact with the status bar,
@@ -254,12 +258,28 @@ def __exit__(self, *args):
254258
255259 def sigwinch_handler (self , signum , frame ):
256260 old_rows , old_columns = self .height , self .width
257- self .width , self .height = self .get_term_wh ()
261+ self .height , self .width = self .get_term_hw ()
258262 cursor_dy = self .get_cursor_vertical_diff ()
259- logging .debug ('sigwinch! Changed from %r to %r' , (old_rows , old_columns ), (self .height , self .width ))
260- logging .debug ('cursor moved %d lines down' , cursor_dy )
261263 self .scroll_offset -= cursor_dy
262- logging .debug ('scroll offset is now %d' , self .scroll_offset )
264+ logging .info ('sigwinch! Changed from %r to %r' , (old_rows , old_columns ), (self .height , self .width ))
265+ logging .info ('decreasing scroll offset by %d to %d' , cursor_dy , self .scroll_offset )
266+
267+ def startup (self ):
268+ """
269+ Execute PYTHONSTARTUP file if it exits. Call this after front
270+ end-specific initialisation.
271+ """
272+ filename = os .environ .get ('PYTHONSTARTUP' )
273+ if filename :
274+ if os .path .isfile (filename ):
275+ with open (filename , 'r' ) as f :
276+ if py3 :
277+ #TODO runsource has a new signature in PY3
278+ self .interp .runsource (f .read (), filename , 'exec' )
279+ else :
280+ self .interp .runsource (f .read (), filename , 'exec' )
281+ else :
282+ raise IOError ("Python startup file (PYTHONSTARTUP) not found at %s" % filename )
263283
264284 def clean_up_current_line_for_exit (self ):
265285 """Called when trying to exit to prep for final paint"""
@@ -693,6 +713,7 @@ def paint(self, about_to_exit=False, user_quit=False):
693713 min_height -= 1
694714
695715 current_line_start_row = len (self .lines_for_display ) - max (0 , self .scroll_offset )
716+ #current_line_start_row = len(self.lines_for_display) - self.scroll_offset
696717 if self .request_paint_to_clear_screen : # or show_status_bar and about_to_exit ?
697718 self .request_paint_to_clear_screen = False
698719 if self .config .curtsies_fill_terminal : #TODO clean up this logic - really necessary check?
0 commit comments