From a736ad78adfbbc273299cdbe8d31727baf66a1b7 2013-07-31 01:40:26 From: Alex Rudy Date: 2013-07-31 01:40:26 Subject: [PATCH] Adds p.break_ for explicit newlines in lib.pretty p.break_() explicitly includes a newline while pretty printing, and maintains indentation. A simple test is included to show this working. --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 36d7824..ee5e059 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -229,7 +229,17 @@ class PrettyPrinter(_PrettyPrinterBase): self.buffer.append(Breakable(sep, width, self)) self.buffer_width += width self._break_outer_groups() - + + def break_(self): + """ + Explicitly insert a newline into the output, maintaining correct indentation. + """ + self.flush() + self.output.write(self.newline) + self.output.write(' ' * self.indentation) + self.output_width = self.indentation + self.buffer_width = 0 + def begin_group(self, indent=0, open=''): """ diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index 689ae48..db5224c 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -58,6 +58,13 @@ class NoModule(object): NoModule.__module__ = None +class Breaking(object): + def _repr_pretty_(self, p, cycle): + with p.group(4,"TG: ",":"): + p.text("Breaking(") + p.break_() + p.text(")") + def test_indentation(): """Test correct indentation in groups""" @@ -118,3 +125,11 @@ def test_pprint_nomod(): """ output = pretty.pretty(NoModule) nt.assert_equal(output, 'NoModule') + +def test_pprint_break(): + """ + Test that p.break_ produces expected output + """ + output = pretty.pretty(Breaking()) + expected = "TG: Breaking(\n ):" + nt.assert_equal(output, expected)