Skip to content

Commit 048a22c

Browse files
authored
Merge pull request astanin#279 from pjkundert/feature-separating
Fix support for separating lines
2 parents cecb08e + b39a162 commit 048a22c

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
@@ -2549,7 +2549,6 @@ def _format_table(
25492549
append_row = _append_basic_row
25502550

25512551
padded_headers = pad_row(headers, pad)
2552-
padded_rows = [pad_row(row, pad) for row in rows]
25532552

25542553
if fmt.lineabove and "lineabove" not in hidden:
25552554
_append_line(lines, padded_widths, colaligns, fmt.lineabove)
@@ -2559,18 +2558,18 @@ def _format_table(
25592558
if fmt.linebelowheader and "linebelowheader" not in hidden:
25602559
_append_line(lines, padded_widths, colaligns, fmt.linebelowheader)
25612560

2562-
if padded_rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
2561+
if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
25632562
# initial rows with a line below
2564-
for row, ralign in zip(padded_rows[:-1], rowaligns):
2563+
for row, ralign in zip(rows[:-1], rowaligns):
25652564
if row != SEPARATING_LINE:
25662565
append_row(
2567-
lines, row, padded_widths, colaligns, fmt.datarow, rowalign=ralign
2566+
lines, pad_row(row, pad), padded_widths, colaligns, fmt.datarow, rowalign=ralign
25682567
)
25692568
_append_line(lines, padded_widths, colaligns, fmt.linebetweenrows)
25702569
# the last row without a line below
25712570
append_row(
25722571
lines,
2573-
padded_rows[-1],
2572+
pad_row(rows[-1], pad),
25742573
padded_widths,
25752574
colaligns,
25762575
fmt.datarow,
@@ -2584,13 +2583,13 @@ def _format_table(
25842583
or fmt.lineabove
25852584
or Line("", "", "", "")
25862585
)
2587-
for row in padded_rows:
2586+
for row in rows:
25882587
# test to see if either the 1st column or the 2nd column (account for showindex) has
25892588
# the SEPARATING_LINE flag
25902589
if _is_separating_line(row):
25912590
_append_line(lines, padded_widths, colaligns, separating_line)
25922591
else:
2593-
append_row(lines, row, padded_widths, colaligns, fmt.datarow)
2592+
append_row(lines, pad_row(row, pad), padded_widths, colaligns, fmt.datarow)
25942593

25952594
if fmt.linebelow and "linebelow" not in hidden:
25962595
_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
@@ -261,6 +261,21 @@ def test_simple_with_sep_line():
261261
assert_equal(expected, result)
262262

263263

264+
def test_orgtbl_with_sep_line():
265+
"Output: orgtbl with headers and separating line"
266+
expected = "\n".join(
267+
[
268+
"| strings | numbers |",
269+
"|-----------+-----------|",
270+
"| spam | 41.9999 |",
271+
"|-----------+-----------|",
272+
"| eggs | 451 |",
273+
]
274+
)
275+
result = tabulate(_test_table_with_sep_line, _test_table_headers, tablefmt="orgtbl")
276+
assert_equal(expected, result)
277+
278+
264279
def test_readme_example_with_sep():
265280
table = [["Earth", 6371], ["Mars", 3390], SEPARATING_LINE, ["Moon", 1737]]
266281
expected = "\n".join(
@@ -315,6 +330,28 @@ def test_simple_multiline_2_with_sep_line():
315330
assert_equal(expected, result)
316331

317332

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

0 commit comments

Comments
 (0)