Skip to content

Commit 38a9b63

Browse files
committed
Handle embedded newlines in print_table() wireservice#749
1 parent 6152fea commit 38a9b63

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

agate/table/print_table.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
6868
widths = [len(n) for n in column_names]
6969
number_formatters = []
7070
formatted_data = []
71+
multi_line_rows = False
7172

7273
# Determine correct number of decimal places for each Number column
7374
for i, c in enumerate(self._columns):
@@ -89,7 +90,7 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
8990
if i >= max_rows:
9091
break
9192

92-
formatted_row = []
93+
formatted_row = [[]]
9394

9495
for j, v in enumerate(row):
9596
if j >= max_columns:
@@ -104,14 +105,33 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
104105
)
105106
else:
106107
v = six.text_type(v)
108+
vs = v.splitlines()
109+
if len(vs) > 1:
110+
multi_line_rows = True
111+
112+
v = vs[0]
113+
for k, xv in enumerate(vs):
114+
if k == 0:
115+
v = xv
116+
continue
117+
118+
if max_column_width is not None and len(xv) > max_column_width:
119+
xv = '%s...' % xv[:max_column_width - 3]
120+
121+
if len(xv) > widths[j]:
122+
widths[j] = len(xv)
123+
124+
if k == len(formatted_row):
125+
formatted_row.append([''] * max_columns)
126+
formatted_row[k][j] = xv
107127

108128
if max_column_width is not None and len(v) > max_column_width:
109129
v = '%s...' % v[:max_column_width - 3]
110130

111131
if len(v) > widths[j]:
112132
widths[j] = len(v)
113133

114-
formatted_row.append(v)
134+
formatted_row[0].append(v)
115135

116136
if j >= max_columns:
117137
break
@@ -140,7 +160,6 @@ def write_row(formatted_row):
140160

141161
write('%s%s%s' % (v_line, text, v_line))
142162

143-
# Dashes span each width with '+' character at intersection of
144163
# horizontal and vertical dividers.
145164
divider = '%(v_line)s %(columns)s %(v_line)s' % {
146165
'h_line': h_line,
@@ -154,7 +173,10 @@ def write_row(formatted_row):
154173

155174
# Rows
156175
for formatted_row in formatted_data:
157-
write_row(formatted_row)
176+
for row_line in formatted_row:
177+
write_row(row_line)
178+
if multi_line_rows:
179+
write(divider)
158180

159181
# Row indicating data was truncated
160182
if rows_truncated:

0 commit comments

Comments
 (0)