Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle embedded newlines in print_table() #749 #750

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions agate/table/print_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
widths = [len(n) for n in column_names]
number_formatters = []
formatted_data = []
multi_line_rows = False

# Determine correct number of decimal places for each Number column
for i, c in enumerate(self._columns):
Expand All @@ -87,7 +88,7 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
if i >= max_rows:
break

formatted_row = []
formatted_row = [[]]

for j, v in enumerate(row):
if j >= max_columns:
Expand All @@ -102,14 +103,33 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
)
else:
v = str(v)
vs = v.splitlines()
if len(vs) > 1:
multi_line_rows = True

v = vs[0]
for k, xv in enumerate(vs):
if k == 0:
v = xv
continue

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

if len(xv) > widths[j]:
widths[j] = len(xv)

if k == len(formatted_row):
formatted_row.append([''] * max_columns)
formatted_row[k][j] = xv

if max_column_width is not None and len(v) > max_column_width:
v = '%s%s' % (v[:max_column_width - len_truncation], truncation)

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

formatted_row.append(v)
formatted_row[0].append(v)

if j >= max_columns:
break
Expand Down Expand Up @@ -138,6 +158,7 @@ def write_row(formatted_row):

write(f'{v_line}{text}{v_line}')

# horizontal and vertical dividers.
divider = '{v_line} {columns} {v_line}'.format(
v_line=v_line,
columns=' | '.join(h_line * w for w in widths)
Expand All @@ -149,7 +170,10 @@ def write_row(formatted_row):

# Rows
for formatted_row in formatted_data:
write_row(formatted_row)
for row_line in formatted_row:
write_row(row_line)
if multi_line_rows:
write(divider)

# Row indicating data was truncated
if rows_truncated:
Expand Down
Loading