@@ -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