1919from rich .cells import set_cell_size
2020from rich .console import OverflowMethod
2121from rich .segment import Segment , Segments
22+ from rich .style import Style as RichStyle
2223from rich .terminal_theme import TerminalTheme
2324from rich .text import Text
2425from typing_extensions import Final , TypeAlias
2728from textual ._context import active_app
2829from textual ._loop import loop_last
2930from textual .color import Color
31+ from textual .css .styles import RulesMap
3032from textual .css .types import TextAlign
3133from textual .selection import Selection
3234from textual .strip import Strip
3335from textual .style import Style
3436from textual .visual import Visual
3537
3638if TYPE_CHECKING :
37- from textual . widget import Widget
39+ pass
3840
3941
4042ContentType : TypeAlias = Union ["Content" , str ]
@@ -275,7 +277,11 @@ def __lt__(self, other: object) -> bool:
275277 return self .plain < other .plain
276278 return NotImplemented
277279
278- def get_optimal_width (self , widget : Widget , container_width : int ) -> int :
280+ def get_optimal_width (
281+ self ,
282+ rules : RulesMap ,
283+ container_width : int ,
284+ ) -> int :
279285 """Get optimal width of the visual to display its content. Part of the Textual Visual protocol.
280286
281287 Args:
@@ -289,7 +295,7 @@ def get_optimal_width(self, widget: Widget, container_width: int) -> int:
289295 lines = self .without_spans .split ("\n " )
290296 return max (line .cell_length for line in lines )
291297
292- def get_height (self , widget : Widget , width : int ) -> int :
298+ def get_height (self , rules : RulesMap , width : int ) -> int :
293299 """Get the height of the visual if rendered with the given width. Part of the Textual Visual protocol.
294300
295301 Args:
@@ -365,37 +371,30 @@ def get_span(y: int) -> tuple[int, int] | None:
365371
366372 def render_strips (
367373 self ,
368- widget : Widget ,
374+ rules : RulesMap ,
369375 width : int ,
370376 height : int | None ,
371377 style : Style ,
378+ selection : Selection | None = None ,
379+ selection_style : Style | None = None ,
372380 ) -> list [Strip ]:
373381 if not width :
374382 return []
375383
376- selection = widget .selection
377- if selection is not None :
378- selection_style = Style .from_rich_style (
379- widget .screen .get_component_rich_style ("screen--selection" )
380- )
381-
382- else :
383- selection_style = None
384-
385384 lines = self ._wrap_and_format (
386385 width ,
387- align = widget . styles . text_align ,
386+ align = rules . get ( " text_align" , "left" ) ,
388387 overflow = "fold" ,
389388 no_wrap = False ,
390389 tab_size = 8 ,
391- selection = widget . selection ,
390+ selection = selection ,
392391 selection_style = selection_style ,
393392 )
394393
395394 if height is not None :
396395 lines = lines [:height ]
397396
398- strip_lines = [line .to_strip (widget , style ) for line in lines ]
397+ strip_lines = [Strip ( * line .to_strip (style ) ) for line in lines ]
399398 return strip_lines
400399
401400 def __len__ (self ) -> int :
@@ -1128,7 +1127,7 @@ def __init__(
11281127 def plain (self ) -> str :
11291128 return self .content .plain
11301129
1131- def to_strip (self , widget : Widget , style : Style ) -> Strip :
1130+ def to_strip (self , style : Style ) -> tuple [ list [ Segment ], int ] :
11321131 _Segment = Segment
11331132 align = self .align
11341133 width = self .width
@@ -1137,8 +1136,6 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
11371136 x = self .x
11381137 y = self .y
11391138
1140- parse_style = widget .app .stylesheet .parse_style
1141-
11421139 if align in ("start" , "left" ) or (align == "justify" and self .line_end ):
11431140 pass
11441141
@@ -1166,9 +1163,7 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
11661163 add_segment = segments .append
11671164 x = self .x
11681165 for index , word in enumerate (words ):
1169- for text , text_style in word .render (
1170- style , end = "" , parse_style = parse_style
1171- ):
1166+ for text , text_style in word .render (style , end = "" ):
11721167 add_segment (
11731168 _Segment (
11741169 text , (style + text_style ).rich_style_with_offset (x , y )
@@ -1178,16 +1173,15 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
11781173 if index < len (spaces ) and (pad := spaces [index ]):
11791174 add_segment (_Segment (" " * pad , (style + text_style ).rich_style ))
11801175
1181- strip = Strip (self ._apply_link_style (widget , segments ), width )
1182- return strip
1176+ return segments , width
11831177
11841178 segments = (
11851179 [Segment (" " * pad_left , style .background_style .rich_style )]
11861180 if pad_left
11871181 else []
11881182 )
11891183 add_segment = segments .append
1190- for text , text_style in content .render (style , end = "" , parse_style = parse_style ):
1184+ for text , text_style in content .render (style , end = "" ):
11911185 add_segment (
11921186 _Segment (text , (style + text_style ).rich_style_with_offset (x , y ))
11931187 )
@@ -1197,16 +1191,13 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
11971191 segments .append (
11981192 _Segment (" " * pad_right , style .background_style .rich_style )
11991193 )
1200- strip = Strip (
1201- self ._apply_link_style (widget , segments ),
1202- content .cell_length + pad_left + pad_right ,
1203- )
1204- return strip
1194+
1195+ return (segments , content .cell_length + pad_left + pad_right )
12051196
12061197 def _apply_link_style (
1207- self , widget : Widget , segments : list [Segment ]
1198+ self , link_style : RichStyle , segments : list [Segment ]
12081199 ) -> list [Segment ]:
1209- link_style = widget . link_style
1200+
12101201 _Segment = Segment
12111202 segments = [
12121203 _Segment (
0 commit comments