@@ -77,6 +77,7 @@ class Table:
77
77
width (int, optional): The width in characters of the table, or ``None`` to automatically fit. Defaults to None.
78
78
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.
79
79
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.
80
81
pad_edge (bool, optional): Enable padding of edge cells. Defaults to True.
81
82
expand (bool, optional): Expand the table to fit the available space if ``True`` otherwise the table width will be auto-calculated. Defaults to False.
82
83
show_header (bool, optional): Show a header row. Defaults to True.
@@ -102,13 +103,13 @@ def __init__(
102
103
width : int = None ,
103
104
box : Optional [box .Box ] = box .HEAVY_HEAD ,
104
105
padding : PaddingDimensions = (0 , 1 ),
106
+ collapse_padding : bool = False ,
105
107
pad_edge : bool = True ,
106
108
expand : bool = False ,
107
109
show_header : bool = True ,
108
110
show_footer : bool = False ,
109
111
show_edge : bool = True ,
110
112
show_lines : bool = False ,
111
- collapse_padding : bool = False ,
112
113
style : StyleType = "none" ,
113
114
row_styles : Iterable [StyleType ] = None ,
114
115
header_style : StyleType = None ,
@@ -171,9 +172,7 @@ def _extra_width(self) -> int:
171
172
if self .box and self .show_edge :
172
173
width += 2
173
174
if self .box :
174
- width += len (self .columns )
175
- if self .pad_edge :
176
- width += 2
175
+ width += len (self .columns ) - 1
177
176
return width
178
177
179
178
@property
@@ -397,35 +396,47 @@ def _get_cells(self, column_index: int, column: Column) -> Iterable[_Cell]:
397
396
"""Get all the cells with padding and optional header."""
398
397
399
398
collapse_padding = self .collapse_padding
399
+ pad_edge = self .pad_edge
400
400
padding = self .padding
401
401
any_padding = any (padding )
402
402
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
405
405
406
- def add_padding (renderable : "RenderableType" ) -> "RenderableType" :
406
+ def add_padding (
407
+ renderable : "RenderableType" , first_row : bool , last_row : bool
408
+ ) -> "RenderableType" :
407
409
if not any_padding :
408
410
return renderable
409
411
top , right , bottom , left = padding
412
+
410
413
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 )
415
418
416
- if not self . pad_edge :
417
- if first :
419
+ if not pad_edge :
420
+ if first_column :
418
421
left = 0
419
- if last :
422
+ if last_column :
420
423
right = 0
424
+ if first_row :
425
+ top = 0
426
+ if last_row :
427
+ bottom = 0
421
428
return Padding (renderable , (top , right , bottom , left ))
422
429
430
+ raw_cells : List [Tuple [StyleType , "RenderableType" ]] = []
431
+ _append = raw_cells .append
423
432
if self .show_header :
424
- yield _Cell ( column .header_style , add_padding ( column .header ))
433
+ _append (( column .header_style , column .header ))
425
434
for cell in column .cells :
426
- yield _Cell ( column .style , add_padding ( cell ))
435
+ _append (( column .style , cell ))
427
436
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 ))
429
440
430
441
def _measure_column (
431
442
self , console : "Console" , column_index : int , column : Column , max_width : int
0 commit comments