From 545f9b260a979d600c408d9fe96b185e38678287 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 27 Feb 2021 13:15:51 +0000 Subject: [PATCH] optimized options --- CHANGELOG.md | 10 ++++++++++ pyproject.toml | 2 +- rich/console.py | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be62cdad..f9327e96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ 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). +## [9.12.2] - 2021-02-27 + +### Added + +- Added ConsoleOptions.copy + +### Changed + +- Optimized ConsoleOptions.update + ## [9.12.1] - 2021-02-27 ### Fixed diff --git a/pyproject.toml b/pyproject.toml index d3d915d9..311086c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "rich" homepage = "https://github.com/willmcgugan/rich" documentation = "https://rich.readthedocs.io/en/latest/" -version = "9.12.1" +version = "9.12.2" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" authors = ["Will McGugan "] license = "MIT" diff --git a/rich/console.py b/rich/console.py index 9a5dbe64..28108b3b 100644 --- a/rich/console.py +++ b/rich/console.py @@ -133,6 +133,16 @@ class ConsoleOptions: """Check if renderables should use ascii only.""" return not self.encoding.startswith("utf") + def copy(self) -> "ConsoleOptions": + """Return a copy of the options. + + Returns: + ConsoleOptions: a copy of self. + """ + options = ConsoleOptions.__new__(ConsoleOptions) + options.__dict__ = self.__dict__.copy() + return options + def update( self, width: Union[int, NoChange] = NO_CHANGE, @@ -145,7 +155,7 @@ class ConsoleOptions: height: Union[Optional[int], NoChange] = NO_CHANGE, ) -> "ConsoleOptions": """Update values, return a copy.""" - options = replace(self) + options = self.copy() if not isinstance(width, NoChange): options.min_width = options.max_width = max(0, width) if not isinstance(min_width, NoChange): @@ -173,8 +183,8 @@ class ConsoleOptions: Returns: ~ConsoleOptions: New console options instance """ - width = max(0, width) - options = replace(self, min_width=width, max_width=width) + options = self.copy() + options.min_width = options.max_width = max(0, width) return options