Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sections docs and test #2544

Merged
merged 3 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow a `max_depth` argument to be passed to the `install()` hook https://github.com/Textualize/rich/issues/2486
- Document using `None` as name in `__rich_repr__` for tuple positional args https://github.com/Textualize/rich/pull/2379
- Add `font_aspect_ratio` parameter in SVG export https://github.com/Textualize/rich/pull/2539/files
- Added `Table.add_section` method. https://github.com/Textualize/rich/pull/2544

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion docs/source/tables.rst
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions rich/table.py
Expand Up @@ -462,6 +462,12 @@ def add_cell(column: Column, renderable: "RenderableType") -> None:
)
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":
Expand Down
26 changes: 26 additions & 0 deletions tests/test_table.py
Expand Up @@ -209,6 +209,32 @@ 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(
width=80,
force_terminal=True,
color_system="truecolor",
legacy_windows=False,
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)
Expand Down