Added padding to panel

This commit is contained in:
Will McGugan 2020-05-22 21:52:28 +01:00
parent a3b09979d5
commit 55988c6e72
2 changed files with 16 additions and 2 deletions

View File

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.2.3] - Unreleased
### Added
- Added `padding` parameter to Panel
## [1.2.2] - 2020-05-22 ## [1.2.2] - 2020-05-22
### Fixed ### Fixed

View File

@ -10,6 +10,7 @@ from .console import (
RenderResult, RenderResult,
Measurement, Measurement,
) )
from .padding import Padding, PaddingDimensions
from .style import Style from .style import Style
from .text import Text from .text import Text
from .segment import Segment from .segment import Segment
@ -29,6 +30,7 @@ class Panel:
width, otherwise it will be sized to fit the contents. Defaults to True. width, otherwise it will be sized to fit the contents. Defaults to True.
style (str, optional): The style of the border. Defaults to "none". style (str, optional): The style of the border. Defaults to "none".
width (Optional[int], optional): Optional width of panel. Defaults to None to auto-detect. width (Optional[int], optional): Optional width of panel. Defaults to None to auto-detect.
padding (Optional[PaddingDimensions]): Optional padding. Defaults to 0
""" """
def __init__( def __init__(
@ -38,16 +40,22 @@ class Panel:
expand: bool = True, expand: bool = True,
style: Union[str, Style] = "none", style: Union[str, Style] = "none",
width: Optional[int] = None, width: Optional[int] = None,
padding: PaddingDimensions = 0,
) -> None: ) -> None:
self.renderable = renderable self.renderable = renderable
self.box = box self.box = box
self.expand = expand self.expand = expand
self.style = style self.style = style
self.width = width self.width = width
self.padding = padding
def __rich_console__( def __rich_console__(
self, console: Console, options: ConsoleOptions self, console: Console, options: ConsoleOptions
) -> RenderResult: ) -> RenderResult:
_padding = Padding.unpack(self.padding)
renderable = (
Padding(self.renderable, _padding) if any(_padding) else self.renderable
)
style = console.get_style(self.style) style = console.get_style(self.style)
width = ( width = (
options.max_width options.max_width
@ -57,11 +65,11 @@ class Panel:
child_width = ( child_width = (
width - 2 width - 2
if self.expand if self.expand
else Measurement.get(console, self.renderable, width - 2).maximum else Measurement.get(console, renderable, width - 2).maximum
) )
width = child_width + 2 width = child_width + 2
child_options = options.update(width=child_width) child_options = options.update(width=child_width)
lines = console.render_lines(self.renderable, child_options) lines = console.render_lines(renderable, child_options)
box = SQUARE if console.legacy_windows else (self.box or ROUNDED) box = SQUARE if console.legacy_windows else (self.box or ROUNDED)
line_start = Segment(box.mid_left, style) line_start = Segment(box.mid_left, style)
line_end = Segment(f"{box.mid_right}\n", style) line_end = Segment(f"{box.mid_right}\n", style)