Strip improvements and line api

This commit is contained in:
Will McGugan
2023-02-01 16:22:14 +01:00
parent faa2ee68d7
commit 9e5814ed0f
3 changed files with 24 additions and 6 deletions

View File

@@ -10,7 +10,7 @@ from rich.style import Style
from ._border import get_box, render_row
from ._filter import LineFilter
from ._opacity import _apply_opacity
from ._segment_tools import line_crop, line_pad, line_trim
from ._segment_tools import line_pad, line_trim
from ._typing import TypeAlias
from .color import Color
from .geometry import Region, Size, Spacing
@@ -22,7 +22,7 @@ if TYPE_CHECKING:
from .css.styles import StylesBase
from .widget import Widget
RenderLineCallback: TypeAlias = Callable[[int], List[Segment]]
RenderLineCallback: TypeAlias = Callable[[int], Strip]
@lru_cache(1024 * 8)
@@ -313,6 +313,7 @@ class StylesCache:
content_y = y - gutter.top
if content_y < content_height:
line = render_content_line(y - gutter.top)
line = line.adjust_cell_length(content_width)
else:
line = [make_blank(content_width, inner)]
if inner:

View File

@@ -6,7 +6,7 @@ from typing import Iterable, Iterator
import rich.repr
from rich.cells import cell_len, set_cell_size
from rich.segment import Segment
from rich.style import Style
from rich.style import Style, StyleType
from ._cache import FIFOCache
from ._filter import LineFilter
@@ -49,7 +49,7 @@ class Strip:
return "".join(segment.text for segment in self._segments)
@classmethod
def blank(cls, cell_length: int, style: Style | None) -> Strip:
def blank(cls, cell_length: int, style: StyleType | None = None) -> Strip:
"""Create a blank strip.
Args:
@@ -59,7 +59,8 @@ class Strip:
Returns:
New strip.
"""
return cls([Segment(" " * cell_length, style)], cell_length)
segment_style = Style.parse(style) if isinstance(style, str) else style
return cls([Segment(" " * cell_length, segment_style)], cell_length)
@classmethod
def from_lines(
@@ -135,6 +136,23 @@ class Strip:
self._segments == strip._segments and self.cell_length == strip.cell_length
)
def extend_cell_length(self, cell_length: int, style: Style | None = None) -> Strip:
"""Extend the cell length if it is less than the given value.
Args:
cell_length: Required minimum cell length.
style: Style for padding if the cell length is extended.
Returns:
A new Strip.
"""
if self.cell_length < cell_length:
missing_space = cell_length - self.cell_length
segments = self._segments + [Segment(" " * missing_space, style)]
return Strip(segments, cell_length)
else:
return self
def adjust_cell_length(self, cell_length: int, style: Style | None = None) -> Strip:
"""Adjust the cell length, possibly truncating or extending.

View File

@@ -14,7 +14,6 @@ from ..reactive import var
from ..geometry import Size, Region
from ..scroll_view import ScrollView
from .._cache import LRUCache
from .._segment_tools import line_crop
from ..strip import Strip