From 0ffc54b1f2c3e52d3836037d22ec096d2e688c28 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 19 Oct 2023 18:21:06 -0600 Subject: [PATCH 1/5] fix: Prevent text wrapping when already within width --- table2ascii/table_to_ascii.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index dd092e2..9ff4aed 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -130,6 +130,11 @@ def __determine_alignments( return list(alignments) + def __widest_line(self, value: SupportsStr) -> int: + """Returns the width of the longest line in a multi-line string""" + text = str(value) + return max(self.__str_width(line) for line in text.splitlines()) if len(text) else 0 + def __auto_column_widths(self) -> list[int]: """Get the minimum number of characters needed for the values in each column in the table with 1 space of padding on each side. @@ -138,18 +143,13 @@ def __auto_column_widths(self) -> list[int]: The minimum number of characters needed for each column """ - def widest_line(value: SupportsStr) -> int: - """Returns the width of the longest line in a multi-line string""" - text = str(value) - return max(self.__str_width(line) for line in text.splitlines()) if len(text) else 0 - def get_column_width(row: Sequence[SupportsStr], column: int) -> int: """Get the width of a cell in a column""" value = row[column] next_value = row[column + 1] if column < self.__columns - 1 else None if value is Merge.LEFT or next_value is Merge.LEFT: return 0 - return widest_line(value) + return self.__widest_line(value) column_widths = [] # get the width necessary for each column @@ -306,7 +306,10 @@ def __wrap_long_lines_in_merged_cells( if row[other_col_index] is not Merge.LEFT: break merged_width += self.__column_widths[other_col_index] + len(column_separator) - cell = textwrap.fill(str(cell), merged_width - self.__cell_padding * 2) + widest_line = self.__widest_line(cell) + max_content_width = merged_width - self.__cell_padding * 2 + if widest_line > max_content_width: + cell = textwrap.fill(str(cell), max_content_width) wrapped_row.append(cell) return wrapped_row From bd0da35dc557c3cb47d4613b83b60a2c73d33831 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 19 Oct 2023 18:37:20 -0600 Subject: [PATCH 2/5] refactor: change variable name, ensure string type --- table2ascii/table_to_ascii.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index 9ff4aed..b547f5f 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -306,10 +306,11 @@ def __wrap_long_lines_in_merged_cells( if row[other_col_index] is not Merge.LEFT: break merged_width += self.__column_widths[other_col_index] + len(column_separator) - widest_line = self.__widest_line(cell) + cell = str(cell) + widest_line_width = self.__widest_line(cell) max_content_width = merged_width - self.__cell_padding * 2 - if widest_line > max_content_width: - cell = textwrap.fill(str(cell), max_content_width) + if widest_line_width > max_content_width: + cell = textwrap.fill(cell, max_content_width) wrapped_row.append(cell) return wrapped_row From d668717aa2dd5a02e43e2d6673430380a7e4549e Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 19 Oct 2023 18:45:07 -0600 Subject: [PATCH 3/5] Update test_convert.py --- tests/test_convert.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_convert.py b/tests/test_convert.py index 3030cd6..d510a1a 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -305,3 +305,22 @@ def test_east_asian_wide_characters_and_zero_width_no_wcwidth(): "╚════╩═══════════════╝" ) assert text == expected + + +def test_multiline_cells_with_wrappable_lines(): + text = t2a( + header=["Test"], + body=[["Line One...\nSecond Line...\nLineNumThree\nLineFour\nFive FinalLine"]], + ) + expected = ( + "╔════════════════╗\n" + "║ Test ║\n" + "╟────────────────╢\n" + "║ Line One... ║\n" + "║ Second Line... ║\n" + "║ LineNumThree ║\n" + "║ LineFour ║\n" + "║ Five FinalLine ║\n" + "╚════════════════╝" + ) + assert text == expected From 24f55047546171535d7845b4b3a2197636b35a82 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 19 Oct 2023 18:55:55 -0600 Subject: [PATCH 4/5] refactor: comments and simplification --- table2ascii/table_to_ascii.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index b547f5f..e918d4e 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -307,10 +307,11 @@ def __wrap_long_lines_in_merged_cells( break merged_width += self.__column_widths[other_col_index] + len(column_separator) cell = str(cell) - widest_line_width = self.__widest_line(cell) - max_content_width = merged_width - self.__cell_padding * 2 - if widest_line_width > max_content_width: - cell = textwrap.fill(cell, max_content_width) + # if the text is too wide, wrap it + max_text_width = merged_width - self.__cell_padding * 2 + if self.__widest_line(cell) > max_text_width: + cell = textwrap.fill(cell, max_text_width) + # add the wrapped cell to the row wrapped_row.append(cell) return wrapped_row From 73e2e19b18b0f1f1742cc5ceec761c09be5e221e Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 19 Oct 2023 19:00:49 -0600 Subject: [PATCH 5/5] refactor: rename variable again --- table2ascii/table_to_ascii.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index e918d4e..65dfb8d 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -308,9 +308,9 @@ def __wrap_long_lines_in_merged_cells( merged_width += self.__column_widths[other_col_index] + len(column_separator) cell = str(cell) # if the text is too wide, wrap it - max_text_width = merged_width - self.__cell_padding * 2 - if self.__widest_line(cell) > max_text_width: - cell = textwrap.fill(cell, max_text_width) + inner_cell_width = merged_width - self.__cell_padding * 2 + if self.__widest_line(cell) > inner_cell_width: + cell = textwrap.fill(cell, inner_cell_width) # add the wrapped cell to the row wrapped_row.append(cell) return wrapped_row