Skip to content

Commit 63ded48

Browse files
committed
Handle newlines better in URWIDRepl.echo (closes bpython#150).
We were incorrectly using multiple lines for each call to echo even if the displayed string did not end in a newline. This fixes it by reusing the last urwid Text widget in that case, adding an implicit newline on prompt.
1 parent ab62fb9 commit 63ded48

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

bpython/urwid.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,28 @@ def __init__(self, event_loop, palette, interpreter, config):
444444

445445
self.edits = []
446446
self.edit = None
447+
self.current_output = None
447448
self._completion_update_suppressed = False
448449

449450
# Subclasses of Repl need to implement echo, current_line, cw
450-
def echo(self, s):
451-
s = s.rstrip('\n')
451+
def echo(self, orig_s):
452+
s = orig_s.rstrip('\n')
452453
if s:
453-
text = urwid.Text(('output', s))
454-
if self.edit is None:
455-
self.listbox.body.append(text)
454+
if self.current_output is None:
455+
self.current_output = urwid.Text(('output', s))
456+
if self.edit is None:
457+
self.listbox.body.append(self.current_output)
458+
else:
459+
self.listbox.body.insert(-1, self.current_output)
460+
# The edit widget should be focused and *stay* focused.
461+
# XXX TODO: make sure the cursor stays in the same spot.
462+
self.listbox.set_focus(len(self.listbox.body) - 1)
456463
else:
457-
self.listbox.body.insert(-1, text)
458-
# The edit widget should be focused and *stay* focused.
459-
# XXX TODO: make sure the cursor stays in the same spot.
460-
self.listbox.set_focus(len(self.listbox.body) - 1)
464+
# XXX this assumes this all has "output" markup applied.
465+
self.current_output.set_text(
466+
('output', self.current_output.text + s))
467+
if orig_s.endswith('\n'):
468+
self.current_output = None
461469
# TODO: maybe do the redraw after a short delay
462470
# (for performance)
463471
self.main_loop.draw_screen()
@@ -708,6 +716,10 @@ def keyboard_interrupt(self):
708716
self.echo('KeyboardInterrupt')
709717

710718
def prompt(self, more):
719+
# Clear current output here, or output resulting from the
720+
# current prompt run will end up appended to the edit widget
721+
# sitting above this prompt:
722+
self.current_output = None
711723
# XXX is this the right place?
712724
self.rl_history.reset()
713725
# XXX what is s_hist?

0 commit comments

Comments
 (0)