Skip to content

Commit

Permalink
refactor ModeManager
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Jul 11, 2024
1 parent e0509d8 commit 20ca409
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
19 changes: 8 additions & 11 deletions termspark/painter/mode_manager.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
from importlib import import_module
from typing import Union

from .modes.hex import HEX
from .modes.name import Name
from .modes.rgb import RGB
from termspark.painter.modes.mode import Mode


class ModeManager:
__mode: Union[RGB, HEX, Name]

def __init__(self, color: Union[str, tuple]):
self._color = color

if RGB.check(color):
self.__mode = RGB(color)
elif HEX.check(color):
self.__mode = HEX(color)
else:
self.__mode = Name(color)
for mode in Mode.__subclasses__():
_module = import_module(f"termspark.painter.modes.{mode.__name__.lower()}")
_class = getattr(_module, mode.__name__)
if _class.check(color):
self.__mode = _class(color)
break

def format(self):
return self.__mode.format()
5 changes: 5 additions & 0 deletions termspark/painter/modes/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@


class Mode(ABC):
@staticmethod
@abstractmethod
def check(color: Union[str, tuple, None]) -> bool:
pass

@abstractmethod
def format(self) -> Union[str, bool]:
pass
4 changes: 4 additions & 0 deletions termspark/painter/modes/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ def __init__(self, color: Union[str, tuple]):
assert type(color) == str
self.__color = getattr(Color, color.upper(), color)

@staticmethod
def check(color: Union[str, tuple, None]) -> bool:
return True

def format(self) -> Union[str, bool]:
return RGB(self.__color).format()

0 comments on commit 20ca409

Please sign in to comment.