@@ -131,26 +131,37 @@ def _is_separating_line(row):
131131 )
132132
133133
134- def _pipe_segment_with_colons (align , colwidth ):
134+ def _pipe_segment_with_colons (align , colwidth , padding = 0 ):
135135 """Return a segment of a horizontal line with optional colons which
136- indicate column's alignment (as in `pipe` output format)."""
136+ indicate column's alignment (as in `pipe` output format).
137+
138+ When ``padding`` is non-zero, the segment respects the format's padding by
139+ placing spaces at the padding positions instead of filling the entire width
140+ with dashes. This makes separator rows consistent with data rows, which
141+ already honour padding via ``_pad_row``.
142+ """
137143 w = colwidth
144+ pad = " " * padding
145+ # Width available for dashes and alignment colons.
146+ dw = w - 2 * padding
138147 if align in ["right" , "decimal" ]:
139- return ("-" * (w - 1 )) + ":"
148+ return pad + ("-" * (dw - 1 )) + ":" + pad
140149 elif align == "center" :
141- return ":" + ("-" * (w - 2 )) + ":"
150+ return pad + ":" + ("-" * (dw - 2 )) + ":" + pad
142151 elif align == "left" :
143- return ":" + ("-" * (w - 1 ))
152+ return pad + ":" + ("-" * (dw - 1 )) + pad
144153 else :
145- return "-" * w
154+ return pad + ( "-" * dw ) + pad
146155
147156
148- def _pipe_line_with_colons (colwidths , colaligns ):
157+ def _pipe_line_with_colons (colwidths , colaligns , padding = 0 ):
149158 """Return a horizontal line with optional colons to indicate column's
150159 alignment (as in `pipe` output format)."""
151160 if not colaligns : # e.g. printing an empty data frame (github issue #15)
152161 colaligns = ["" ] * len (colwidths )
153- segments = "|" .join (_pipe_segment_with_colons (a , w ) for a , w in zip (colaligns , colwidths ))
162+ segments = "|" .join (
163+ _pipe_segment_with_colons (a , w , padding ) for a , w in zip (colaligns , colwidths )
164+ )
154165 return f"|{ segments } |"
155166
156167
@@ -168,7 +179,7 @@ def _grid_segment_with_colons(colwidth, align):
168179 return "=" * width
169180
170181
171- def _grid_line_with_colons (colwidths , colaligns ):
182+ def _grid_line_with_colons (colwidths , colaligns , ** kwargs ):
172183 """Return a horizontal line with optional colons to indicate column's alignment
173184 in a grid table."""
174185 if not colaligns :
@@ -200,7 +211,7 @@ def _textile_row_with_attrs(cell_values, colwidths, colaligns):
200211 return f"|{ values } |"
201212
202213
203- def _html_begin_table_without_header (colwidths_ignore , colaligns_ignore ):
214+ def _html_begin_table_without_header (colwidths_ignore , colaligns_ignore , ** kwargs ):
204215 # this table header will be suppressed if there is a header row
205216 return "<table>\n <tbody>"
206217
@@ -242,7 +253,7 @@ def _moin_row_with_attrs(celltag, cell_values, colwidths, colaligns, header=""):
242253 return "" .join (values_with_attrs ) + "||"
243254
244255
245- def _latex_line_begin_tabular (colwidths , colaligns , booktabs = False , longtable = False ):
256+ def _latex_line_begin_tabular (colwidths , colaligns , booktabs = False , longtable = False , ** kwargs ):
246257 alignment = {"left" : "l" , "right" : "r" , "center" : "c" , "decimal" : "r" }
247258 tabular_columns_fmt = "" .join ([alignment .get (a , "l" ) for a in colaligns ])
248259 return "\n " .join (
@@ -255,7 +266,7 @@ def _latex_line_begin_tabular(colwidths, colaligns, booktabs=False, longtable=Fa
255266 )
256267
257268
258- def _asciidoc_row (is_header , * args ):
269+ def _asciidoc_row (is_header , * args , ** kwargs ):
259270 """handle header and data rows for asciidoc format"""
260271
261272 def make_header_line (is_header , colwidths , colaligns ):
@@ -2063,7 +2074,7 @@ def tabulate(
20632074 >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
20642075 ... ["strings", "numbers"], "pipe"))
20652076 | strings | numbers |
2066- |:----------| ----------: |
2077+ | :-------- | --------: |
20672078 | spam | 41.9999 |
20682079 | eggs | 451 |
20692080
@@ -2077,7 +2088,7 @@ def tabulate(
20772088 eggs | 451
20782089
20792090 >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="pipe"))
2080- |:-----| ---------: |
2091+ | :--- | -------: |
20812092 | spam | 41.9999 |
20822093 | eggs | 451 |
20832094
@@ -2578,21 +2589,21 @@ def _append_multiline_row(
25782589 return lines
25792590
25802591
2581- def _build_line (colwidths , colaligns , linefmt ):
2592+ def _build_line (colwidths , colaligns , linefmt , padding = 0 ):
25822593 "Return a string which represents a horizontal line."
25832594 if not linefmt :
25842595 return None
25852596 if callable (linefmt ):
2586- return linefmt (colwidths , colaligns )
2597+ return linefmt (colwidths , colaligns , padding = padding )
25872598 else :
25882599 begin , fill , sep , end = linefmt
25892600 cells = [fill * w for w in colwidths ]
25902601 rowfmt = DataRow (begin , sep , end )
25912602 return _build_simple_row (cells , rowfmt )
25922603
25932604
2594- def _append_line (lines , colwidths , colaligns , linefmt ):
2595- lines .append (_build_line (colwidths , colaligns , linefmt ))
2605+ def _append_line (lines , colwidths , colaligns , linefmt , padding = 0 ):
2606+ lines .append (_build_line (colwidths , colaligns , linefmt , padding = padding ))
25962607 return lines
25972608
25982609
@@ -2629,12 +2640,12 @@ def _format_table(
26292640 padded_headers = pad_row (headers , pad )
26302641
26312642 if fmt .lineabove and "lineabove" not in hidden :
2632- _append_line (lines , padded_widths , colaligns , fmt .lineabove )
2643+ _append_line (lines , padded_widths , colaligns , fmt .lineabove , padding = pad )
26332644
26342645 if padded_headers :
26352646 append_row (lines , padded_headers , padded_widths , headersaligns , headerrow )
26362647 if fmt .linebelowheader and "linebelowheader" not in hidden :
2637- _append_line (lines , padded_widths , colaligns , fmt .linebelowheader )
2648+ _append_line (lines , padded_widths , colaligns , fmt .linebelowheader , padding = pad )
26382649
26392650 if rows and fmt .linebetweenrows and "linebetweenrows" not in hidden :
26402651 # initial rows with a line below
@@ -2648,7 +2659,7 @@ def _format_table(
26482659 fmt .datarow ,
26492660 rowalign = ralign ,
26502661 )
2651- _append_line (lines , padded_widths , colaligns , fmt .linebetweenrows )
2662+ _append_line (lines , padded_widths , colaligns , fmt .linebetweenrows , padding = pad )
26522663 # the last row without a line below
26532664 append_row (
26542665 lines ,
@@ -2670,12 +2681,12 @@ def _format_table(
26702681 # test to see if either the 1st column or the 2nd column (account for showindex) has
26712682 # the SEPARATING_LINE flag
26722683 if _is_separating_line (row ):
2673- _append_line (lines , padded_widths , colaligns , separating_line )
2684+ _append_line (lines , padded_widths , colaligns , separating_line , padding = pad )
26742685 else :
26752686 append_row (lines , pad_row (row , pad ), padded_widths , colaligns , fmt .datarow )
26762687
26772688 if fmt .linebelow and "linebelow" not in hidden :
2678- _append_line (lines , padded_widths , colaligns , fmt .linebelow )
2689+ _append_line (lines , padded_widths , colaligns , fmt .linebelow , padding = pad )
26792690
26802691 if headers or rows :
26812692 output = "\n " .join (lines )
0 commit comments