From 3201b238656245990cc451f40fa10d0e3ffe018f Mon Sep 17 00:00:00 2001 From: Johnson Wang Date: Sun, 22 Dec 2024 01:45:01 -0500 Subject: [PATCH] Revert "Merge pull request #186 from JonnyDing/fix/repr-ansi-issue" This reverts commit 31c074d46af142d21992c5497d7a77e46c689fc0, reversing changes made to da3d38ba0962b5b491ca54905c52f022ba19d92e. --- genesis/datatypes.py | 12 ++++-------- genesis/options/options.py | 8 ++------ genesis/repr_base.py | 12 ++++-------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/genesis/datatypes.py b/genesis/datatypes.py index 25f14fd..48cce0d 100644 --- a/genesis/datatypes.py +++ b/genesis/datatypes.py @@ -42,10 +42,10 @@ def _repr_elem(self, elem, common_length=0): def _repr_elem_colorized(self, elem, common_length=0): content = self._repr_elem(elem, common_length) idx = content.find(">") - formatted_content = f"{colors.BLUE}{formats.ITALIC}{content[:idx + 1]}{formats.RESET}{content[idx + 1:]}" + formatted_content = f"{colors.BLUE}{formats.ITALIC}{content[:idx+1]}{formats.RESET}{content[idx+1:]}" idx = formatted_content.find(":") if idx >= 0: - formatted_content = f"{formatted_content[:idx]}{colors.GRAY}:{colors.MINT}{formatted_content[idx + 1:]}" + formatted_content = f"{formatted_content[:idx]}{colors.GRAY}:{colors.MINT}{formatted_content[idx+1:]}" formatted_content += formats.RESET return formatted_content @@ -73,10 +73,6 @@ def _repr_brief(self): return repr_str def __repr__(self): - if not __debug__: - self.__colorized__repr__() - - def __colorized__repr__(self): repr_str = f"{colors.BLUE}{self._repr_type()}(len={colors.MINT}{formats.UNDERLINE}{len(self)}{formats.RESET}{colors.BLUE}, [" if len(self) == 0: @@ -123,8 +119,8 @@ def __colorized__repr__(self): right_line_len = line_len - left_line_len repr_str = ( - f"{colors.CORN}{'─' * left_line_len} {formats.BOLD}{formats.ITALIC}{header}{formats.RESET} {colors.CORN}{'─' * right_line_len}{formats.RESET}\n" - + repr_str + f"{colors.CORN}{'─' * left_line_len} {formats.BOLD}{formats.ITALIC}{header}{formats.RESET} {colors.CORN}{'─' * right_line_len}{formats.RESET}\n" + + repr_str ) return repr_str diff --git a/genesis/options/options.py b/genesis/options/options.py index cd46cfb..746b4fe 100644 --- a/genesis/options/options.py +++ b/genesis/options/options.py @@ -43,15 +43,11 @@ def copy_attributes_from(self, options, override=False): def _repr_type(cls): return f"<{cls.__module__}.{cls.__qualname__}>".replace("genesis", "gs") - def __repr__(self): - if not __debug__: - self.__colorized__repr__() - - def __colorized__repr__(self) -> str: + def __repr__(self) -> str: property_attrs = self.__dict__.keys() max_attr_len = max([len(attr) for attr in property_attrs]) - repr_str = f"{colors.CORN}{'─' * (max_attr_len + 3)} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─' * (max_attr_len + 3)}\n" + repr_str = f"{colors.CORN}{'─'*(max_attr_len + 3)} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─' * (max_attr_len + 3)}\n" for attr in property_attrs: formatted_str = f"{colors.BLUE}'{attr}'{formats.RESET}" diff --git a/genesis/repr_base.py b/genesis/repr_base.py index b00e527..544519d 100644 --- a/genesis/repr_base.py +++ b/genesis/repr_base.py @@ -31,11 +31,7 @@ def _repr_brief(self): repr_str += f", material: {self.material}" return repr_str - def __repr__(self): - if not __debug__: - self.__colorized__repr__() - - def __colorized__repr__(self) -> str: + def __repr__(self) -> str: all_attrs = self.__dir__() property_attrs = [] @@ -59,7 +55,7 @@ def __colorized__repr__(self) -> str: continue idx = content.find(">") # format with italic and color - formatted_content = f"{colors.MINT}{formats.ITALIC}{content[:idx + 1]}{formats.RESET}{colors.MINT}{content[idx + 1:]}{formats.RESET}" + formatted_content = f"{colors.MINT}{formats.ITALIC}{content[:idx+1]}{formats.RESET}{colors.MINT}{content[idx+1:]}{formats.RESET}" # in case it's multi-line if isinstance(getattr(self, attr), gs.List): # 4 = 2 x ' + : + space @@ -85,8 +81,8 @@ def __colorized__repr__(self) -> str: right_line_len = max(right_line_len, min_line_len) repr_str = ( - f"{colors.CORN}{'─' * left_line_len} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─' * right_line_len}\n" - + repr_str + f"{colors.CORN}{'─'*left_line_len} {formats.BOLD}{formats.ITALIC}{self._repr_type()}{formats.RESET} {colors.CORN}{'─'*right_line_len}\n" + + repr_str ) return repr_str