From bf53d8f58929159686af937dfb64d21d026d80b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 18:20:49 +0100 Subject: [PATCH 01/10] test: added tests for max_table_width computation --- tests/test_prettytable.py | 65 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 214c5c0..54e96d3 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -2011,14 +2011,69 @@ def test_max_table_width(self) -> None: assert ( table.get_string().strip() == """ -+-----+ -| Fie | -+-----+ -| 0 | -+-----+ ++---+ +| F | ++---+ +| 0 | ++---+ """.strip() ) + def test_compute_table_width(self) -> None: + table = PrettyTable() + table.max_table_width = 5 + table.add_row([0]) + assert table._compute_table_width() <= table.max_table_width + + def test_max_table_width_wide(self) -> None: + table = PrettyTable() + table.max_table_width = 52 + table.add_row( + [ + 0, + 0, + 0, + 0, + 0, + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " + "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " + "erat, sed diam voluptua", + ] + ) + + assert ( + table.get_string().strip() + == """ ++---+---+---+---+---+------------------------------+ +| F | F | F | F | F | Field 6 | ++---+---+---+---+---+------------------------------+ +| 0 | 0 | 0 | 0 | 0 | Lorem ipsum dolor sit amet, | +| | | | | | consetetur sadipscing elitr, | +| | | | | | sed diam nonumy eirmod | +| | | | | | tempor invidunt ut labore et | +| | | | | | dolore magna aliquyam erat, | +| | | | | | sed diam voluptua | ++---+---+---+---+---+------------------------------+""".strip() + ) + assert table._compute_table_width() <= table.max_table_width + + def test_compute_table_width_wide(self) -> None: + table = PrettyTable() + table.max_table_width = 52 + table.add_row( + [ + 0, + 0, + 0, + 0, + 0, + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " + "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " + "erat, sed diam voluptua", + ] + ) + assert table._compute_table_width() <= table.max_table_width + class TestRowEndSection: def test_row_end_section(self) -> None: From cd1e1bf1f5e3f67dffe05ac2153363d73e0da67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 18:21:22 +0100 Subject: [PATCH 02/10] fix: consider vrules in _conpute_table_width --- src/prettytable/prettytable.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index df1cb86..f36744f 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1558,13 +1558,22 @@ def _format_value(self, field, value): return formatter(field, value) def _compute_table_width(self, options): - table_width = 2 if options["vrules"] in (FRAME, ALL) else 0 + if options["vrules"] == FRAME: + table_width = 2 + if options["vrules"] == ALL: + table_width = 1 + else: + table_width = 0 per_col_padding = sum(self._get_padding_widths(options)) for index, fieldname in enumerate(self.field_names): if not options["fields"] or ( options["fields"] and fieldname in options["fields"] ): - table_width += self._widths[index] + per_col_padding + table_width += ( + self._widths[index] + per_col_padding + 1 + if options["vrules"] == ALL + else 0 + ) return table_width def _compute_widths(self, rows, options) -> None: From 94f92f7c2258667ccdce8feac38f8fb5b6c696d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 18:22:56 +0100 Subject: [PATCH 03/10] refactor: sum padding_widths only once --- src/prettytable/prettytable.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index f36744f..c2ac3c1 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1599,6 +1599,7 @@ def _compute_widths(self, rows, options) -> None: widths[index] = max(widths[index], self.min_width[fieldname]) self._widths = widths + per_col_padding = sum(self._get_padding_widths(options)) # Are we exceeding max_table_width? if self._max_table_width: table_width = self._compute_table_width(options) @@ -1611,9 +1612,7 @@ def _compute_widths(self, rows, options) -> None: # Are we under min_table_width or title width? if self._min_table_width or options["title"]: if options["title"]: - title_width = len(options["title"]) + sum( - self._get_padding_widths(options) - ) + title_width = len(options["title"]) + per_col_padding if options["vrules"] in (FRAME, ALL): title_width += 2 else: @@ -1628,9 +1627,7 @@ def _compute_widths(self, rows, options) -> None: borders = 0 # Subtract padding for each column and borders - min_width -= ( - sum([sum(self._get_padding_widths(options)) for _ in widths]) + borders - ) + min_width -= sum([per_col_padding for _ in widths]) + borders # What is being scaled is content so we sum column widths content_width = sum(widths) or 1 From a220c6bec4d77242160f7252fc472195f1e4d02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 20:34:15 +0100 Subject: [PATCH 04/10] tests: fixed tests --- tests/test_prettytable.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 54e96d3..0985419 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -2019,12 +2019,6 @@ def test_max_table_width(self) -> None: """.strip() ) - def test_compute_table_width(self) -> None: - table = PrettyTable() - table.max_table_width = 5 - table.add_row([0]) - assert table._compute_table_width() <= table.max_table_width - def test_max_table_width_wide(self) -> None: table = PrettyTable() table.max_table_width = 52 @@ -2055,24 +2049,6 @@ def test_max_table_width_wide(self) -> None: | | | | | | sed diam voluptua | +---+---+---+---+---+------------------------------+""".strip() ) - assert table._compute_table_width() <= table.max_table_width - - def test_compute_table_width_wide(self) -> None: - table = PrettyTable() - table.max_table_width = 52 - table.add_row( - [ - 0, - 0, - 0, - 0, - 0, - "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " - "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " - "erat, sed diam voluptua", - ] - ) - assert table._compute_table_width() <= table.max_table_width class TestRowEndSection: From b4dfe920d4d435bb965860044c97df2d1ddd2a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 21:02:12 +0100 Subject: [PATCH 05/10] fix: downscaling of table widths when max_table_width is set --- src/prettytable/prettytable.py | 10 +++++++-- tests/test_prettytable.py | 39 +++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index c2ac3c1..b9f8823 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1605,8 +1605,14 @@ def _compute_widths(self, rows, options) -> None: table_width = self._compute_table_width(options) if table_width > self._max_table_width: # Shrink widths in proportion - scale = 1.0 * self._max_table_width / table_width - widths = [int(w * scale) for w in widths] + if options["vrules"] == FRAME: + vrulers = 2 + elif options["vrules"] == ALL: + vrulers = len(widths) - 1 + scale = ( + self._max_table_width - per_col_padding * len(widths) - vrulers + ) / (table_width - per_col_padding * len(widths) - vrulers) + widths = [max(1, int(w * scale)) for w in widths] self._widths = widths # Are we under min_table_width or title width? diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index 0985419..ca4ef22 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -2008,14 +2008,15 @@ def test_max_table_width(self) -> None: table.max_table_width = 5 table.add_row([0]) + # FIXME: Table is wider than table.max_table_width assert ( table.get_string().strip() == """ -+---+ -| F | -+---+ -| 0 | -+---+ ++----+ +| Fi | ++----+ +| 0 | ++----+ """.strip() ) @@ -2050,6 +2051,34 @@ def test_max_table_width_wide(self) -> None: +---+---+---+---+---+------------------------------+""".strip() ) + def test_max_table_width_wide2(self) -> None: + table = PrettyTable() + table.max_table_width = 70 + table.add_row( + [ + "Lorem", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + "ipsum", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + "dolor", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + ] + ) + + assert ( + table.get_string().strip() + == """ ++---+-----------------+---+-----------------+---+-----------------+ +| F | Field 2 | F | Field 4 | F | Field 6 | ++---+-----------------+---+-----------------+---+-----------------+ +| L | Lorem ipsum | i | Lorem ipsum | d | Lorem ipsum | +| o | dolor sit amet, | p | dolor sit amet, | o | dolor sit amet, | +| r | consetetur | s | consetetur | l | consetetur | +| e | sadipscing | u | sadipscing | o | sadipscing | +| m | elitr, sed diam | m | elitr, sed diam | r | elitr, sed diam | ++---+-----------------+---+-----------------+---+-----------------+""".strip() + ) + class TestRowEndSection: def test_row_end_section(self) -> None: From 949abc7735c46bdc70cf45718a49e90a9dde804f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 22:44:25 +0100 Subject: [PATCH 06/10] fix: compute_table_width returned 0 for FRAME and NONE --- src/prettytable/prettytable.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index b9f8823..ed30f4e 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1570,9 +1570,9 @@ def _compute_table_width(self, options): options["fields"] and fieldname in options["fields"] ): table_width += ( - self._widths[index] + per_col_padding + 1 - if options["vrules"] == ALL - else 0 + self._widths[index] + + per_col_padding + + (1 if options["vrules"] == ALL else 0) ) return table_width @@ -1609,6 +1609,8 @@ def _compute_widths(self, rows, options) -> None: vrulers = 2 elif options["vrules"] == ALL: vrulers = len(widths) - 1 + else: + vrulers = 0 scale = ( self._max_table_width - per_col_padding * len(widths) - vrulers ) / (table_width - per_col_padding * len(widths) - vrulers) From 4d4481aaadd845edd804df0fcbc0fc3ff805d124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Sun, 3 Mar 2024 23:09:49 +0100 Subject: [PATCH 07/10] fix: more tests for vrules set to frame or none there is always an additional column, if ruler is visible or not --- src/prettytable/prettytable.py | 13 ++----- tests/test_prettytable.py | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index ed30f4e..85a173e 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1569,11 +1569,7 @@ def _compute_table_width(self, options): if not options["fields"] or ( options["fields"] and fieldname in options["fields"] ): - table_width += ( - self._widths[index] - + per_col_padding - + (1 if options["vrules"] == ALL else 0) - ) + table_width += self._widths[index] + per_col_padding + 1 return table_width def _compute_widths(self, rows, options) -> None: @@ -1605,12 +1601,7 @@ def _compute_widths(self, rows, options) -> None: table_width = self._compute_table_width(options) if table_width > self._max_table_width: # Shrink widths in proportion - if options["vrules"] == FRAME: - vrulers = 2 - elif options["vrules"] == ALL: - vrulers = len(widths) - 1 - else: - vrulers = 0 + vrulers = len(widths) - 1 scale = ( self._max_table_width - per_col_padding * len(widths) - vrulers ) / (table_width - per_col_padding * len(widths) - vrulers) diff --git a/tests/test_prettytable.py b/tests/test_prettytable.py index ca4ef22..594fdd3 100644 --- a/tests/test_prettytable.py +++ b/tests/test_prettytable.py @@ -2079,6 +2079,70 @@ def test_max_table_width_wide2(self) -> None: +---+-----------------+---+-----------------+---+-----------------+""".strip() ) + def test_max_table_width_wide_vrules_frame(self) -> None: + table = PrettyTable() + table.max_table_width = 52 + table.vrules = FRAME + table.add_row( + [ + 0, + 0, + 0, + 0, + 0, + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " + "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " + "erat, sed diam voluptua", + ] + ) + + assert ( + table.get_string().strip() + == """ ++--------------------------------------------------+ +| F F F F F Field 6 | ++--------------------------------------------------+ +| 0 0 0 0 0 Lorem ipsum dolor sit amet, | +| consetetur sadipscing elitr, | +| sed diam nonumy eirmod | +| tempor invidunt ut labore et | +| dolore magna aliquyam erat, | +| sed diam voluptua | ++--------------------------------------------------+""".strip() + ) + + def test_max_table_width_wide_vrules_none(self) -> None: + table = PrettyTable() + table.max_table_width = 52 + table.vrules = NONE + table.add_row( + [ + 0, + 0, + 0, + 0, + 0, + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam " + "nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " + "erat, sed diam voluptua", + ] + ) + + assert ( + table.get_string().strip() + == """ +---------------------------------------------------- + F F F F F Field 6 +---------------------------------------------------- + 0 0 0 0 0 Lorem ipsum dolor sit amet, + consetetur sadipscing elitr, + sed diam nonumy eirmod + tempor invidunt ut labore et + dolore magna aliquyam erat, + sed diam voluptua +----------------------------------------------------""".strip() # noqa: W291 + ) + class TestRowEndSection: def test_row_end_section(self) -> None: From c1fd089e900ba088c73fb680441b98e339f2babc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Wed, 27 Mar 2024 15:01:52 +0100 Subject: [PATCH 08/10] refactor: avoid temp var Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- src/prettytable/prettytable.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index 85a173e..339231b 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1605,8 +1605,7 @@ def _compute_widths(self, rows, options) -> None: scale = ( self._max_table_width - per_col_padding * len(widths) - vrulers ) / (table_width - per_col_padding * len(widths) - vrulers) - widths = [max(1, int(w * scale)) for w in widths] - self._widths = widths + self._widths = [max(1, int(w * scale)) for w in widths] # Are we under min_table_width or title width? if self._min_table_width or options["title"]: From 4f59f39f9a194b3b0df0897ba1bc290a93337c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Wed, 27 Mar 2024 15:20:22 +0100 Subject: [PATCH 09/10] refactor: use var for same value in numerator and denominator --- src/prettytable/prettytable.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index 339231b..c435533 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1601,10 +1601,10 @@ def _compute_widths(self, rows, options) -> None: table_width = self._compute_table_width(options) if table_width > self._max_table_width: # Shrink widths in proportion - vrulers = len(widths) - 1 - scale = ( - self._max_table_width - per_col_padding * len(widths) - vrulers - ) / (table_width - per_col_padding * len(widths) - vrulers) + markup_chars = per_col_padding * len(widths) - len(widths) - 1 + scale = (self._max_table_width - markup_chars) / ( + table_width - markup_chars + ) self._widths = [max(1, int(w * scale)) for w in widths] # Are we under min_table_width or title width? From cd8f2b881c6cb362093377ccd39844c89654ed59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20St=C3=BCrtz?= Date: Wed, 27 Mar 2024 15:51:29 +0100 Subject: [PATCH 10/10] fix: presign error --- src/prettytable/prettytable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prettytable/prettytable.py b/src/prettytable/prettytable.py index c435533..0204a7a 100644 --- a/src/prettytable/prettytable.py +++ b/src/prettytable/prettytable.py @@ -1601,7 +1601,7 @@ def _compute_widths(self, rows, options) -> None: table_width = self._compute_table_width(options) if table_width > self._max_table_width: # Shrink widths in proportion - markup_chars = per_col_padding * len(widths) - len(widths) - 1 + markup_chars = per_col_padding * len(widths) + len(widths) - 1 scale = (self._max_table_width - markup_chars) / ( table_width - markup_chars )