@@ -115,6 +115,9 @@ def pre_run(
115
115
def _process_text (self , line : str ) -> None :
116
116
117
117
if line and not line .isspace ():
118
+ if self .insert_blank_line_after_input :
119
+ self .app .output .write ("\n " )
120
+
118
121
try :
119
122
# Eval and print.
120
123
self ._execute (line )
@@ -181,45 +184,49 @@ def show_result(self, result: object) -> None:
181
184
Show __repr__ for an `eval` result.
182
185
"""
183
186
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
+ )
185
198
186
199
# If __pt_repr__ is present, take this. This can return
187
200
# prompt_toolkit formatted text.
188
201
if hasattr (result , "__pt_repr__" ):
189
202
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 )
193
208
except :
194
209
pass
195
210
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
-
206
211
# Align every line to the prompt.
207
212
line_sep = "\n " + " " * fragment_list_width (out_prompt )
208
213
indented_repr : StyleAndTextTuples = []
209
214
210
- for fragment in split_lines (result_repr ):
215
+ lines = list (split_lines (formatted_result_repr ))
216
+
217
+ for i , fragment in enumerate (lines ):
211
218
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 ))
216
223
217
224
# Write output tokens.
218
225
if self .enable_syntax_highlighting :
219
226
formatted_output = merge_formatted_text ([out_prompt , indented_repr ])
220
227
else :
221
228
formatted_output = FormattedText (
222
- out_prompt + [("" , fragment_list_to_text (result_repr ))]
229
+ out_prompt + [("" , fragment_list_to_text (formatted_result_repr ))]
223
230
)
224
231
225
232
print_formatted_text (
0 commit comments