Skip to content

Commit

Permalink
Merge pull request #331 from cdar/bugfix/322-328/separating-lines
Browse files Browse the repository at this point in the history
Fix support for separating lines
  • Loading branch information
astanin authored Sep 26, 2024
2 parents 520ed0e + 0655054 commit ee0472e
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 @@ -2419,6 +2419,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 @@ -2544,9 +2546,10 @@ def _format_table(
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 @@ -339,6 +339,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 ee0472e

Please sign in to comment.