X Tutup
Skip to content

Commit 4b6297a

Browse files
run startup file in bython-curtsies
(helps with Django workaround!)
1 parent 3cfec9a commit 4b6297a

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

bpython/curtsies.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import curtsies
88
import curtsies.window
99
import curtsies.input
10-
import curtsies.terminal
1110
import curtsies.events
1211

1312
from bpython.curtsiesfrontend.repl import Repl
@@ -26,7 +25,7 @@ def main(args=None, locals_=None, banner=None):
2625
]))
2726
if options.log:
2827
import logging
29-
logging.basicConfig(filename='scroll.log', level=logging.DEBUG)
28+
logging.basicConfig(filename='scroll.log', level=logging.INFO)
3029

3130
interp = None
3231
paste = None
@@ -48,9 +47,10 @@ def main(args=None, locals_=None, banner=None):
4847
else:
4948
sys.path.insert(0, '') # expected for interactive sessions (vanilla python does it)
5049

51-
mainloop(config, locals_, banner, interp, paste)
5250

53-
def mainloop(config, locals_, banner, interp=None, paste=None):
51+
mainloop(config, locals_, banner, interp, paste, interactive=(not exec_args))
52+
53+
def mainloop(config, locals_, banner, interp=None, paste=None, interactive=True):
5454
with curtsies.input.Input(keynames='curses') as input_generator:
5555
with curtsies.window.CursorAwareWindow(
5656
sys.stdout,
@@ -71,10 +71,11 @@ def event_or_refresh():
7171
with Repl(config=config,
7272
locals_=locals_,
7373
request_refresh=request_refresh,
74-
get_term_wh=window.get_term_wh,
74+
get_term_hw=window.get_term_hw,
7575
get_cursor_vertical_diff=window.get_cursor_vertical_diff,
7676
banner=banner,
77-
interp=interp) as repl:
77+
interp=interp,
78+
interactive=interactive) as repl:
7879
repl.height, repl.width = window.t.height, window.t.width
7980

8081
def process_event(e):

bpython/curtsiesfrontend/repl.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import errno
44
import greenlet
55
import logging
6+
import os
67
import re
78
import signal
89
import 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

Comments
 (0)
X Tutup