diff --git a/jrnl/messages/Message.py b/jrnl/messages/Message.py new file mode 100644 index 00000000..2c02e8a0 --- /dev/null +++ b/jrnl/messages/Message.py @@ -0,0 +1,11 @@ +from typing import NamedTuple +from typing import Mapping + +from .MsgText import MsgText +from .MsgStyle import MsgStyle + + +class Message(NamedTuple): + text: MsgText + style: MsgStyle = MsgStyle.NORMAL + params: Mapping = {} diff --git a/jrnl/messages/MsgStyle.py b/jrnl/messages/MsgStyle.py new file mode 100644 index 00000000..41daa535 --- /dev/null +++ b/jrnl/messages/MsgStyle.py @@ -0,0 +1,89 @@ +from enum import Enum +from typing import NamedTuple +from typing import Callable +from rich.panel import Panel +from rich import box + +from .MsgText import MsgText + + +class MsgStyle(Enum): + class _Color(NamedTuple): + """ + String representing a standard color to display + see: https://rich.readthedocs.io/en/stable/appendix/colors.html + """ + + color: str + + class _Decoration(Enum): + NONE = { + "callback": lambda x, **_: x, + "args": {}, + } + BOX = { + "callback": Panel, + "args": { + "expand": False, + "padding": (0, 2), + "title_align": "left", + "box": box.HEAVY, + }, + } + + @property + def callback(self) -> Callable: + return self.value["callback"] + + @property + def args(self) -> dict: + return self.value["args"] + + PROMPT = { + "decoration": _Decoration.NONE, + "color": _Color("white"), + "append_space": True, + } + TITLE = { + "decoration": _Decoration.BOX, + "color": _Color("cyan"), + } + NORMAL = { + "decoration": _Decoration.BOX, + "color": _Color("white"), + } + WARNING = { + "decoration": _Decoration.BOX, + "color": _Color("yellow"), + } + ERROR = { + "decoration": _Decoration.BOX, + "color": _Color("red"), + "box_title": str(MsgText.Error), + } + ERROR_ON_NEW_LINE = { + "decoration": _Decoration.BOX, + "color": _Color("red"), + "prepend_newline": True, + "box_title": str(MsgText.Error), + } + + @property + def decoration(self) -> _Decoration: + return self.value["decoration"] + + @property + def color(self) -> _Color: + return self.value["color"].color + + @property + def prepend_newline(self) -> bool: + return self.value.get("prepend_newline", False) + + @property + def append_space(self) -> bool: + return self.value.get("append_space", False) + + @property + def box_title(self) -> MsgText: + return self.value.get("box_title", None) diff --git a/jrnl/messages.py b/jrnl/messages/MsgText.py similarity index 78% rename from jrnl/messages.py rename to jrnl/messages/MsgText.py index 7393f419..f8e85b60 100644 --- a/jrnl/messages.py +++ b/jrnl/messages/MsgText.py @@ -1,42 +1,4 @@ from enum import Enum -from typing import NamedTuple -from typing import Mapping -from typing import Callable -from rich.panel import Panel -from rich import box - - -class _MsgColor(NamedTuple): - """ - String representing a standard color to display - see: https://rich.readthedocs.io/en/stable/appendix/colors.html - """ - - color: str - - -class MsgDecoration(Enum): - NONE = { - "callback": lambda x, **_: x, - "args": {}, - } - BOX = { - "callback": Panel, - "args": { - "expand": False, - "padding": (0, 2), - "title_align": "left", - "box": box.HEAVY, - }, - } - - @property - def callback(self) -> Callable: - return self.value["callback"] - - @property - def args(self) -> dict: - return self.value["args"] class MsgText(Enum): @@ -284,60 +246,3 @@ class MsgText(Enum): The command {old_cmd} is deprecated and will be removed from jrnl soon. Please use {new_cmd} instead. """ - - -class MsgStyle(Enum): - PROMPT = { - "decoration": MsgDecoration.NONE, - "color": _MsgColor("white"), - "append_space": True, - } - TITLE = { - "decoration": MsgDecoration.BOX, - "color": _MsgColor("cyan"), - } - NORMAL = { - "decoration": MsgDecoration.BOX, - "color": _MsgColor("white"), - } - WARNING = { - "decoration": MsgDecoration.BOX, - "color": _MsgColor("yellow"), - } - ERROR = { - "decoration": MsgDecoration.BOX, - "color": _MsgColor("red"), - "box_title": str(MsgText.Error), - } - ERROR_ON_NEW_LINE = { - "decoration": MsgDecoration.BOX, - "color": _MsgColor("red"), - "prepend_newline": True, - "box_title": str(MsgText.Error), - } - - @property - def decoration(self) -> MsgDecoration: - return self.value["decoration"] - - @property - def color(self) -> _MsgColor: - return self.value["color"].color - - @property - def prepend_newline(self) -> bool: - return self.value.get("prepend_newline", False) - - @property - def append_space(self) -> bool: - return self.value.get("append_space", False) - - @property - def box_title(self) -> MsgText: - return self.value.get("box_title", None) - - -class Message(NamedTuple): - text: MsgText - style: MsgStyle = MsgStyle.NORMAL - params: Mapping = {} diff --git a/jrnl/messages/__init__.py b/jrnl/messages/__init__.py new file mode 100644 index 00000000..930fbe75 --- /dev/null +++ b/jrnl/messages/__init__.py @@ -0,0 +1,7 @@ +from .Message import Message +from .MsgStyle import MsgStyle +from .MsgText import MsgText + +Message = Message +MsgStyle = MsgStyle +MsgText = MsgText