Skip to content

Commit 54849cb

Browse files
Added 'insert_blank_line_after_input' configuration option and fixed a few __pt_repr__ formatting issues.
1 parent 0f57868 commit 54849cb

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

ptpython/python_input.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def __init__(
254254

255255
self.exit_message: str = "Do you really want to exit?"
256256
self.insert_blank_line_after_output: bool = True # (For the REPL.)
257+
self.insert_blank_line_after_input: bool = False # (For the REPL.)
257258

258259
# The buffers.
259260
self.default_buffer = self._create_buffer()
@@ -640,6 +641,11 @@ def get_values():
640641
for s in self.all_prompt_styles
641642
),
642643
),
644+
simple_option(
645+
title="Blank line after input",
646+
description="Insert a blank line after the input.",
647+
field_name="insert_blank_line_after_input",
648+
),
643649
simple_option(
644650
title="Blank line after output",
645651
description="Insert a blank line after the output.",

ptpython/repl.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ def pre_run(
115115
def _process_text(self, line: str) -> None:
116116

117117
if line and not line.isspace():
118+
if self.insert_blank_line_after_input:
119+
self.app.output.write("\n")
120+
118121
try:
119122
# Eval and print.
120123
self._execute(line)
@@ -181,45 +184,49 @@ def show_result(self, result: object) -> None:
181184
Show __repr__ for an `eval` result.
182185
"""
183186
out_prompt = to_formatted_text(self.get_output_prompt())
184-
result_repr = to_formatted_text("%r\n" % (result,))
187+
188+
# If the repr is valid Python code, use the Pygments lexer.
189+
result_repr = repr(result)
190+
try:
191+
compile(result_repr, "", "eval")
192+
except SyntaxError:
193+
formatted_result_repr = to_formatted_text(result_repr)
194+
else:
195+
formatted_result_repr = to_formatted_text(
196+
PygmentsTokens(list(_lex_python_result(result_repr)))
197+
)
185198

186199
# If __pt_repr__ is present, take this. This can return
187200
# prompt_toolkit formatted text.
188201
if hasattr(result, "__pt_repr__"):
189202
try:
190-
result_repr = to_formatted_text(getattr(result, "__pt_repr__")())
191-
if isinstance(result_repr, list):
192-
result_repr = FormattedText(result_repr)
203+
formatted_result_repr = to_formatted_text(
204+
getattr(result, "__pt_repr__")()
205+
)
206+
if isinstance(formatted_result_repr, list):
207+
formatted_result_repr = FormattedText(formatted_result_repr)
193208
except:
194209
pass
195210

196-
# If we have a string so far, and it's valid Python code,
197-
# use the Pygments lexer.
198-
if isinstance(result, str):
199-
try:
200-
compile(result, "", "eval")
201-
except SyntaxError:
202-
pass
203-
else:
204-
result = PygmentsTokens(list(_lex_python_result(result)))
205-
206211
# Align every line to the prompt.
207212
line_sep = "\n" + " " * fragment_list_width(out_prompt)
208213
indented_repr: StyleAndTextTuples = []
209214

210-
for fragment in split_lines(result_repr):
215+
lines = list(split_lines(formatted_result_repr))
216+
217+
for i, fragment in enumerate(lines):
211218
indented_repr.extend(fragment)
212-
indented_repr.append(("", line_sep))
213-
if indented_repr:
214-
indented_repr.pop()
215-
indented_repr.append(("", "\n"))
219+
220+
# Add indentation separator between lines, not after the last line.
221+
if i != len(lines) - 1:
222+
indented_repr.append(("", line_sep))
216223

217224
# Write output tokens.
218225
if self.enable_syntax_highlighting:
219226
formatted_output = merge_formatted_text([out_prompt, indented_repr])
220227
else:
221228
formatted_output = FormattedText(
222-
out_prompt + [("", fragment_list_to_text(result_repr))]
229+
out_prompt + [("", fragment_list_to_text(formatted_result_repr))]
223230
)
224231

225232
print_formatted_text(

0 commit comments

Comments
 (0)