Skip to content

Commit 51387ef

Browse files
committed
collapse_padding
1 parent b0e496c commit 51387ef

File tree

7 files changed

+43
-19
lines changed

7 files changed

+43
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [1.1.7] - 2020-05-19
99

10+
### Added
11+
12+
- Added collapse_padding option to Table.
13+
1014
### Changed
1115

1216
- Some style attributes may be abbreviated (b for bold, i for italic etc). Previously abbreviations worked in console markup but only one at a time, i.e. "[b]Hello[/]" but not "[b i]Hello[/]" -- now they work everywhere.

examples/table.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from rich.console import Console
8+
from rich.measure import Measurement
89
from rich.table import Table
910

1011
table = Table(title="Star Wars Movies")

rich/rule.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Union
22

33
from .console import Console, ConsoleOptions, RenderResult
4+
from .segment import Segment
45
from .style import Style
56
from .text import Text
67

rich/segment.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ def filter_control(
8585

8686
@classmethod
8787
def split_lines(cls, segments: Iterable["Segment"]) -> Iterable[List["Segment"]]:
88+
"""Split a sequence of segments in to a list of lines.
8889
90+
Args:
91+
segments (Iterable[Segment]): Segments potentially containing line feeds.
92+
93+
Yields:
94+
Iterable[List[Segment]]: Iterable of segment lists, one per line.
95+
"""
8996
line: List[Segment] = []
9097
append = line.append
9198

rich/table.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/_card_render.py

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/test_log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def render_log():
2323

2424

2525
def test_log():
26-
expected = "\n\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b[2mtest_log.py:20\x1b[0m\x1b[2m \x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b[2mtest_log.py:21\x1b[0m\x1b[2m \x1b[0m\n \x1b[3m Locals \x1b[0m \n \x1b[34m╭─────────┬────────────────────────────────────────╮\x1b[0m \n \x1b[34m│\x1b[0m\x1b[32m'console'\x1b[0m\x1b[34m│\x1b[0m\x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m\x1b[34m│\x1b[0m \n \x1b[34m╰─────────┴────────────────────────────────────────╯\x1b[0m \n"
26+
expected = "\n\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b[2mtest_log.py:20\x1b[0m\x1b[2m \x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b[2mtest_log.py:21\x1b[0m\x1b[2m \x1b[0m\n \x1b[3m Locals \x1b[0m \n \x1b[34m╭─────────┬────────────────────────────────────────╮\x1b[0m \n \x1b[34m│\x1b[0m\x1b[32m'console'\x1b[0m\x1b[34m│\x1b[0m\x1b[1m<\x1b[0m\x1b[1;38;5;13mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m\x1b[34m│\x1b[0m \n \x1b[34m╰─────────┴────────────────────────────────────────╯\x1b[0m \n"
2727
assert render_log() == expected
2828

2929

0 commit comments

Comments
 (0)