Skip to content

Commit

Permalink
feat: create abstract messenger for extendability
Browse files Browse the repository at this point in the history
  • Loading branch information
aintbe committed Nov 6, 2024
1 parent af73b0d commit d24ff71
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 25 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# ohmygod - CLI Tool Powered by Buddha
# ohmygod - CLI Tool Powered by Gods

https://github.com/user-attachments/assets/9861c2c6-6bb2-4bc6-b575-e7c5dbbcd72b
ohmygod is an extension of `Console` class from [rich](<(https://github.com/Textualize/rich)>) library. If your program does not need a cute guardian god, you may want to use the original library instead.

https://github.com/user-attachments/assets/9861c2c6-6bb2-4bc6-b575-e7c5dbbcd72b

### Set up with Poetry

Expand Down
7 changes: 5 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ohmygod import OhMyGod, HolyMessage
from ohmygod import OhMyGod, Buddha
import time

omg = OhMyGod()
omg = OhMyGod(Buddha)

@omg.protect("> Praying for a long running process")
def long_running_process(fail: bool):
Expand All @@ -23,3 +23,6 @@ def long_running_process(fail: bool):
# Show an error message if the process fails
omg.clear()
omg.error(str(e))

# Print using messages defined by the package
omg.print(omg.quotes.BLESSING)
9 changes: 4 additions & 5 deletions ohmygod/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""OH MY GOD package built upon rich console interface"""

__version__ = "0.1.5"

__version__ = "0.2.0"

from .main import OhMyGod
from .message import Message as HolyMessage
from .utils import Color as HolyColor
from .utils import Color
from .messenger.buddha import Buddha

__all__ = ["OhMyGod", "HolyMessage", "HolyColor"]
__all__ = ["OhMyGod", "Color", "Buddha"]
22 changes: 15 additions & 7 deletions ohmygod/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@
import time
from queue import Queue

from . import message as Message
from .messenger.buddha import Buddha
from .messenger.messenger import _Messenger as Messenger


class OhMyGod(Console):
"""Console interface powered by Buddha"""
def __init__(self):
def __init__(self, messenger_cls: type[Messenger] = Buddha):
super().__init__()
self.messenger = messenger_cls()
self.bless()


def bless(self):
"""Give a blessing to the program"""
self.print(Text(Message._BLESSING))
self.print(Text(self.messenger.BLESSING))


@property
def quotes(self):
"""Retrieve importable messages for the messenger"""
return self.messenger.quotes


def protect(self, message: str = ""):
Expand All @@ -32,7 +40,7 @@ def pray():
with Live(auto_refresh=False, console=self) as live:
state = 0
while not signal.is_set():
prayer = Text(Message._PRAYER_ANIMATED[state % 2])
prayer = Text(self.messenger.PRAYER_ANIMATED[state % 2])
dots = "." * (state % 4)

live.update(prayer + message + dots)
Expand Down Expand Up @@ -71,18 +79,18 @@ def success(self, message: str = ""):
"""Print a success message to the screen"""
with Live(auto_refresh=False, console=self) as live:
for i in range(3):
live.update(Text(Message._HURRAY_ANIMATED[i % 2] + message))
live.update(Text(self.messenger.HURRAY_ANIMATED[i % 2] + message))
live.refresh()
if i < 2:
time.sleep(0.4 + 0.3 * i)


def error(self, message: str = ""):
"""Print an error message to the screen"""
self.print(Text(Message._ERROR_COLORED), end="")
self.print(Text(self.messenger.ERROR_COLORED), end="")

# Print characters in the error message one by one
for char in Message._ERROR_ANIMATION:
for char in self.messenger.ERROR_ANIMATION:
self.print(Text(char, style="red"), end="")
time.sleep(0.1)

Expand Down
46 changes: 39 additions & 7 deletions ohmygod/message.py → ohmygod/messenger/buddha.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rich.text import Text
from .utils import Color, _Message

from .messenger import _Messenger
from ..utils import Color, _Message


_BLESSING = _Message.from_str(r"""
Expand Down Expand Up @@ -71,10 +73,40 @@
_ERROR = _ERROR_TEMPLATE.clean() + _ERROR_ANIMATION


class Message:
"""Importable messages for the OhMyGod"""
class Buddha(_Messenger):
@property
def BLESSING(self):
return _BLESSING

@property
def PRAYER_ANIMATED(self):
return _PRAYER_ANIMATED

@property
def HURRAY_ANIMATED(self):
return _HURRAY_ANIMATED

@property
def ERROR_COLORED(self):
return _ERROR_COLORED

@property
def ERROR_ANIMATION(self):
return _ERROR_ANIMATION

BLESSING = Text(_BLESSING)
PRAYER = Text(_PRAYER)
HURRAY = Text(_HURRAY)
ERROR = Text(_ERROR)
class Quotes(_Messenger.Quotes):
@property
def BLESSING(self):
return Text(_BLESSING)

@property
def PRAYER(self):
return Text(_PRAYER)

@property
def HURRAY(self):
return Text(_HURRAY)

@property
def ERROR(self):
return Text(_ERROR)
58 changes: 58 additions & 0 deletions ohmygod/messenger/messenger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from abc import ABC, abstractmethod
from rich.text import Text
from typing import List

from ..utils import _Message


class _Messenger(ABC):
@property
@abstractmethod
def BLESSING(self) -> _Message:
pass

@property
@abstractmethod
def PRAYER_ANIMATED(self) -> List[_Message]:
pass

@property
@abstractmethod
def HURRAY_ANIMATED(self) -> List[_Message]:
pass

@property
@abstractmethod
def ERROR_COLORED(self) -> _Message:
pass

@property
@abstractmethod
def ERROR_ANIMATION(self) -> _Message:
pass

@property
def quotes(self) -> 'Quotes':
"""Importable messages for the OhMyGod"""
return self.Quotes()

class Quotes(ABC):
@property
@abstractmethod
def BLESSING(self) -> Text:
pass

@property
@abstractmethod
def PRAYER(self) -> Text:
pass

@property
@abstractmethod
def HURRAY(self) -> Text:
pass

@property
@abstractmethod
def ERROR(self) -> Text:
pass
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "ohmygod"
version = "0.1.5"
description = "Rich CLI tool powered by Buddha"
version = "0.2.0"
description = "Rich CLI tool powered by gods"
authors = ["Sori Lim <[email protected]>"]
license = "MIT"
readme = "README.md"
Expand Down

0 comments on commit d24ff71

Please sign in to comment.