From 70157303d435a30d05151502651f6ec0bc2eef46 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Tue, 17 May 2022 12:04:24 -0700 Subject: [PATCH] clean up print_msgs function --- jrnl/cli.py | 2 +- jrnl/editor.py | 2 +- jrnl/messages.py | 99 +++++++++++++++++++++-------------- jrnl/output.py | 59 ++++++--------------- jrnl/plugins/jrnl_importer.py | 2 +- jrnl/prompt.py | 2 +- 6 files changed, 81 insertions(+), 85 deletions(-) diff --git a/jrnl/cli.py b/jrnl/cli.py index 9bcb324c..305e4502 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -49,7 +49,7 @@ def cli(manual_args=None): print_msg( Message( MsgText.KeyboardInterruptMsg, - MsgStyle.ERROR, + MsgStyle.ERROR_ON_NEW_LINE, ) ) diff --git a/jrnl/editor.py b/jrnl/editor.py index 68e49fef..91aa4d52 100644 --- a/jrnl/editor.py +++ b/jrnl/editor.py @@ -66,7 +66,7 @@ def get_text_from_stdin(): except KeyboardInterrupt: logging.error("Write mode: keyboard interrupt") raise JrnlException( - Message(MsgText.KeyboardInterruptMsg, MsgStyle.ERROR), + Message(MsgText.KeyboardInterruptMsg, MsgStyle.ERROR_ON_NEW_LINE), Message(MsgText.JournalNotSaved, MsgStyle.WARNING), ) diff --git a/jrnl/messages.py b/jrnl/messages.py index 6733835f..ef763797 100644 --- a/jrnl/messages.py +++ b/jrnl/messages.py @@ -44,45 +44,6 @@ class MsgDecoration(Enum): return self.value["args"] -class MsgStyle(Enum): - BARE = { - "decoration": MsgDecoration.NONE, - "color": _MsgColor("white"), - } - PLAIN = { - "decoration": MsgDecoration.BRACKET, - "color": _MsgColor("white"), - } - PROMPT = { - "decoration": MsgDecoration.NONE, - "color": _MsgColor("white"), - } - 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"), - } - - @property - def decoration(self) -> MsgDecoration: - return self.value["decoration"] - - @property - def color(self) -> _MsgColor: - return self.value["color"].color - - class MsgText(Enum): def __str__(self) -> str: return self.value @@ -122,6 +83,7 @@ class MsgText(Enum): OneCharacterNo = "n" # --- Exceptions ---# + Error = "Error" UncaughtException = """ {name} {exception} @@ -320,6 +282,65 @@ class MsgText(Enum): """ +class MsgStyle(Enum): + BARE = { + "decoration": MsgDecoration.NONE, + "color": _MsgColor("white"), + } + PLAIN = { + "decoration": MsgDecoration.BRACKET, + "color": _MsgColor("white"), + } + 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 diff --git a/jrnl/output.py b/jrnl/output.py index 02d0056a..69346773 100644 --- a/jrnl/output.py +++ b/jrnl/output.py @@ -39,74 +39,49 @@ def list_journals(configuration): return result -def print_msg(msg: Message, is_prompt: bool = False) -> Union[None, str]: - return print_msgs([msg], style=msg.style, is_prompt=is_prompt) +def print_msg(msg: Message) -> Union[None, str]: + """Helper function to print a single message""" + return print_msgs([msg], style=msg.style) def print_msgs( msgs: list[Message], delimiter: str = "\n", style: MsgStyle = MsgStyle.NORMAL, - is_prompt: bool = False, + get_input: bool = False, ) -> Union[None, str]: # Same as print_msg, but for a list - text = Text("") - decoration_callback = style.decoration.callback - args = style.decoration.args - prepend_newline = False - append_space = False + text = Text("", end="") + kwargs = style.decoration.args - for msg in msgs: - args = _add_extra_style_args_if_needed(args, msg=msg) - - if _needs_prepended_newline(msg): - prepend_newline = True - - if _needs_appended_space(msg): - append_space = True + for i, msg in enumerate(msgs): + kwargs = _add_extra_style_args_if_needed(kwargs, msg=msg) m = format_msg_text(msg) - m.append(delimiter) + + if i != len(msgs) - 1: + m.append(delimiter) text.append(m) - text.rstrip() - - if append_space: + if style.append_space: text.append(" ") + decorated_text = style.decoration.callback(text, **kwargs) + # Always print messages to stderr console = Console(stderr=True) - decorated_text = decoration_callback(text, **args) - - if is_prompt: + if get_input: return str(console.input(prompt=decorated_text)) - else: - console.print(decorated_text, new_line_start=prepend_newline) + console.print(decorated_text, new_line_start=style.prepend_newline) def _add_extra_style_args_if_needed(args, msg): args["border_style"] = msg.style.color - args["title"] = "Error" if msg.style == MsgStyle.ERROR else None + args["title"] = msg.style.box_title return args -def _needs_prepended_newline(msg: Message) -> bool: - return is_keyboard_int(msg) - - -def _needs_appended_space(msg: Message) -> bool: - return is_prompt(msg) - - -def is_prompt(msg: Message) -> bool: - return msg.style == MsgStyle.PROMPT - - -def is_keyboard_int(msg: Message) -> bool: - return msg.text == MsgText.KeyboardInterruptMsg - - def format_msg_text(msg: Message) -> Text: text = textwrap.dedent(msg.text.value.format(**msg.params)).strip() return Text(text) diff --git a/jrnl/plugins/jrnl_importer.py b/jrnl/plugins/jrnl_importer.py index 39f53f85..cfdee0d7 100644 --- a/jrnl/plugins/jrnl_importer.py +++ b/jrnl/plugins/jrnl_importer.py @@ -29,7 +29,7 @@ class JRNLImporter: other_journal_txt = sys.stdin.read() except KeyboardInterrupt: raise JrnlException( - Message(MsgText.KeyboardInterruptMsg, MsgStyle.ERROR), + Message(MsgText.KeyboardInterruptMsg, MsgStyle.ERROR_ON_NEW_LINE), Message(MsgText.ImportAborted, MsgStyle.WARNING), ) diff --git a/jrnl/prompt.py b/jrnl/prompt.py index fcf179e9..91c9d69a 100644 --- a/jrnl/prompt.py +++ b/jrnl/prompt.py @@ -40,7 +40,7 @@ def yesno(prompt: Message, default: bool = True) -> bool: ], style=MsgStyle.PROMPT, delimiter=" ", - is_prompt=True, + get_input=True, ) answers = {