Skip to content

Commit

Permalink
optimized padding
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Feb 27, 2021
1 parent 43a26c0 commit f6d6f73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions rich/padding.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -94,23 +94,26 @@ 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
yield from line
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
Expand Down

0 comments on commit f6d6f73

Please sign in to comment.