Skip to content

Commit 8488909

Browse files
committed
fix pipe output (render lines with a specialized function)
1 parent a05a08c commit 8488909

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

tabulate.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@
7979
"padding", "hiding"])
8080

8181

82+
def _pipe_segment_with_colons(align, colwidth):
83+
"""Return a segment of a horizontal line with optional colons which
84+
indicate column's alignment (as in `pipe` output format)."""
85+
w = colwidth
86+
if align in ["right", "decimal"]:
87+
return ('-' * (w - 1)) + ":"
88+
elif align == "center":
89+
return ":" + ('-' * (w - 2)) + ":"
90+
elif align == "left":
91+
return ":" + ('-' * (w - 1))
92+
else:
93+
return '-' * w
94+
95+
96+
def _pipe_line_with_colons(colwidths, colaligns):
97+
"""Return a horizontal line with optional colons to indicate column's
98+
alignment (as in `pipe` output format)."""
99+
segments = [_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)]
100+
return u"|" + u"|".join(segments) + u"|"
101+
102+
82103
_table_formats = {"simple":
83104
TableFormat(lineabove=Line("", "-", " ", ""),
84105
linebelowheader=Line("", "-", " ", ""),
@@ -102,14 +123,15 @@
102123
headerrow=DataRow("|", "|", "|"),
103124
datarow=DataRow("|", "|", "|"),
104125
padding=1, hiding=None),
105-
"pipe": # TODO: colons in linebelowheader
106-
TableFormat(lineabove=None,
107-
linebelowheader=Line("|", "-", "|", "|"),
126+
"pipe":
127+
TableFormat(lineabove=_pipe_line_with_colons,
128+
linebelowheader=_pipe_line_with_colons,
108129
linebetweenrows=None,
109130
linebelow=None,
110131
headerrow=DataRow("|", "|", "|"),
111132
datarow=DataRow("|", "|", "|"),
112-
padding=1, hiding=None),
133+
padding=1,
134+
hiding=HidingRules(with_header_hide=["lineabove"])),
113135
"orgtbl":
114136
TableFormat(lineabove=None,
115137
linebelowheader=Line("|", "-", "+", "|"),
@@ -732,12 +754,13 @@ def _build_row(cells, padding, rowfmt):
732754
return (begin + sep.join(padded_cells) + end).rstrip()
733755

734756

735-
def _build_line(colwidths, padding, linefmt):
757+
def _build_line(colwidths, colaligns, padding, linefmt):
736758
"Return a string which represents a horizontal line."
737759
if not linefmt:
738760
return None
739761
if isfunction(linefmt):
740-
raise NotImplementedError("line format as a function")
762+
padded_widths = [w + 2*padding for w in colwidths]
763+
return linefmt(padded_widths, colaligns)
741764
else:
742765
begin, fill, sep, end = linefmt
743766
cells = [fill*(w + 2*padding) for w in colwidths]
@@ -754,21 +777,6 @@ def _mediawiki_cell_attrs(row, colaligns):
754777
return row2
755778

756779

757-
def _line_segment_with_colons(linefmt, align, colwidth):
758-
"""Return a segment of a horizontal line with optional colons which
759-
indicate column's alignment (as in `pipe` output format)."""
760-
fill = linefmt.hline
761-
w = colwidth
762-
if align in ["right", "decimal"]:
763-
return (fill[0] * (w - 1)) + ":"
764-
elif align == "center":
765-
return ":" + (fill[0] * (w - 2)) + ":"
766-
elif align == "left":
767-
return ":" + (fill[0] * (w - 1))
768-
else:
769-
return fill[0] * w
770-
771-
772780
def _format_table(fmt, headers, rows, colwidths, colaligns):
773781
"""Produce a plain-text representation of the table."""
774782
lines = []
@@ -777,25 +785,25 @@ def _format_table(fmt, headers, rows, colwidths, colaligns):
777785
headerrow = fmt.headerrow
778786

779787
if fmt.lineabove and "lineabove" not in hidden:
780-
lines.append(_build_line(colwidths, pad, fmt.lineabove))
788+
lines.append(_build_line(colwidths, colaligns, pad, fmt.lineabove))
781789

782790
if headers:
783791
lines.append(_build_row(headers, pad, headerrow))
784792
if fmt.linebelowheader and "linebelowheader" not in hidden:
785-
lines.append(_build_line(colwidths, pad, fmt.linebelowheader))
793+
lines.append(_build_line(colwidths, colaligns, pad, fmt.linebelowheader))
786794

787795
if rows and fmt.linebetweenrows and "linebetweenrows" not in hidden:
788796
# initial rows with a line below
789797
for row in rows[:-1]:
790798
lines.append(_build_row(row, pad, fmt.datarow))
791-
lines.append(_build_line(colwidths, pad, fmt.linebetweenrows))
799+
lines.append(_build_line(colwidths, colaligns, pad, fmt.linebetweenrows))
792800
# the last row without a line below
793801
lines.append(_build_row(rows[-1], pad, fmt.datarow))
794802
else:
795803
for row in rows:
796804
lines.append(_build_row(row, pad, fmt.datarow))
797805

798806
if fmt.linebelow and "linebelow" not in hidden:
799-
lines.append(_build_line(colwidths, pad, fmt.linebelow))
807+
lines.append(_build_line(colwidths, colaligns, pad, fmt.linebelow))
800808

801809
return "\n".join(lines)

0 commit comments

Comments
 (0)