diff --git a/CHANGELOG.md b/CHANGELOG.md index f9327e96c..f9073a727 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [9.12.3] - Unlreleased + +### Changed + +- Optimized Padding + ## [9.12.2] - 2021-02-27 ### Added diff --git a/rich/padding.py b/rich/padding.py index 9fa173a53..ae80184d5 100644 --- a/rich/padding.py +++ b/rich/padding.py @@ -1,4 +1,4 @@ -from typing import cast, Tuple, TYPE_CHECKING, Union +from typing import cast, List, Optional, Tuple, TYPE_CHECKING, Union if TYPE_CHECKING: from .console import ( @@ -94,15 +94,16 @@ def __rich_console__( lines = console.render_lines( self.renderable, child_options, style=style, pad=False ) - lines = Segment.set_shape(lines, child_options.max_width, style=style) - - blank_line = Segment(" " * width + "\n", style) - top = [blank_line] * self.top - bottom = [blank_line] * self.bottom - left = Segment(" " * self.left, style) if self.left else None - right = Segment(" " * self.right, style) if self.right else None - new_line = Segment.line() - yield from top + _Segment = Segment + lines = _Segment.set_shape(lines, child_options.max_width, style=style) + + left = _Segment(" " * self.left, style) if self.left else None + right = _Segment(" " * self.right, style) if self.right else None + new_line = _Segment.line() + blank_line: Optional[List[Segment]] = None + if self.top: + blank_line = [_Segment(" " * width + "\n", style)] + yield from blank_line * self.top for line in lines: if left is not None: yield left @@ -110,7 +111,9 @@ def __rich_console__( if right is not None: yield right yield new_line - yield from bottom + if self.bottom: + blank_line = blank_line or [_Segment(" " * width + "\n", style)] + yield from blank_line * self.bottom def __rich_measure__(self, console: "Console", max_width: int) -> "Measurement": extra_width = self.left + self.right