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

Fix support for separating lines #279

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 6 additions & 7 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2549,7 +2549,6 @@ def _format_table(
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 @@ -2559,18 +2558,18 @@ def _format_table(
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):
if row != SEPARATING_LINE:
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 @@ -2584,13 +2583,13 @@ def _format_table(
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 @@ -261,6 +261,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 @@ -315,6 +330,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
Loading