From 16b3830408df98db41967774e59175a1f919ce25 Mon Sep 17 00:00:00 2001 From: TomJGooding <101601846+TomJGooding@users.noreply.github.com> Date: Thu, 3 Oct 2024 16:05:56 +0100 Subject: [PATCH] fix(table): highlight columns added by add_row --- CHANGELOG.md | 6 ++++++ rich/table.py | 2 +- tests/test_table.py | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bae8d5..1915a9d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Fixed `Table` columns not highlighting when added by `add_row` https://github.com/Textualize/rich/issues/3517 + ## [13.9.1] - 2024-10-01 ### Fixed diff --git a/rich/table.py b/rich/table.py index 10af2e6f..7b1fa9e4 100644 --- a/rich/table.py +++ b/rich/table.py @@ -451,7 +451,7 @@ class Table(JupyterMixin): ] for index, renderable in enumerate(cell_renderables): if index == len(columns): - column = Column(_index=index) + column = Column(_index=index, highlight=self.highlight) for _ in self.rows: add_cell(column, Text("")) self.columns.append(column) diff --git a/tests/test_table.py b/tests/test_table.py index 79033f21..8767283c 100644 --- a/tests/test_table.py +++ b/tests/test_table.py @@ -363,6 +363,25 @@ def test_placement_table_box_elements(show_header, show_footer, expected): assert output == expected +def test_columns_highlight_added_by_add_row() -> None: + """Regression test for https://github.com/Textualize/rich/issues/3517""" + table = Table(show_header=False, highlight=True) + table.add_row("1", repr("FOO")) + + assert table.columns[0].highlight == table.highlight + assert table.columns[1].highlight == table.highlight + + console = Console(record=True) + console.print(table) + output = console.export_text(styles=True) + print(repr(output)) + + expected = ( + "┌───┬───────┐\n│ \x1b[1;36m1\x1b[0m │ \x1b[32m'FOO'\x1b[0m │\n└───┴───────┘\n" + ) + assert output == expected + + if __name__ == "__main__": render = render_tables() print(render)