-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tools] add generic method for formatting ascii tables
- Loading branch information
Showing
3 changed files
with
51 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# This file is part of the pyMOR project (http://www.pymor.org). | ||
# Copyright 2013-2016 pyMOR developers and contributors. All rights reserved. | ||
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) | ||
|
||
from itertools import zip_longest | ||
from textwrap import wrap | ||
|
||
import numpy as np | ||
|
||
|
||
def format_table(rows, width='AUTO', title=None): | ||
if width == 'AUTO': | ||
try: | ||
from shutil import get_terminal_size | ||
width = get_terminal_size()[0] - 1 | ||
except ImportError: | ||
width = 1000000 | ||
rows = rows.copy() | ||
column_widths = [max(map(len, c)) for c in zip(*rows)] | ||
if sum(column_widths) + 2*(len(column_widths) - 1) > width: | ||
largest_column = np.argmax(column_widths) | ||
column_widths[largest_column] = 0 | ||
min_width = max(column_widths) | ||
column_widths[largest_column] = max(min_width, width - 2*(len(column_widths) - 1) - sum(column_widths)) | ||
total_width = sum(column_widths) + 2*(len(column_widths) - 1) | ||
|
||
wrapped_rows = [] | ||
for row in rows: | ||
cols = [wrap(c, width=cw) for c, cw in zip(row, column_widths)] | ||
for r in zip_longest(*cols, fillvalue=""): | ||
wrapped_rows.append(r) | ||
rows = wrapped_rows | ||
|
||
rows.insert(1, ['-' * cw for cw in column_widths]) | ||
|
||
if title is not None: | ||
title = '{:^{}}\n{:^{}}\n\n'.format(title, total_width, '='*len(title), total_width) | ||
else: | ||
title = '' | ||
|
||
return title + '\n'.join(' '.join('{:<{}}'.format(c, cw) for c, cw in zip(r, column_widths)) | ||
for r in rows) |