Skip to content

Commit

Permalink
Fix support for separating lines
Browse files Browse the repository at this point in the history
  • Loading branch information
cdar committed Jun 25, 2024
1 parent 95ae5eb commit 0655054
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,8 @@ def _expand_iterable(original, num_desired, default):

def _pad_row(cells, padding):
if cells:
if cells == SEPARATING_LINE:
return SEPARATING_LINE
pad = " " * padding
padded_cells = [pad + cell + pad for cell in cells]
return padded_cells
Expand Down Expand Up @@ -2427,9 +2429,10 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
if padded_rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
# initial rows with a line below
for row, ralign in zip(padded_rows[:-1], rowaligns):
append_row(
lines, row, padded_widths, colaligns, fmt.datarow, rowalign=ralign
)
if row != SEPARATING_LINE:
append_row(
lines, row, padded_widths, colaligns, fmt.datarow, rowalign=ralign
)
_append_line(lines, padded_widths, colaligns, fmt.linebetweenrows)
# the last row without a line below
append_row(
Expand Down
30 changes: 30 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,36 @@ def test_simple_headerless_with_sep_line():
assert_equal(expected, result)


def test_simple_headerless_with_sep_line_with_padding_in_tablefmt():
"Output: simple without headers with sep line with padding in tablefmt"
expected = "\n".join(
[
"|------|----------|",
"| spam | 41.9999 |",
"|------|----------|",
"| eggs | 451 |",
]
)
result = tabulate(_test_table_with_sep_line, tablefmt="github")
assert_equal(expected, result)


def test_simple_headerless_with_sep_line_with_linebetweenrows_in_tablefmt():
"Output: simple without headers with sep line with linebetweenrows in tablefmt"
expected = "\n".join(
[
"+------+----------+",
"| spam | 41.9999 |",
"+------+----------+",
"+------+----------+",
"| eggs | 451 |",
"+------+----------+",
]
)
result = tabulate(_test_table_with_sep_line, tablefmt="grid")
assert_equal(expected, result)


def test_simple_multiline_headerless():
"Output: simple with multiline cells without headers"
table = [["foo bar\nbaz\nbau", "hello"], ["", "multiline\nworld"]]
Expand Down

0 comments on commit 0655054

Please sign in to comment.