Skip to content

Commit d13227b

Browse files
committed
At least partially hook up readline history (up/down key).
1 parent 8ae4336 commit d13227b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

bpython/urwid.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class BPythonEdit(urwid.Edit):
132132
133133
- move_cursor_to_coords is ignored
134134
(except for internal calls from keypress or mouse_event).
135+
136+
- arrow up/down are ignored.
135137
"""
136138

137139
def __init__(self, *args, **kwargs):
@@ -191,10 +193,13 @@ def move_cursor_to_coords(self, *args):
191193
return urwid.Edit.move_cursor_to_coords(self, *args)
192194
return False
193195

194-
def keypress(self, *args):
196+
def keypress(self, size, key):
195197
self._bpy_may_move_cursor = True
196198
try:
197-
return urwid.Edit.keypress(self, *args)
199+
# Do not handle up/down arrow, leave them for the repl.
200+
if urwid.command_map[key] in ('cursor up', 'cursor down'):
201+
return key
202+
return urwid.Edit.keypress(self, size, key)
198203
finally:
199204
self._bpy_may_move_cursor = False
200205

@@ -474,6 +479,8 @@ def keyboard_interrupt(self):
474479
self.echo('KeyboardInterrupt')
475480

476481
def prompt(self, more):
482+
# XXX is this the right place?
483+
self.rl_history.reset()
477484
# XXX what is s_hist?
478485
if not more:
479486
self.edit = BPythonEdit(caption=('prompt', '>>> '))
@@ -514,6 +521,18 @@ def handle_input(self, event):
514521
# ctrl+d on an empty line exits
515522
if self.edit is not None and not self.edit.get_edit_text():
516523
raise urwid.ExitMainLoop()
524+
elif urwid.command_map[event] == 'cursor up':
525+
# "back" from bpython.cli
526+
self.cpos = 0
527+
self.rl_history.enter(self.edit.get_edit_text())
528+
self.edit.set_edit_text('')
529+
self.edit.insert_text(self.rl_history.back())
530+
elif urwid.command_map[event] == 'cursor down':
531+
# "fwd" from bpython.cli
532+
self.cpos = 0
533+
self.rl_history.enter(self.edit.get_edit_text())
534+
self.edit.set_edit_text('')
535+
self.edit.insert_text(self.rl_history.forward())
517536
#else:
518537
# self.echo(repr(event))
519538

0 commit comments

Comments
 (0)