Skip to content

Commit 62fc31c

Browse files
committed
implement multiline headers
1 parent bf22f8e commit 62fc31c

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

README.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -499,30 +499,30 @@ Version history
499499
---------------
500500

501501
- 0.8.1: FUTURE RELEASE
502-
- 0.8.0: ``latex_raw`` format, column-specific floating point formatting.
503-
Python 3.5 & 3.6 support. Drop support for Python 2.6, 3.2, 3.3.
502+
- 0.8.0: Multiline cells. ``latex_raw`` format. Column-specific floating point formatting.
503+
Python 3.5 & 3.6 support. Drop support for Python 2.6, 3.2, 3.3 (should still work).
504504
- 0.7.7: Identical to 0.7.6, resolving some PyPI issues.
505505
- 0.7.6: Bug fixes. New table formats (``psql``, ``jira``, ``moinmoin``, ``textile``).
506-
Wide character support. Printing from database cursors.
507-
Option to print row indices. Boolean columns. Ragged rows.
508-
Option to disable number parsing.
506+
Wide character support. Printing from database cursors.
507+
Option to print row indices. Boolean columns. Ragged rows.
508+
Option to disable number parsing.
509509
- 0.7.5: Bug fixes. ``--float`` format option for the command line utility.
510510
- 0.7.4: Bug fixes. ``fancy_grid`` and ``html`` formats. Command line utility.
511511
- 0.7.3: Bug fixes. Python 3.4 support. Iterables of dicts. ``latex_booktabs`` format.
512512
- 0.7.2: Python 3.2 support.
513513
- 0.7.1: Bug fixes. ``tsv`` format. Column alignment can be disabled.
514-
- 0.7: ``latex`` tables. Printing lists of named tuples and NumPy
515-
record arrays. Fix printing date and time values. Python <= 2.6.4 is supported.
516-
- 0.6: ``mediawiki`` tables, bug fixes.
514+
- 0.7: ``latex`` tables. Printing lists of named tuples and NumPy
515+
record arrays. Fix printing date and time values. Python <= 2.6.4 is supported.
516+
- 0.6: ``mediawiki`` tables, bug fixes.
517517
- 0.5.1: Fix README.rst formatting. Optimize (performance similar to 0.4.4).
518-
- 0.5: ANSI color sequences. Printing dicts of iterables and Pandas' dataframes.
518+
- 0.5: ANSI color sequences. Printing dicts of iterables and Pandas' dataframes.
519519
- 0.4.4: Python 2.6 support.
520520
- 0.4.3: Bug fix, None as a missing value.
521521
- 0.4.2: Fix manifest file.
522522
- 0.4.1: Update license and documentation.
523-
- 0.4: Unicode support, Python3 support, ``rst`` tables.
524-
- 0.3: Initial PyPI release. Table formats: ``simple``, ``plain``,
525-
``grid``, ``pipe``, and ``orgtbl``.
523+
- 0.4: Unicode support, Python3 support, ``rst`` tables.
524+
- 0.3: Initial PyPI release. Table formats: ``simple``, ``plain``,
525+
``grid``, ``pipe``, and ``orgtbl``.
526526

527527

528528
How to contribute

tabulate.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,15 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
700700
return "{0}".format(val)
701701

702702

703-
def _align_header(header, alignment, width, visible_width):
703+
def _align_header(header, alignment, width, visible_width, is_multiline=False):
704704
"Pad string header to width chars given known visible_width of the header."
705-
width += len(header) - visible_width
705+
if is_multiline:
706+
header_lines = re.split(_multiline_codes, header)
707+
padded_lines = [_align_header(h, alignment, width, visible_width) for h in header_lines]
708+
return "\n".join(padded_lines)
709+
# else: not multiline
710+
ninvisible = max(0, len(header) - visible_width)
711+
width += ninvisible
706712
if alignment == "left":
707713
return _padright(width, header)
708714
elif alignment == "center":
@@ -1221,7 +1227,7 @@ def tabulate(tabular_data, headers=(), tablefmt="simple",
12211227
t_cols = cols or [['']] * len(headers)
12221228
t_aligns = aligns or [stralign] * len(headers)
12231229
minwidths = [max(minw, width_fn(c[0])) for minw, c in zip(minwidths, t_cols)]
1224-
headers = [_align_header(h, a, minw, width_fn(h))
1230+
headers = [_align_header(h, a, minw, width_fn(h), is_multiline)
12251231
for h, a, minw in zip(headers, t_aligns, minwidths)]
12261232
rows = list(zip(*cols))
12271233
else:

test/test_output.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ def test_grid_multiline_headerless():
9393
assert_equal(expected, result)
9494

9595

96+
def test_grid_multiline():
97+
"Output: grid with multiline cells with headers"
98+
table = [[2, "foo\nbar"]]
99+
headers = ("more\nspam \x1b[31meggs\x1b[0m", "more spam\n& eggs")
100+
expected = "\n".join([
101+
"+-------------+-------------+",
102+
"| more | more spam |",
103+
"| spam \x1b[31meggs\x1b[0m | & eggs |",
104+
"+=============+=============+",
105+
"| 2 | foo |",
106+
"| | bar |",
107+
"+-------------+-------------+"])
108+
result = tabulate(table, headers, tablefmt="grid")
109+
assert_equal(expected, result)
110+
111+
96112
def test_fancy_grid():
97113
"Output: fancy_grid with headers"
98114
expected = '\n'.join([

0 commit comments

Comments
 (0)