19
19
from rich .cells import set_cell_size
20
20
from rich .console import OverflowMethod
21
21
from rich .segment import Segment , Segments
22
+ from rich .style import Style as RichStyle
22
23
from rich .terminal_theme import TerminalTheme
23
24
from rich .text import Text
24
25
from typing_extensions import Final , TypeAlias
27
28
from textual ._context import active_app
28
29
from textual ._loop import loop_last
29
30
from textual .color import Color
31
+ from textual .css .styles import RulesMap
30
32
from textual .css .types import TextAlign
31
33
from textual .selection import Selection
32
34
from textual .strip import Strip
33
35
from textual .style import Style
34
36
from textual .visual import Visual
35
37
36
38
if TYPE_CHECKING :
37
- from textual . widget import Widget
39
+ pass
38
40
39
41
40
42
ContentType : TypeAlias = Union ["Content" , str ]
@@ -275,7 +277,11 @@ def __lt__(self, other: object) -> bool:
275
277
return self .plain < other .plain
276
278
return NotImplemented
277
279
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 :
279
285
"""Get optimal width of the visual to display its content. Part of the Textual Visual protocol.
280
286
281
287
Args:
@@ -289,7 +295,7 @@ def get_optimal_width(self, widget: Widget, container_width: int) -> int:
289
295
lines = self .without_spans .split ("\n " )
290
296
return max (line .cell_length for line in lines )
291
297
292
- def get_height (self , widget : Widget , width : int ) -> int :
298
+ def get_height (self , rules : RulesMap , width : int ) -> int :
293
299
"""Get the height of the visual if rendered with the given width. Part of the Textual Visual protocol.
294
300
295
301
Args:
@@ -365,37 +371,30 @@ def get_span(y: int) -> tuple[int, int] | None:
365
371
366
372
def render_strips (
367
373
self ,
368
- widget : Widget ,
374
+ rules : RulesMap ,
369
375
width : int ,
370
376
height : int | None ,
371
377
style : Style ,
378
+ selection : Selection | None = None ,
379
+ selection_style : Style | None = None ,
372
380
) -> list [Strip ]:
373
381
if not width :
374
382
return []
375
383
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
-
385
384
lines = self ._wrap_and_format (
386
385
width ,
387
- align = widget . styles . text_align ,
386
+ align = rules . get ( " text_align" , "left" ) ,
388
387
overflow = "fold" ,
389
388
no_wrap = False ,
390
389
tab_size = 8 ,
391
- selection = widget . selection ,
390
+ selection = selection ,
392
391
selection_style = selection_style ,
393
392
)
394
393
395
394
if height is not None :
396
395
lines = lines [:height ]
397
396
398
- strip_lines = [line .to_strip (widget , style ) for line in lines ]
397
+ strip_lines = [Strip ( * line .to_strip (style ) ) for line in lines ]
399
398
return strip_lines
400
399
401
400
def __len__ (self ) -> int :
@@ -1128,7 +1127,7 @@ def __init__(
1128
1127
def plain (self ) -> str :
1129
1128
return self .content .plain
1130
1129
1131
- def to_strip (self , widget : Widget , style : Style ) -> Strip :
1130
+ def to_strip (self , style : Style ) -> tuple [ list [ Segment ], int ] :
1132
1131
_Segment = Segment
1133
1132
align = self .align
1134
1133
width = self .width
@@ -1137,8 +1136,6 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
1137
1136
x = self .x
1138
1137
y = self .y
1139
1138
1140
- parse_style = widget .app .stylesheet .parse_style
1141
-
1142
1139
if align in ("start" , "left" ) or (align == "justify" and self .line_end ):
1143
1140
pass
1144
1141
@@ -1166,9 +1163,7 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
1166
1163
add_segment = segments .append
1167
1164
x = self .x
1168
1165
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 = "" ):
1172
1167
add_segment (
1173
1168
_Segment (
1174
1169
text , (style + text_style ).rich_style_with_offset (x , y )
@@ -1178,16 +1173,15 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
1178
1173
if index < len (spaces ) and (pad := spaces [index ]):
1179
1174
add_segment (_Segment (" " * pad , (style + text_style ).rich_style ))
1180
1175
1181
- strip = Strip (self ._apply_link_style (widget , segments ), width )
1182
- return strip
1176
+ return segments , width
1183
1177
1184
1178
segments = (
1185
1179
[Segment (" " * pad_left , style .background_style .rich_style )]
1186
1180
if pad_left
1187
1181
else []
1188
1182
)
1189
1183
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 = "" ):
1191
1185
add_segment (
1192
1186
_Segment (text , (style + text_style ).rich_style_with_offset (x , y ))
1193
1187
)
@@ -1197,16 +1191,13 @@ def to_strip(self, widget: Widget, style: Style) -> Strip:
1197
1191
segments .append (
1198
1192
_Segment (" " * pad_right , style .background_style .rich_style )
1199
1193
)
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 )
1205
1196
1206
1197
def _apply_link_style (
1207
- self , widget : Widget , segments : list [Segment ]
1198
+ self , link_style : RichStyle , segments : list [Segment ]
1208
1199
) -> list [Segment ]:
1209
- link_style = widget . link_style
1200
+
1210
1201
_Segment = Segment
1211
1202
segments = [
1212
1203
_Segment (
0 commit comments