MCPcopy
hub / github.com/Textualize/rich / render

Method render

rich/text.py:719–776  ·  view source on GitHub ↗

Render the text as Segments. Args: console (Console): Console instance. end (Optional[str], optional): Optional end character. Returns: Iterable[Segment]: Result of render that may be written to the console.

(self, console: "Console", end: str = "")

Source from the content-addressed store, hash-verified

717 return Measurement(min_text_width, max_text_width)
718
719 def render(self, console: "Console", end: str = "") -> Iterable["Segment"]:
720 """Render the text as Segments.
721
722 Args:
723 console (Console): Console instance.
724 end (Optional[str], optional): Optional end character.
725
726 Returns:
727 Iterable[Segment]: Result of render that may be written to the console.
728 """
729 _Segment = Segment
730 text = self.plain
731 if not self._spans:
732 yield Segment(text)
733 if end:
734 yield _Segment(end)
735 return
736 get_style = partial(console.get_style, default=Style.null())
737
738 enumerated_spans = list(enumerate(self._spans, 1))
739 style_map = {index: get_style(span.style) for index, span in enumerated_spans}
740 style_map[0] = get_style(self.style)
741
742 spans = [
743 (0, False, 0),
744 *((span.start, False, index) for index, span in enumerated_spans),
745 *((span.end, True, index) for index, span in enumerated_spans),
746 (len(text), True, 0),
747 ]
748 spans.sort(key=itemgetter(0, 1))
749
750 stack: List[int] = []
751 stack_append = stack.append
752 stack_pop = stack.remove
753
754 style_cache: Dict[Tuple[Style, ...], Style] = {}
755 style_cache_get = style_cache.get
756 combine = Style.combine
757
758 def get_current_style() -> Style:
759 """Construct current style from stack."""
760 styles = tuple(style_map[_style_id] for _style_id in sorted(stack))
761 cached_style = style_cache_get(styles)
762 if cached_style is not None:
763 return cached_style
764 current_style = combine(styles)
765 style_cache[styles] = current_style
766 return current_style
767
768 for (offset, leaving, style_id), (next_offset, _, _) in zip(spans, spans[1:]):
769 if leaving:
770 stack_pop(style_id)
771 else:
772 stack_append(style_id)
773 if next_offset > offset:
774 yield _Segment(text[offset:next_offset], get_current_style())
775 if end:
776 yield _Segment(end)

Callers 14

test_renderFunction · 0.45
time_renderMethod · 0.45
__rich_console__Method · 0.45
_repr_mimebundle_Method · 0.45
__rich_console__Method · 0.45
_get_syntaxMethod · 0.45
render_annotationMethod · 0.45
segment.pyFile · 0.45
__rich_console__Method · 0.45
__rich_console__Method · 0.45
__rich_console__Method · 0.45

Calls 3

SegmentClass · 0.85
get_styleFunction · 0.85
nullMethod · 0.80

Tested by 1

test_renderFunction · 0.36