@@ -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
0 commit comments