From 36fa43da752f3e0fa435f0e561cd8fc4169b7d3e Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 23 Sep 2022 15:21:28 +0100 Subject: [PATCH] sections docs and test --- docs/source/tables.rst | 2 +- rich/table.py | 6 ++++++ tests/test_table.py | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/source/tables.rst b/docs/source/tables.rst index 9cebe60c..c4a1b791 100644 --- a/docs/source/tables.rst +++ b/docs/source/tables.rst @@ -96,7 +96,7 @@ Lines By default, Tables will show a line under the header only. If you want to show lines between all rows add ``show_lines=True`` to the constructor. -You can also force a line on the next row by setting ``end_section=True`` on the call to ``add_row()``. +You can also force a line on the next row by setting ``end_section=True`` on the call to :meth:`~rich.table.Table.add_row`, or by calling the :meth:`~rich.table.Table.add_section` to add a line between the current and subsequent rows. Empty Tables diff --git a/rich/table.py b/rich/table.py index 9d067ba5..578d3d63 100644 --- a/rich/table.py +++ b/rich/table.py @@ -462,6 +462,12 @@ class Table(JupyterMixin): ) self.rows.append(Row(style=style, end_section=end_section)) + def add_section(self) -> None: + """Add a new section (draw a line after current row).""" + + if self.rows: + self.rows[-1].end_section = True + def __rich_console__( self, console: "Console", options: "ConsoleOptions" ) -> "RenderResult": diff --git a/tests/test_table.py b/tests/test_table.py index e376e167..808c2d8f 100644 --- a/tests/test_table.py +++ b/tests/test_table.py @@ -209,6 +209,26 @@ def test_table_show_header_false_substitution(box, result): assert output == result +def test_section(): + table = Table("foo") + table.add_section() # Null-op + table.add_row("row1") + table.add_row("row2", end_section=True) + table.add_row("row3") + table.add_row("row4") + table.add_section() + table.add_row("row5") + table.add_section() # Null-op + + console = Console(record=True) + console.print(table) + output = console.export_text() + print(repr(output)) + expected = "┏━━━━━━┓\n┃ foo ┃\n┡━━━━━━┩\n│ row1 │\n│ row2 │\n├──────┤\n│ row3 │\n│ row4 │\n├──────┤\n│ row5 │\n└──────┘\n" + + assert output == expected + + if __name__ == "__main__": render = render_tables() print(render)