@@ -90,16 +90,22 @@ def __auto_column_widths(self) -> List[int]:
90
90
Returns:
91
91
List[int]: The minimum number of characters needed for each column
92
92
"""
93
+
94
+ def longest_line (text : str ) -> int :
95
+ """Returns the length of the longest line in a multi-line string"""
96
+ return max (len (line ) for line in text .splitlines ()) if len (text ) else 0
97
+
93
98
column_widths = []
99
+ # get the width necessary for each column
94
100
for i in range (self .__columns ):
95
101
# number of characters in column of i of header, each body row, and footer
96
- header_size = len (str (self .__header [i ])) if self .__header else 0
102
+ header_size = longest_line (str (self .__header [i ])) if self .__header else 0
97
103
body_size = (
98
- map (lambda row , i = i : len (str (row [i ])), self .__body )
104
+ map (lambda row , i = i : longest_line (str (row [i ])), self .__body )
99
105
if self .__body
100
106
else [0 ]
101
107
)
102
- footer_size = len (str (self .__footer [i ])) if self .__footer else 0
108
+ footer_size = longest_line (str (self .__footer [i ])) if self .__footer else 0
103
109
# get the max and add 2 for padding each side with a space
104
110
column_widths .append (max (header_size , * body_size , footer_size ) + 2 )
105
111
return column_widths
@@ -144,37 +150,51 @@ def __row_to_ascii(
144
150
Returns:
145
151
str: The line in the ascii table
146
152
"""
147
- # left edge of the row
148
- output = left_edge
149
- # add columns
150
- for i in range (self .__columns ):
151
- # content between separators
152
- output += (
153
- # edge or row separator if filler is a specific character
154
- filler * self .__column_widths [i ]
155
- if isinstance (filler , str )
156
- # otherwise, use the column content
157
- else self .__pad (
158
- filler [i ], self .__column_widths [i ], self .__alignments [i ]
159
- )
160
- )
161
- # column seperator
162
- sep = column_seperator
163
- if i == 0 and self .__first_col_heading :
164
- # use column heading if first column option is specified
165
- sep = heading_col_sep
166
- elif i == self .__columns - 2 and self .__last_col_heading :
167
- # use column heading if last column option is specified
168
- sep = heading_col_sep
169
- elif i == self .__columns - 1 :
170
- # replace last seperator with symbol for edge of the row
171
- sep = right_edge
172
- output += sep
173
- # don't use separation row if it's only space
174
- if output .strip () == "" :
175
- return ""
176
- # otherwise, return the row followed by newline
177
- return output + "\n "
153
+ output = ""
154
+ # find the maximum number of lines a single cell in the column has (minimum of 1)
155
+ num_lines = max (len (str (cell ).splitlines ()) for cell in filler ) or 1
156
+ # repeat for each line of text in the cell
157
+ for line_index in range (num_lines ):
158
+ # left edge of the row
159
+ output += left_edge
160
+ # add columns
161
+ for col_index in range (self .__columns ):
162
+ # content between separators
163
+ col_content = ""
164
+ # if filler is a separator character, repeat it for the full width of the column
165
+ if isinstance (filler , str ):
166
+ col_content = filler * self .__column_widths [col_index ]
167
+ # otherwise, use the text from the corresponding column in the filler list
168
+ else :
169
+ # get the text of the current line in the cell
170
+ # if there are fewer lines in the current cell than others, empty string is used
171
+ col_lines = str (filler [col_index ]).splitlines ()
172
+ if line_index < len (col_lines ):
173
+ col_content = col_lines [line_index ]
174
+ # pad the text to the width of the column using the alignment
175
+ col_content = self .__pad (
176
+ col_content ,
177
+ self .__column_widths [col_index ],
178
+ self .__alignments [col_index ],
179
+ )
180
+ output += col_content
181
+ # column seperator
182
+ sep = column_seperator
183
+ if col_index == 0 and self .__first_col_heading :
184
+ # use column heading if first column option is specified
185
+ sep = heading_col_sep
186
+ elif col_index == self .__columns - 2 and self .__last_col_heading :
187
+ # use column heading if last column option is specified
188
+ sep = heading_col_sep
189
+ elif col_index == self .__columns - 1 :
190
+ # replace last seperator with symbol for edge of the row
191
+ sep = right_edge
192
+ output += sep
193
+ output += "\n "
194
+ # don't use separation row if it's only space
195
+ if isinstance (filler , str ) and output .strip () == "" :
196
+ output = ""
197
+ return output
178
198
179
199
def __top_edge_to_ascii (self ) -> str :
180
200
"""
0 commit comments