Skip to content

Commit

Permalink
Fix support for separating lines
Browse files Browse the repository at this point in the history
  • Loading branch information
pjkundert committed Jul 20, 2023
1 parent 95ae5eb commit b1ed1fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
13 changes: 6 additions & 7 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,6 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
append_row = _append_basic_row

padded_headers = pad_row(headers, pad)
padded_rows = [pad_row(row, pad) for row in rows]

if fmt.lineabove and "lineabove" not in hidden:
_append_line(lines, padded_widths, colaligns, fmt.lineabove)
Expand All @@ -2424,17 +2423,17 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
if fmt.linebelowheader and "linebelowheader" not in hidden:
_append_line(lines, padded_widths, colaligns, fmt.linebelowheader)

if padded_rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
# initial rows with a line below
for row, ralign in zip(padded_rows[:-1], rowaligns):
for row, ralign in zip(rows[:-1], rowaligns):
append_row(
lines, row, padded_widths, colaligns, fmt.datarow, rowalign=ralign
lines, pad_row(row, pad), padded_widths, colaligns, fmt.datarow, rowalign=ralign
)
_append_line(lines, padded_widths, colaligns, fmt.linebetweenrows)
# the last row without a line below
append_row(
lines,
padded_rows[-1],
pad_row(rows[-1], pad),
padded_widths,
colaligns,
fmt.datarow,
Expand All @@ -2448,13 +2447,13 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
or fmt.lineabove
or Line("", "", "", "")
)
for row in padded_rows:
for row in rows:
# test to see if either the 1st column or the 2nd column (account for showindex) has
# the SEPARATING_LINE flag
if _is_separating_line(row):
_append_line(lines, padded_widths, colaligns, separating_line)
else:
append_row(lines, row, padded_widths, colaligns, fmt.datarow)
append_row(lines, pad_row(row, pad), padded_widths, colaligns, fmt.datarow)

if fmt.linebelow and "linebelow" not in hidden:
_append_line(lines, padded_widths, colaligns, fmt.linebelow)
Expand Down
37 changes: 37 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ def test_simple_with_sep_line():
assert_equal(expected, result)


def test_orgtbl_with_sep_line():
"Output: orgtbl with headers and separating line"
expected = "\n".join(
[
"| strings | numbers |",
"|-----------+-----------|",
"| spam | 41.9999 |",
"|-----------+-----------|",
"| eggs | 451 |",
]
)
result = tabulate(_test_table_with_sep_line, _test_table_headers, tablefmt="orgtbl")
assert_equal(expected, result)


def test_readme_example_with_sep():
table = [["Earth", 6371], ["Mars", 3390], SEPARATING_LINE, ["Moon", 1737]]
expected = "\n".join(
Expand Down Expand Up @@ -311,6 +326,28 @@ def test_simple_multiline_2_with_sep_line():
assert_equal(expected, result)


def test_orgtbl_multiline_2_with_sep_line():
"Output: simple with multiline cells"
expected = "\n".join(
[
"| key | value |",
"|-------+-----------|",
"| foo | bar |",
"|-------+-----------|",
"| spam | multiline |",
"| | world |",
]
)
table = [
["key", "value"],
["foo", "bar"],
SEPARATING_LINE,
["spam", "multiline\nworld"],
]
result = tabulate(table, headers="firstrow", stralign="center", tablefmt="orgtbl")
assert_equal(expected, result)


def test_simple_headerless():
"Output: simple without headers"
expected = "\n".join(
Expand Down

0 comments on commit b1ed1fd

Please sign in to comment.