diff --git a/rich/segment.py b/rich/segment.py index 26ee041d..3222f3d2 100644 --- a/rich/segment.py +++ b/rich/segment.py @@ -312,7 +312,7 @@ class Segment(NamedTuple): yield segment else: text, style, _is_control = segment - yield cls(text, style.copy(link=None) if style else None) + yield cls(text, style.update_link(None) if style else None) @classmethod def strip_styles(cls, segments: Iterable["Segment"]) -> Iterable["Segment"]: diff --git a/rich/style.py b/rich/style.py index 707324a9..4eca6361 100644 --- a/rich/style.py +++ b/rich/style.py @@ -501,7 +501,7 @@ class Style: iter_styles = iter(styles) return sum(iter_styles, next(iter_styles)) - def copy(self, link: str = None) -> "Style": + def copy(self) -> "Style": """Get a copy of this style. Returns: @@ -516,9 +516,30 @@ class Style: style._bgcolor = self._bgcolor style._attributes = self._attributes style._set_attributes = self._set_attributes - _link = self._link if link is None else link - style._link = _link - style._link_id = f"{time()}-{randint(0, 999999)}" if _link else "" + style._link = self._link + style._link_id = f"{time()}-{randint(0, 999999)}" if self._link else "" + style._hash = self._hash + style._null = False + return style + + def update_link(self, link: str = None) -> "Style": + """Get a copy with a different value for link. + + Args: + link (str, optional): New value for link. Defaults to None. + + Returns: + Style: A new Style instance. + """ + style = self.__new__(Style) + style._ansi = self._ansi + style._style_definition = self._style_definition + style._color = self._color + style._bgcolor = self._bgcolor + style._attributes = self._attributes + style._set_attributes = self._set_attributes + style._link = link + style._link_id = f"{time()}-{randint(0, 999999)}" if link else "" style._hash = self._hash style._null = False return style diff --git a/tests/test_segment.py b/tests/test_segment.py index c5e24995..04e01ad8 100644 --- a/tests/test_segment.py +++ b/tests/test_segment.py @@ -86,3 +86,13 @@ def test_filter_control(): assert list(Segment.filter_control(segments, is_control=True)) == [ Segment("bar", is_control=True) ] + + +def test_strip_styles(): + segments = [Segment("foo", Style(bold=True))] + assert list(Segment.strip_styles(segments)) == [Segment("foo", None)] + + +def test_strip_links(): + segments = [Segment("foo", Style(bold=True, link="https://www.example.org"))] + assert list(Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))] \ No newline at end of file