diff --git a/rich/table.py b/rich/table.py index 942175dc3a..0bc7fcfeaa 100644 --- a/rich/table.py +++ b/rich/table.py @@ -702,6 +702,13 @@ def _get_padding_width(self, column_index: int) -> int: if self.collapse_padding: if column_index > 0: pad_left = max(0, pad_left - pad_right) + if not self.pad_edge: + first_column = column_index == 0 + last_column = column_index == len(self.columns) - 1 + if first_column: + pad_left = 0 + if last_column: + pad_right = 0 return pad_left + pad_right def _measure_column( diff --git a/tests/test_table.py b/tests/test_table.py index 8767283c56..e0f039c8da 100644 --- a/tests/test_table.py +++ b/tests/test_table.py @@ -382,6 +382,42 @@ def test_columns_highlight_added_by_add_row() -> None: assert output == expected +def test_pad_edge_false_with_fixed_width_columns() -> None: + """Regression test for https://github.com/Textualize/rich/issues/3892 + + When pad_edge=False with fixed-width columns, the edge columns should not + have extra padding added to their width calculation. + """ + output = io.StringIO() + console = Console( + width=60, + file=output, + force_terminal=True, + legacy_windows=False, + color_system=None, + _environ={}, + ) + + table = Table( + Column('verb', width=5), + Column('noun', width=5), + Column('wh', width=2), + box=None, + pad_edge=False, + expand=False, + show_header=True, + ) + table.add_row('hello', 'world', 'oh') + console.print(table) + + result = output.getvalue().rstrip('\n') + expected = '\n'.join([ + 'verb noun wh', + 'hello world oh' + ]) + assert result == expected + + if __name__ == "__main__": render = render_tables() print(render)