@@ -130,6 +130,11 @@ def __determine_alignments(
130
130
131
131
return list (alignments )
132
132
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
+
133
138
def __auto_column_widths (self ) -> list [int ]:
134
139
"""Get the minimum number of characters needed for the values in each column in the table
135
140
with 1 space of padding on each side.
@@ -138,18 +143,13 @@ def __auto_column_widths(self) -> list[int]:
138
143
The minimum number of characters needed for each column
139
144
"""
140
145
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
-
146
146
def get_column_width (row : Sequence [SupportsStr ], column : int ) -> int :
147
147
"""Get the width of a cell in a column"""
148
148
value = row [column ]
149
149
next_value = row [column + 1 ] if column < self .__columns - 1 else None
150
150
if value is Merge .LEFT or next_value is Merge .LEFT :
151
151
return 0
152
- return widest_line (value )
152
+ return self . __widest_line (value )
153
153
154
154
column_widths = []
155
155
# get the width necessary for each column
@@ -306,7 +306,12 @@ def __wrap_long_lines_in_merged_cells(
306
306
if row [other_col_index ] is not Merge .LEFT :
307
307
break
308
308
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
310
315
wrapped_row .append (cell )
311
316
return wrapped_row
312
317
0 commit comments