Skip to content

Commit

Permalink
Merge pull request #2903 from Textualize/style-reset
Browse files Browse the repository at this point in the history
Added Style.reset method
  • Loading branch information
willmcgugan committed Mar 27, 2023
2 parents 83c38eb + 635ca9d commit 076e0d2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <willmcgugan@gmail.com>"]
license = "MIT"
Expand Down
23 changes: 23 additions & 0 deletions rich/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 076e0d2

Please sign in to comment.