Skip to content

Commit 32a7b6f

Browse files
authored
fix: Prevent text wrapping when already within width (#113)
1 parent c6735c1 commit 32a7b6f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

Diff for: table2ascii/table_to_ascii.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ def __determine_alignments(
130130

131131
return list(alignments)
132132

133+
def __widest_line(self, value: SupportsStr) -> int:
134+
"""Returns the width of the longest line in a multi-line string"""
135+
text = str(value)
136+
return max(self.__str_width(line) for line in text.splitlines()) if len(text) else 0
137+
133138
def __auto_column_widths(self) -> list[int]:
134139
"""Get the minimum number of characters needed for the values in each column in the table
135140
with 1 space of padding on each side.
@@ -138,18 +143,13 @@ def __auto_column_widths(self) -> list[int]:
138143
The minimum number of characters needed for each column
139144
"""
140145

141-
def widest_line(value: SupportsStr) -> int:
142-
"""Returns the width of the longest line in a multi-line string"""
143-
text = str(value)
144-
return max(self.__str_width(line) for line in text.splitlines()) if len(text) else 0
145-
146146
def get_column_width(row: Sequence[SupportsStr], column: int) -> int:
147147
"""Get the width of a cell in a column"""
148148
value = row[column]
149149
next_value = row[column + 1] if column < self.__columns - 1 else None
150150
if value is Merge.LEFT or next_value is Merge.LEFT:
151151
return 0
152-
return widest_line(value)
152+
return self.__widest_line(value)
153153

154154
column_widths = []
155155
# get the width necessary for each column
@@ -306,7 +306,12 @@ def __wrap_long_lines_in_merged_cells(
306306
if row[other_col_index] is not Merge.LEFT:
307307
break
308308
merged_width += self.__column_widths[other_col_index] + len(column_separator)
309-
cell = textwrap.fill(str(cell), merged_width - self.__cell_padding * 2)
309+
cell = str(cell)
310+
# if the text is too wide, wrap it
311+
inner_cell_width = merged_width - self.__cell_padding * 2
312+
if self.__widest_line(cell) > inner_cell_width:
313+
cell = textwrap.fill(cell, inner_cell_width)
314+
# add the wrapped cell to the row
310315
wrapped_row.append(cell)
311316
return wrapped_row
312317

Diff for: tests/test_convert.py

+19
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,22 @@ def test_east_asian_wide_characters_and_zero_width_no_wcwidth():
305305
"╚════╩═══════════════╝"
306306
)
307307
assert text == expected
308+
309+
310+
def test_multiline_cells_with_wrappable_lines():
311+
text = t2a(
312+
header=["Test"],
313+
body=[["Line One...\nSecond Line...\nLineNumThree\nLineFour\nFive FinalLine"]],
314+
)
315+
expected = (
316+
"╔════════════════╗\n"
317+
"║ Test ║\n"
318+
"╟────────────────╢\n"
319+
"║ Line One... ║\n"
320+
"║ Second Line... ║\n"
321+
"║ LineNumThree ║\n"
322+
"║ LineFour ║\n"
323+
"║ Five FinalLine ║\n"
324+
"╚════════════════╝"
325+
)
326+
assert text == expected

0 commit comments

Comments
 (0)