Skip to content

Commit 076e0d2

Browse files
authored
Merge pull request #2903 from Textualize/style-reset
Added Style.reset method
2 parents 83c38eb + 635ca9d commit 076e0d2

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [13.3.3] - 2023-02-27
9+
10+
### Added
11+
12+
- Added Style.clear_meta_and_links
813

914
## [13.3.2] - 2023-02-04
1015

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/Textualize/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "13.3.2"
5+
version = "13.3.3"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <[email protected]>"]
88
license = "MIT"

rich/style.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,29 @@ def copy(self) -> "Style":
645645
style._meta = self._meta
646646
return style
647647

648+
@lru_cache(maxsize=128)
649+
def clear_meta_and_links(self) -> "Style":
650+
"""Get a copy of this style with link and meta information removed.
651+
652+
Returns:
653+
Style: New style object.
654+
"""
655+
if self._null:
656+
return NULL_STYLE
657+
style: Style = self.__new__(Style)
658+
style._ansi = self._ansi
659+
style._style_definition = self._style_definition
660+
style._color = self._color
661+
style._bgcolor = self._bgcolor
662+
style._attributes = self._attributes
663+
style._set_attributes = self._set_attributes
664+
style._link = None
665+
style._link_id = ""
666+
style._hash = self._hash
667+
style._null = False
668+
style._meta = None
669+
return style
670+
648671
def update_link(self, link: Optional[str] = None) -> "Style":
649672
"""Get a copy with a different value for link.
650673

rich/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ def fit(self, width: int) -> Lines:
12001200
width (int): Maximum characters in a line.
12011201
12021202
Returns:
1203-
Lines: List of lines.
1203+
Lines: Lines container.
12041204
"""
12051205
lines: Lines = Lines()
12061206
append = lines.append

tests/test_style.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,25 @@ def test_from_meta():
229229
def test_on():
230230
style = Style.on({"foo": "bar"}, click="CLICK") + Style(color="red")
231231
assert style.meta == {"foo": "bar", "@click": "CLICK"}
232+
233+
234+
def test_clear_meta_and_links():
235+
style = Style.parse("bold red on black link https://example.org") + Style.on(
236+
click="CLICK"
237+
)
238+
239+
assert style.meta == {"@click": "CLICK"}
240+
assert style.link == "https://example.org"
241+
assert style.color == Color.parse("red")
242+
assert style.bgcolor == Color.parse("black")
243+
assert style.bold
244+
assert not style.italic
245+
246+
clear_style = style.clear_meta_and_links()
247+
248+
assert clear_style.meta == {}
249+
assert clear_style.link == None
250+
assert clear_style.color == Color.parse("red")
251+
assert clear_style.bgcolor == Color.parse("black")
252+
assert clear_style.bold
253+
assert not clear_style.italic

0 commit comments

Comments
 (0)