Skip to content

Commit

Permalink
refactor: Re-implement render style forced support
Browse files Browse the repository at this point in the history
- Change: Replace `enable_forced_support()` and
  `disable_forced_support()` with property `forced_support` in
  `BaseImage`.
  • Loading branch information
AnonymouX47 committed Mar 26, 2023
1 parent 475d22c commit 889a4ca
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions src/term_image/image/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
COLOR_RESET,
CSI,
ClassInstanceMethod,
ClassProperty,
ClassPropertyMeta,
cached,
get_cell_size,
Expand Down Expand Up @@ -348,6 +349,43 @@ def __str__(self) -> str:
""",
)

forced_support = ClassProperty(
lambda cls: cls._forced_support,
doc="""Render style forced support status
:type: bool
GET:
Returns the forced support status of the render style of the invoker.
SET:
Forced support is enabled or disabled for the render style of the invoker.
If forced support is:
* **enabled**, the render style is treated as if it were supported,
regardless of the return value of :py:meth:`is_supported`.
* **disabled**, the return value of :py:meth:`is_supported` determines if
the render style is supported or not.
By **default**, forced support is **disabled** by the base style class
(:py:class:`BaseImage`).
NOTE:
* This property is :term:`descendant`.
* This doesn't affect the return value of :py:meth:`is_supported` but
may affect operations that require that a render style be supported e.g
instantiation of some render style classes.
""",
)

@forced_support.setter
def forced_support(cls, status):
if not isinstance(status, bool):
raise TypeError(f"Invalid type for 'status' (got: {type(status).__name__})")

cls._forced_support = status

frame_duration = property(
lambda self: self._frame_duration if self._is_animated else None,
doc="""Duration of a single frame for :term:`animated` images
Expand Down Expand Up @@ -672,20 +710,6 @@ def close(self) -> None:
finally:
self._closed = True

@classmethod
def disable_forced_support(cls):
"""Disables forced support for a render style.
Causes the return value of :py:meth:`is_supported` determines if the render
style is supported or not, which is the default behaviour.
NOTE:
This setting is :term:`descendant` i.e it affects the class on which it
is disabled and all its subclasses **for which it is not enabled**
(via :py:meth:`enable_forced_support`).
"""
cls._forced_support = False

def draw(
self,
h_align: Optional[str] = None,
Expand Down Expand Up @@ -857,22 +881,6 @@ def render(image: PIL.Image.Image) -> None:
animated=not style.get("native") and self._is_animated and animate,
)

@classmethod
def enable_forced_support(cls):
"""Enables forced support for a render style.
Causes a render style to be treated as if it were supported, regardless of the
return value of :py:meth:`is_supported`.
NOTE:
This setting is :term:`descendant` i.e it affects the class on which it
is enabled and all its subclasses **for which it is not disabled**
(via :py:meth:`disable_forced_support`).
This doesn't influence the return value of :py:meth:`is_supported`.
"""
cls._forced_support = True

@classmethod
def from_file(
cls,
Expand Down Expand Up @@ -2027,6 +2035,10 @@ class GraphicsImage(BaseImage):
ATTENTION:
This class cannot be directly instantiated. Image instances should be created
from its subclasses.
TIP:
To allow instantiation regardless of whether the render style is supported or
not, enable :py:attr:`~term_image.image.BaseImage.forced_support`.
"""

# Size unit conversion already involves cell size calculation
Expand Down

0 comments on commit 889a4ca

Please sign in to comment.