Skip to content

Commit 3c9c4ae

Browse files
autoimport! also fixed display bug
TODO: paint current line is more complicated than necessary! --HG-- branch : scroll-frontend
1 parent 54dc8f1 commit 3c9c4ae

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

bpython/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def loadini(struct, configfile):
9999
'scroll': {
100100
'list_above' : False,
101101
'fill_terminal' : False,
102+
'auto_import' : False,
102103
},
103104
'gtk': {
104105
'font': 'monospace 10',
@@ -165,6 +166,7 @@ def loadini(struct, configfile):
165166

166167
struct.scroll_list_above = config.getboolean('scroll', 'list_above')
167168
struct.scroll_fill_terminal = config.getboolean('scroll', 'fill_terminal')
169+
struct.scroll_auto_import = config.getboolean('scroll', 'auto_import')
168170

169171
color_scheme_name = config.get('general', 'color_scheme')
170172
color_gtk_scheme_name = config.get('gtk', 'color_scheme')

bpython/scrollfrontend/repl.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ def readline(self):
104104
self.repl.send_to_stdout(value)
105105
return value
106106

107+
108+
def get_interpreter(config, locals_=None):
109+
if config.scroll_auto_import:
110+
111+
locals_ = locals_ if locals_ is not None else {}
112+
113+
class AutoImporter(dict):
114+
def __getitem__(self, item):
115+
if item in locals_:
116+
return locals_[item]
117+
else:
118+
try:
119+
return __import__(item)
120+
except ImportError:
121+
raise KeyError(item)
122+
def __setitem__(self, item, value):
123+
locals_[item] = value
124+
125+
ns = AutoImporter()
126+
return code.InteractiveInterpreter(locals=ns)
127+
else:
128+
return code.InteractiveInterpreter(locals=locals_)
129+
107130
class Repl(BpythonRepl):
108131
"""
109132
@@ -125,12 +148,14 @@ class Repl(BpythonRepl):
125148
## initialization, cleanup
126149
def __init__(self, locals_=None, config=None, stuff_a_refresh_request=lambda: None, banner=None):
127150
logging.debug("starting init")
128-
interp = code.InteractiveInterpreter(locals=locals_)
129151

130152
if config is None:
131153
config = Struct()
132154
loadini(config, default_config_path())
133155

156+
if config.scroll_auto_import:
157+
interp = get_interpreter(config, locals_=locals_)
158+
134159
if banner is None:
135160
banner = _('welcome to bpython')
136161

@@ -322,7 +347,7 @@ def on_enter(self, insert_into_history=True):
322347
self.rl_history.last()
323348
self.history.append(self._current_line)
324349
line = self._current_line
325-
self._current_line = ''
350+
#self._current_line = ''
326351
self.cursor_offset_in_line = 0
327352
self.push(line, insert_into_history=insert_into_history)
328353

@@ -654,6 +679,9 @@ def paint(self, about_to_exit=False):
654679
if self.stdin.has_focus:
655680
cursor_column = len(self.current_stdouterr_line) - self.stdin.cursor_offset_in_line
656681
assert cursor_column >= 0, cursor_column
682+
elif self.coderunner.running:
683+
cursor_column = len(self.current_cursor_line) + self.cursor_offset_in_line
684+
assert cursor_column >= 0, (cursor_column, len(self.current_cursor_line), len(self._current_line), self.cursor_offset_in_line)
657685
else:
658686
cursor_column = len(self.current_cursor_line) - len(self._current_line) + self.cursor_offset_in_line
659687
assert cursor_column >= 0, (cursor_column, len(self.current_cursor_line), len(self._current_line), self.cursor_offset_in_line)
@@ -791,7 +819,7 @@ def reevaluate(self, insert_into_history=False):
791819
self.display_lines = []
792820

793821
self.done = True # this keeps the first prompt correct
794-
self.interp = code.InteractiveInterpreter()
822+
self.interp = get_interpreter(self.config)
795823
self.coderunner.interp = self.interp
796824
self.completer = Autocomplete(self.interp.locals, self.config)
797825
self.completer.autocomplete_mode = 'simple'

0 commit comments

Comments
 (0)