Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ New features:

Fixes:
* #1057: Preserve the indentation of pasted code
* #1050: Keep output without a trailing newline with the prompt that
follows it.

Changes to dependencies:

Expand Down
74 changes: 50 additions & 24 deletions bpython/curtsiesfrontend/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ def __init__(

# formatted version of lines in the buffer kept around so we can
# unhighlight parens using self.reprint_line as called by bpython.Repl
self.display_buffer: list[FmtStr] = []
self.display_buffer: list[tuple[str | FmtStr, FmtStr]] = []
# Remember the wrapped span and prefix when an empty prompt is undone.
self._empty_prompt_output: dict[int, tuple[int, str | FmtStr]] = {}

# how many times display has been scrolled down
# because there wasn't room to display everything
Expand Down Expand Up @@ -963,10 +965,17 @@ def on_enter(self, new_code=True, reset_rl_history=True):
if reset_rl_history:
self.rl_history.reset()

self.history.append(self.current_line)
self.all_logical_lines.append((self.current_line, LineType.INPUT))
self._record_input(self.current_line)
self.push(self.current_line, insert_into_history=new_code)

def _record_input(self, line):
output = self.current_stdouterr_line
if output:
text = output.s if isinstance(output, FmtStr) else output
self.all_logical_lines.append((text, LineType.OUTPUT))
self.history.append(line)
self.all_logical_lines.append((line, LineType.INPUT))

def on_tab(self, back=False):
"""Do something on tab key
taken from bpython.cli
Expand Down Expand Up @@ -1287,9 +1296,12 @@ def push(self, line, insert_into_history=True) -> bool:
line,
display_line,
)
self.display_buffer.append(display_line)
else:
self.display_buffer.append(fmtstr(line))
display_line = fmtstr(line)

output = self.current_stdouterr_line
self.display_buffer.append((output, display_line))
self.current_stdouterr_line = ""

if insert_into_history:
self.insert_into_history(line)
Expand All @@ -1305,7 +1317,11 @@ def push(self, line, insert_into_history=True) -> bool:
if c:
logger.debug("finished - buffer cleared")
self.cursor_offset = 0
start = len(self.display_lines)
self.display_lines.extend(self.display_buffer_lines)
if output and self.buffer == [""]:
end = len(self.display_lines)
self._empty_prompt_output[end] = start, output
self.display_buffer = []
self.buffer = []

Expand Down Expand Up @@ -1362,17 +1378,17 @@ def unhighlight_paren(self):
logger.debug("trying to unhighlight a paren on line %r", lineno)
logger.debug("with these tokens: %r", saved_tokens)
new = bpythonparse(pygformat(saved_tokens, self.formatter))
self.display_buffer[lineno] = self.display_buffer[
lineno
].setslice_with_length(
0, len(new), new, len(self.display_buffer[lineno])
output, line = self.display_buffer[lineno]
self.display_buffer[lineno] = (
output,
line.setslice_with_length(0, len(new), new, len(line)),
)

def clear_current_block(self, remove_from_history=True):
self.display_buffer = []
if remove_from_history:
del self.history[-len(self.buffer) :]
del self.all_logical_lines[-len(self.buffer) :]
for output, _ in reversed(self.display_buffer):
self._rewind_input(output)
self.display_buffer = []
self.buffer = []
self.cursor_offset = 0
self.saved_indent = 0
Expand Down Expand Up @@ -1474,12 +1490,11 @@ def lines_for_display(self):
def display_buffer_lines(self):
"""The display lines (wrapped, colored, +prompts) of current buffer"""
lines = []
for display_line in self.display_buffer:
for output, display_line in self.display_buffer:
prompt = func_for_letter(self.config.color_scheme["prompt"])
more = func_for_letter(self.config.color_scheme["prompt_more"])
display_line = (
more(self.ps2) if lines else prompt(self.ps1)
) + display_line
prompt_text = more(self.ps2) if lines else prompt(self.ps1)
display_line = output + prompt_text + display_line
for line in paint.display_linize(display_line, self.width):
lines.append(line)
return lines
Expand Down Expand Up @@ -1953,8 +1968,10 @@ def cpos(self):
def reprint_line(self, lineno, tokens):
logger.debug("calling reprint line with %r %r", lineno, tokens)
if self.config.syntax:
self.display_buffer[lineno] = bpythonparse(
pygformat(tokens, self.formatter)
output, _ = self.display_buffer[lineno]
self.display_buffer[lineno] = (
output,
bpythonparse(pygformat(tokens, self.formatter)),
)

def take_back_buffer_line(self):
Expand All @@ -1967,16 +1984,24 @@ def take_back_buffer_line(self):
indent = self.predicted_indent(line)
self._current_line = indent * " "
self.cursor_offset = len(self.current_line)
self.display_buffer.pop()
output, _ = self.display_buffer.pop()
self.buffer.pop()
self._rewind_input(output)

def _rewind_input(self, output):
self.history.pop()
self.all_logical_lines.pop()
if output:
self.all_logical_lines.pop()
self.current_stdouterr_line = output + self.current_stdouterr_line

def take_back_empty_line(self):
assert self.history and not self.history[-1]
self.history.pop()
self.display_lines.pop()
self.all_logical_lines.pop()
start, output = self._empty_prompt_output.pop(
len(self.display_lines), (len(self.display_lines) - 1, "")
)
del self.display_lines[start:]
self._rewind_input(output)

def prompt_undo(self):
if self.buffer:
Expand All @@ -1994,8 +2019,7 @@ def prompt_for_undo():
def redo(self) -> None:
if self.redo_stack:
temp = self.redo_stack.pop()
self.history.append(temp)
self.all_logical_lines.append((temp, LineType.INPUT))
self._record_input(temp)
self.push(temp)
else:
self.status_bar.message("Nothing to redo.")
Expand All @@ -2008,6 +2032,8 @@ def reevaluate(self, new_code=False):
old_display_lines = self.display_lines
self.history = []
self.display_lines = []
self._empty_prompt_output.clear()
self.current_stdouterr_line = ""
self.all_logical_lines = []

if not self.weak_rewind:
Expand Down
28 changes: 28 additions & 0 deletions bpython/test/test_curtsies_painting.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ def test_run_line(self):
finally:
sys.stdout = orig_stdout

def test_submit_prompt_after_output_without_newline(self):
self.repl.send_to_stdouterr("hello")
self.repl.on_enter(new_code=False)
self.repl.on_enter(new_code=False)

self.assert_paint_ignoring_formatting(
fsarray(["hello>>> ", ">>> ", ">>> "], width=10), (2, 4)
)

def test_undo_empty_prompt_preserves_wrapped_output(self):
self.repl.send_to_stdouterr("abcdefgh")
self.repl.on_enter(new_code=False)
self.repl.prompt_undo()

self.assert_paint_ignoring_formatting(
fsarray(["abcdefgh>>", "> "], width=10), (1, 2)
)

def test_session_keeps_output_without_newline(self):
self.repl.send_to_stdouterr(green("hello"))
self.repl.current_line = "pass"
self.repl.on_enter(new_code=False)

self.assertEqual(
self.repl.get_session_formatted_for_file(),
"# OUT: hello\npass\n### ",
)

def test_completion(self):
self.repl.height, self.repl.width = (5, 32)
self.repl.current_line = "an"
Expand Down
Loading