Skip to content

Commit

Permalink
Added reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Mar 26, 2023
1 parent 83c38eb commit b7ce2dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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).

## Unreleased

### Added

- Added Style.reset

## [13.3.2] - 2023-02-04

Expand Down
32 changes: 32 additions & 0 deletions rich/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,38 @@ def copy(self) -> "Style":
style._meta = self._meta
return style

@lru_cache(maxsize=128)
def reset(self, link: bool = True, meta: bool = True) -> "Style":
"""Get a copy of this style with optionally link and meta reset to default.
Args:
link (bool, optional): Reset links. Defaults to True.
meta (bool, optional): Reset meta. Defaults to True.
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
if link:
style._link = None
style._link_id = ""
else:
style._link = self._link
style._link_id = f"{randint(0, 999999)}" if self._link else ""
style._hash = self._hash
style._null = False

style._meta = None if meta else self._meta
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

0 comments on commit b7ce2dc

Please sign in to comment.