select fixes

This commit is contained in:
Will McGugan
2024-11-09 20:30:08 +00:00
parent 945e4290d3
commit f8b7daaea6
6 changed files with 44 additions and 29 deletions

View File

@@ -212,6 +212,7 @@ class Content(Visual):
) -> list[Strip]:
if not width:
return []
lines = self.wrap(
width,
justify=self._justify,

View File

@@ -576,7 +576,9 @@ class Strip:
strip = self.adjust_cell_length(cell_length, style)
if not (left or right):
return strip
segments = self._segments.copy()
else:
strip = self
segments = strip._segments.copy()
if left:
segments.insert(0, Segment(" " * left, style))
if right:

View File

@@ -65,7 +65,8 @@ def visualize(widget: Widget, obj: object) -> Visual:
# Doesn't expose the textualize protocol
if is_renderable(obj):
if isinstance(obj, str):
obj = Text.from_markup(obj)
obj = widget.render_str(obj)
# If its is a Rich renderable, wrap it with a RichVisual
return RichVisual(widget, rich_cast(obj))
else:
@@ -228,18 +229,18 @@ class Visual(ABC):
@classmethod
def to_strips(
cls,
widget: Widget,
visual: Visual,
width: int,
height: int,
widget: Widget,
component_classes: list[str] | None = None,
height: int | None,
style: Style,
pad: bool = False,
) -> list[Strip]:
visual_style = (
widget.get_visual_style(*component_classes)
if component_classes
else widget.get_visual_style()
)
strips = visual.render_strips(widget, width, height, visual_style)
strips = visual.render_strips(widget, width, height, style)
if pad:
strips = [
strip.extend_cell_length(width, style.rich_style) for strip in strips
]
return strips
@@ -256,7 +257,9 @@ class RichVisual(Visual):
def _measure(self, console: Console, options: ConsoleOptions) -> Measurement:
return Measurement.get(
console, options, self._widget.post_render(self._renderable)
console,
options,
self._widget.post_render(self._renderable, RichStyle.null()),
)
if self._measurement is None:
self._measurement = Measurement.get(console, options, self._renderable)
@@ -303,12 +306,11 @@ class RichVisual(Visual):
width=width,
height=height,
)
renderable = widget.post_render(self._renderable)
segments = console.render(renderable, options.update_width(width))
rich_style = style.rich_style
renderable = widget.post_render(self._renderable, rich_style)
segments = console.render(renderable, options.update_width(width))
strips = [
Strip(line).apply_style(rich_style)
Strip(line)
for line in islice(
Segment.split_and_crop_lines(
segments, width, include_new_lines=False, pad=False
@@ -354,6 +356,7 @@ class Padding(Visual):
None if height is None else height - padding.height,
style,
)
rich_style = style.rich_style
if padding:
top_padding = [Strip.blank(width, rich_style)] * top if top else []

View File

@@ -3551,7 +3551,9 @@ class Widget(DOMNode):
text_justify = _JUSTIFY_MAP.get(text_align, text_align)
return text_justify
def post_render(self, renderable: RenderableType) -> ConsoleRenderable:
def post_render(
self, renderable: RenderableType, base_style: Style
) -> ConsoleRenderable:
"""Applies style attributes to the default renderable.
This method is called by Textual itself.
@@ -3576,7 +3578,7 @@ class Widget(DOMNode):
renderable = _Styled(
cast(ConsoleRenderable, renderable),
self.rich_style,
base_style,
self.link_style if self.auto_links else None,
)
@@ -3716,7 +3718,7 @@ class Widget(DOMNode):
# visual = visualize(self, renderable)
strips = Visual.to_strips(visual, width, height, self)
strips = Visual.to_strips(self, visual, width, height, self.visual_style)
if not (align_horizontal == "left" and align_horizontal == "top"):
strips = list(
Strip.align(

View File

@@ -14,6 +14,8 @@ from textual import events
if TYPE_CHECKING:
from textual.app import RenderResult
from rich.style import Style
from textual.binding import Binding
from textual.css._error_tools import friendly_list
from textual.geometry import Size
@@ -257,7 +259,9 @@ class Button(Widget, can_focus=True):
self._get_justify_method() or "center",
)
def post_render(self, renderable: RenderableType) -> ConsoleRenderable:
def post_render(
self, renderable: RenderableType, base_style: Style
) -> ConsoleRenderable:
return cast(ConsoleRenderable, renderable)
async def _on_click(self, event: events.Click) -> None:

View File

@@ -482,14 +482,19 @@ class OptionList(ScrollView, can_focus=True):
if padding:
visual = Padding(visual, padding)
strips = Visual.to_strips(
visual,
width,
2,
self,
component_classes=[component_class] if component_class else None,
visual_style = (
self.get_visual_style("option-list--option", component_class)
if component_class
else self.get_visual_style("option-list--option")
)
# strips = visual.render_strips(self, width, None, style=visual_style)
strips = Visual.to_strips(self, visual, width, None, visual_style, pad=True)
# strips = [
# strip.extend_cell_length(width, visual_style.rich_style) for strip in strips
# ]
# padding = self.get_component_styles("option-list--option").padding
# console = self.app.console
# options = console.options.update_width(width)
@@ -501,9 +506,7 @@ class OptionList(ScrollView, can_focus=True):
# lines = self.app.console.render_lines(renderable, options, style=style)
style_meta = Style.from_meta({"option": option_index})
strips = [
strip.adjust_cell_length(width).apply_style(style_meta) for strip in strips
]
strips = [strip.apply_style(style_meta) for strip in strips]
# strips = [Strip(line, width).apply_style(style_meta) for line in lines]