diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9cea686..66571a9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ ci: repos: - repo: https://github.com/psf/black - rev: 23.7.0 + rev: 23.9.1 hooks: - id: black name: Running black in all files. @@ -19,7 +19,7 @@ repos: name: Running isort in all files. - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-ast name: Check if python files are valid syntax for the ast parser diff --git a/pyproject.toml b/pyproject.toml index e7343bd..267f35a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "table2ascii" -version = "1.1.2" +version = "1.1.3" authors = [{name = "Jonah Lawrence", email = "jonah@freshidea.com"}] description = "Convert 2D Python lists into Unicode/ASCII tables" readme = "README.md" diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index dd092e2..65dfb8d 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,12 @@ 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) + cell = str(cell) + # if the text is too wide, wrap it + 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 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
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: