Skip to content

Commit b1ed1fd

Browse files
committed
Fix support for separating lines
1 parent 95ae5eb commit b1ed1fd

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

tabulate/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,6 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
24142414
append_row = _append_basic_row
24152415

24162416
padded_headers = pad_row(headers, pad)
2417-
padded_rows = [pad_row(row, pad) for row in rows]
24182417

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

2427-
if padded_rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
2426+
if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
24282427
# initial rows with a line below
2429-
for row, ralign in zip(padded_rows[:-1], rowaligns):
2428+
for row, ralign in zip(rows[:-1], rowaligns):
24302429
append_row(
2431-
lines, row, padded_widths, colaligns, fmt.datarow, rowalign=ralign
2430+
lines, pad_row(row, pad), padded_widths, colaligns, fmt.datarow, rowalign=ralign
24322431
)
24332432
_append_line(lines, padded_widths, colaligns, fmt.linebetweenrows)
24342433
# the last row without a line below
24352434
append_row(
24362435
lines,
2437-
padded_rows[-1],
2436+
pad_row(rows[-1], pad),
24382437
padded_widths,
24392438
colaligns,
24402439
fmt.datarow,
@@ -2448,13 +2447,13 @@ def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_mu
24482447
or fmt.lineabove
24492448
or Line("", "", "", "")
24502449
)
2451-
for row in padded_rows:
2450+
for row in rows:
24522451
# test to see if either the 1st column or the 2nd column (account for showindex) has
24532452
# the SEPARATING_LINE flag
24542453
if _is_separating_line(row):
24552454
_append_line(lines, padded_widths, colaligns, separating_line)
24562455
else:
2457-
append_row(lines, row, padded_widths, colaligns, fmt.datarow)
2456+
append_row(lines, pad_row(row, pad), padded_widths, colaligns, fmt.datarow)
24582457

24592458
if fmt.linebelow and "linebelow" not in hidden:
24602459
_append_line(lines, padded_widths, colaligns, fmt.linebelow)

test/test_output.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ def test_simple_with_sep_line():
257257
assert_equal(expected, result)
258258

259259

260+
def test_orgtbl_with_sep_line():
261+
"Output: orgtbl with headers and separating line"
262+
expected = "\n".join(
263+
[
264+
"| strings | numbers |",
265+
"|-----------+-----------|",
266+
"| spam | 41.9999 |",
267+
"|-----------+-----------|",
268+
"| eggs | 451 |",
269+
]
270+
)
271+
result = tabulate(_test_table_with_sep_line, _test_table_headers, tablefmt="orgtbl")
272+
assert_equal(expected, result)
273+
274+
260275
def test_readme_example_with_sep():
261276
table = [["Earth", 6371], ["Mars", 3390], SEPARATING_LINE, ["Moon", 1737]]
262277
expected = "\n".join(
@@ -311,6 +326,28 @@ def test_simple_multiline_2_with_sep_line():
311326
assert_equal(expected, result)
312327

313328

329+
def test_orgtbl_multiline_2_with_sep_line():
330+
"Output: simple with multiline cells"
331+
expected = "\n".join(
332+
[
333+
"| key | value |",
334+
"|-------+-----------|",
335+
"| foo | bar |",
336+
"|-------+-----------|",
337+
"| spam | multiline |",
338+
"| | world |",
339+
]
340+
)
341+
table = [
342+
["key", "value"],
343+
["foo", "bar"],
344+
SEPARATING_LINE,
345+
["spam", "multiline\nworld"],
346+
]
347+
result = tabulate(table, headers="firstrow", stralign="center", tablefmt="orgtbl")
348+
assert_equal(expected, result)
349+
350+
314351
def test_simple_headerless():
315352
"Output: simple without headers"
316353
expected = "\n".join(

0 commit comments

Comments
 (0)