@@ -77,6 +77,7 @@ class Table:
7777 width (int, optional): The width in characters of the table, or ``None`` to automatically fit. Defaults to None.
7878 box (Optional[box.Box], optional): One of the constants in box.py used to draw the edges (See :ref:`appendix_box`). Defaults to box.HEAVY_HEAD.
7979 padding (PaddingDimensions, optional): Padding for cells (top, right, bottom, left). Defaults to (0, 1).
80+ collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to False.
8081 pad_edge (bool, optional): Enable padding of edge cells. Defaults to True.
8182 expand (bool, optional): Expand the table to fit the available space if ``True`` otherwise the table width will be auto-calculated. Defaults to False.
8283 show_header (bool, optional): Show a header row. Defaults to True.
@@ -102,13 +103,13 @@ def __init__(
102103 width : int = None ,
103104 box : Optional [box .Box ] = box .HEAVY_HEAD ,
104105 padding : PaddingDimensions = (0 , 1 ),
106+ collapse_padding : bool = False ,
105107 pad_edge : bool = True ,
106108 expand : bool = False ,
107109 show_header : bool = True ,
108110 show_footer : bool = False ,
109111 show_edge : bool = True ,
110112 show_lines : bool = False ,
111- collapse_padding : bool = False ,
112113 style : StyleType = "none" ,
113114 row_styles : Iterable [StyleType ] = None ,
114115 header_style : StyleType = None ,
@@ -171,9 +172,7 @@ def _extra_width(self) -> int:
171172 if self .box and self .show_edge :
172173 width += 2
173174 if self .box :
174- width += len (self .columns )
175- if self .pad_edge :
176- width += 2
175+ width += len (self .columns ) - 1
177176 return width
178177
179178 @property
@@ -397,35 +396,47 @@ def _get_cells(self, column_index: int, column: Column) -> Iterable[_Cell]:
397396 """Get all the cells with padding and optional header."""
398397
399398 collapse_padding = self .collapse_padding
399+ pad_edge = self .pad_edge
400400 padding = self .padding
401401 any_padding = any (padding )
402402
403- first = column_index == 0
404- last = column_index == len (self .columns ) - 1
403+ first_column = column_index == 0
404+ last_column = column_index == len (self .columns ) - 1
405405
406- def add_padding (renderable : "RenderableType" ) -> "RenderableType" :
406+ def add_padding (
407+ renderable : "RenderableType" , first_row : bool , last_row : bool
408+ ) -> "RenderableType" :
407409 if not any_padding :
408410 return renderable
409411 top , right , bottom , left = padding
412+
410413 if collapse_padding :
411- if not first :
412- left = max (right , left )
413- if column_index > 0 :
414- top = max (top , bottom )
414+ if not first_column :
415+ left = max (0 , left - right )
416+ if not last_row :
417+ bottom = max (0 , top - bottom )
415418
416- if not self . pad_edge :
417- if first :
419+ if not pad_edge :
420+ if first_column :
418421 left = 0
419- if last :
422+ if last_column :
420423 right = 0
424+ if first_row :
425+ top = 0
426+ if last_row :
427+ bottom = 0
421428 return Padding (renderable , (top , right , bottom , left ))
422429
430+ raw_cells : List [Tuple [StyleType , "RenderableType" ]] = []
431+ _append = raw_cells .append
423432 if self .show_header :
424- yield _Cell ( column .header_style , add_padding ( column .header ))
433+ _append (( column .header_style , column .header ))
425434 for cell in column .cells :
426- yield _Cell ( column .style , add_padding ( cell ))
435+ _append (( column .style , cell ))
427436 if self .show_footer :
428- yield _Cell (column .footer_style , add_padding (column .footer ))
437+ _append ((column .footer_style , column .footer ))
438+ for first , last , (style , renderable ) in loop_first_last (raw_cells ):
439+ yield _Cell (style , add_padding (renderable , first , last ))
429440
430441 def _measure_column (
431442 self , console : "Console" , column_index : int , column : Column , max_width : int
0 commit comments