Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/textual/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,13 +1202,15 @@ def totals(self) -> tuple[int, int]:
def css(self) -> str:
"""A string containing the spacing in CSS format.

For example: "1" or "2 4" or "4 2 8 2".
For example: "1" or "2 4" or "3 5 2" or "4 2 8 2".
"""
top, right, bottom, left = self
if top == right == bottom == left:
return f"{top}"
if (top, right) == (bottom, left):
return f"{top} {right}"
if right == left:
return f"{top} {right} {bottom}"
else:
return f"{top} {right} {bottom} {left}"

Expand All @@ -1217,7 +1219,7 @@ def unpack(cls, pad: SpacingDimensions) -> Spacing:
"""Unpack padding specified in CSS style.

Args:
pad: An integer, or tuple of 1, 2, or 4 integers.
pad: An integer, or tuple of 1, 2, 3 or 4 integers.

Raises:
ValueError: If `pad` is an invalid value.
Expand All @@ -1234,11 +1236,14 @@ def unpack(cls, pad: SpacingDimensions) -> Spacing:
if pad_len == 2:
pad_top, pad_right = cast(Tuple[int, int], pad)
return cls(pad_top, pad_right, pad_top, pad_right)
if pad_len == 3:
pad_top, pad_side, pad_bottom = cast(Tuple[int, int, int], pad)
return cls(pad_top, pad_side, pad_bottom, pad_side)
if pad_len == 4:
top, right, bottom, left = cast(Tuple[int, int, int, int], pad)
return cls(top, right, bottom, left)
raise ValueError(
f"1, 2 or 4 integers required for spacing properties; {pad_len} given"
f"1, 2, 3 or 4 integers required for spacing properties; {pad_len} given"
)

@classmethod
Expand Down
4 changes: 1 addition & 3 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,12 @@ def test_spacing_unpack():
assert Spacing.unpack(1) == Spacing(1, 1, 1, 1)
assert Spacing.unpack((1,)) == Spacing(1, 1, 1, 1)
assert Spacing.unpack((1, 2)) == Spacing(1, 2, 1, 2)
assert Spacing.unpack((1, 2, 3)) == Spacing(1, 2, 3, 2)
assert Spacing.unpack((1, 2, 3, 4)) == Spacing(1, 2, 3, 4)

with pytest.raises(ValueError):
assert Spacing.unpack(()) == Spacing(1, 2, 1, 2)

with pytest.raises(ValueError):
assert Spacing.unpack((1, 2, 3)) == Spacing(1, 2, 1, 2)

with pytest.raises(ValueError):
assert Spacing.unpack((1, 2, 3, 4, 5)) == Spacing(1, 2, 1, 2)

Expand Down
Loading