From 39ea3ffcd63fa6f199e65387e59066cd7cf2ccba Mon Sep 17 00:00:00 2001 From: Sergio Date: Mon, 9 Mar 2026 07:33:26 -0700 Subject: [PATCH] fix: honor fancy_grid maxcolwidths including cell padding --- tabulate/__init__.py | 8 ++++++++ test/test_output.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 9ee72fe..11ff759 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2256,6 +2256,14 @@ def tabulate( else: # Ignore col width for any 'trailing' columns maxcolwidths = _expand_iterable(maxcolwidths, num_cols, None) + # For fancy_grid, reserve left/right cell padding space before wrapping so + # maxcolwidths caps the rendered column width (issue #354). + if tablefmt == "fancy_grid": + wrap_padding = _table_formats["fancy_grid"].padding * 2 + maxcolwidths = [ + None if w is None else max(1, w - wrap_padding) for w in maxcolwidths + ] + numparses = _expand_numparse(disable_numparse, num_cols) list_of_lists = _wrap_text_to_colwidths( list_of_lists, diff --git a/test/test_output.py b/test/test_output.py index ea3da87..9463880 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -223,6 +223,29 @@ def test_plain_maxheadercolwidths_autowraps(): assert_equal(expected, result) +def test_fancy_grid_maxcolwidth_includes_padding(): + "Output: fancy_grid maxcolwidths caps rendered width including padding" + table = [["The files were concatenated and archived for posterity."]] + expected = "\n".join( + [ + "╒══════════╕", + "│ col │", + "╞══════════╡", + "│ The │", + "│ files │", + "│ were con │", + "│ catenate │", + "│ d and │", + "│ archived │", + "│ for post │", + "│ erity. │", + "╘══════════╛", + ] + ) + result = tabulate(table, headers=["col"], tablefmt="fancy_grid", maxcolwidths=[10]) + assert_equal(expected, result) + + def test_simple(): "Output: simple with headers" expected = "\n".join(