diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d6f5e54..2ef99e5e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [13.3.3] - 2023-02-27 + +### Added + +- Added Style.clear_meta_and_links ## [13.3.2] - 2023-02-04 diff --git a/pyproject.toml b/pyproject.toml index 24c52f750..2a0e5c9c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "rich" homepage = "https://github.com/Textualize/rich" documentation = "https://rich.readthedocs.io/en/latest/" -version = "13.3.2" +version = "13.3.3" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" authors = ["Will McGugan "] license = "MIT" diff --git a/rich/style.py b/rich/style.py index ad388aadb..313c88949 100644 --- a/rich/style.py +++ b/rich/style.py @@ -645,6 +645,29 @@ def copy(self) -> "Style": style._meta = self._meta return style + @lru_cache(maxsize=128) + def clear_meta_and_links(self) -> "Style": + """Get a copy of this style with link and meta information removed. + + Returns: + Style: New style object. + """ + if self._null: + return NULL_STYLE + style: 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 = None + style._link_id = "" + style._hash = self._hash + style._null = False + style._meta = None + return style + def update_link(self, link: Optional[str] = None) -> "Style": """Get a copy with a different value for link. diff --git a/rich/text.py b/rich/text.py index 6c39cfa61..c534b3b19 100644 --- a/rich/text.py +++ b/rich/text.py @@ -1200,7 +1200,7 @@ def fit(self, width: int) -> Lines: width (int): Maximum characters in a line. Returns: - Lines: List of lines. + Lines: Lines container. """ lines: Lines = Lines() append = lines.append diff --git a/tests/test_style.py b/tests/test_style.py index bf02af838..b5c01729e 100644 --- a/tests/test_style.py +++ b/tests/test_style.py @@ -229,3 +229,25 @@ def test_from_meta(): def test_on(): style = Style.on({"foo": "bar"}, click="CLICK") + Style(color="red") assert style.meta == {"foo": "bar", "@click": "CLICK"} + + +def test_clear_meta_and_links(): + style = Style.parse("bold red on black link https://example.org") + Style.on( + click="CLICK" + ) + + assert style.meta == {"@click": "CLICK"} + assert style.link == "https://example.org" + assert style.color == Color.parse("red") + assert style.bgcolor == Color.parse("black") + assert style.bold + assert not style.italic + + clear_style = style.clear_meta_and_links() + + assert clear_style.meta == {} + assert clear_style.link == None + assert clear_style.color == Color.parse("red") + assert clear_style.bgcolor == Color.parse("black") + assert clear_style.bold + assert not clear_style.italic