Skip to content

Commit

Permalink
Add "elide_empty_columns" to TableFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
elric1 committed Mar 4, 2024
1 parent 97422c4 commit 70859a6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def _is_file(f):
"datarow",
"padding",
"align_columns",
"elide_empty_columns",
"with_header_hide",
], defaults = (
None,
Expand All @@ -108,6 +109,7 @@ def _is_file(f):
DataRow("", " ", ""),
0,
True,
False,
None,
),
)
Expand Down Expand Up @@ -2319,6 +2321,14 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
pad = fmt.padding
headerrow = fmt.headerrow

if fmt.elide_empty_columns:
def elim_zero_len(row):
return list(zip(*filter(lambda x: x[0], zip(colwidths, row))))[1]

if len(headers):
headers = elim_zero_len(headers)
rows = [elim_zero_len(row) for row in rows]

padded_widths = [(w + 2 * pad) for w in colwidths]
if is_multiline:
pad_row = lambda row, _: row # noqa do it later, in _append_multiline_row
Expand Down

0 comments on commit 70859a6

Please sign in to comment.