From 4b9fe0cd4f54e56be26c39fa0fee28761d683bdd Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 16 Apr 2022 15:19:38 -0700 Subject: [PATCH] WIP --- jrnl/cli.py | 8 +++++- jrnl/jrnl.py | 44 +++++++++++++++++++++------------ jrnl/messages.py | 11 ++++++++- jrnl/output.py | 63 ++++++++++++++++++++++++++++++++++++++++++------ pyproject.toml | 1 + 5 files changed, 101 insertions(+), 26 deletions(-) diff --git a/jrnl/cli.py b/jrnl/cli.py index cd33f2ec..cd2275ef 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -45,7 +45,13 @@ def cli(manual_args=None): except KeyboardInterrupt: status_code = 1 - print_msg("\nKeyboardInterrupt", "\nAborted by user", msg=Message.ERROR) + + print_msg( + Message( + MsgText.KeyboardInterruptMsg, + MsgType.ERROR, + ) + ) except Exception as e: # uncaught exception diff --git a/jrnl/jrnl.py b/jrnl/jrnl.py index 24d77086..f93f4deb 100644 --- a/jrnl/jrnl.py +++ b/jrnl/jrnl.py @@ -15,6 +15,7 @@ from .editor import get_text_from_stdin from . import time from .override import apply_overrides from jrnl.output import print_msg +from jrnl.output import print_msgs from jrnl.exception import JrnlException from jrnl.messages import Message @@ -145,7 +146,13 @@ def write_mode(args, config, journal, **kwargs): 'Write mode: appending raw text to journal "%s": %s', args.journal_name, raw ) journal.new_entry(raw) - print_msg( Message(MsgText.JournalEntryAdded, MsgType.NORMAL, {"journal_name": args.journal_name})) + print_msg( + Message( + MsgText.JournalEntryAdded, + MsgType.NORMAL, + {"journal_name": args.journal_name}, + ) + ) journal.write() logging.debug("Write mode: completed journal.write()", args.journal_name, raw) @@ -279,35 +286,40 @@ def _print_edited_summary(journal, old_stats, **kwargs): "deleted": old_stats["count"] - len(journal), "modified": len([e for e in journal.entries if e.modified]), } - - prompts = [] + stats["modified"] -= stats["added"] + msgs = [] if stats["added"] > 0: - prompts.append(f"{stats['added']} {_pluralize_entry(stats['added'])} added") - stats["modified"] -= stats["added"] + my_msg = ( + MsgText.JournalCountAddedSingular + if stats["added"] == 1 + else MsgText.JournalCountAddedPlural + ) + msgs.append(Message(my_msg, MsgType.NORMAL, {"num": stats["added"]})) if stats["deleted"] > 0: - prompts.append( - f"{stats['deleted']} {_pluralize_entry(stats['deleted'])} deleted" + my_msg = ( + MsgText.JournalCountDeletedSingular + if stats["added"] == 1 + else MsgText.JournalCountDeletedPlural ) + msgs.append(Message(my_msg, MsgType.NORMAL, {"num": stats["deleted"]})) - if stats["modified"]: - prompts.append( - f"{stats['modified']} {_pluralize_entry(stats['modified'])} modified" + if stats["modified"] > 0: + my_msg = ( + MsgText.JournalCountModifiedSingular + if stats["added"] == 1 + else MsgText.JournalCountModifiedPlural ) + msgs.append(Message(my_msg, MsgType.NORMAL, {"num": stats["modified"]})) - if prompts: - print(f"[{', '.join(prompts).capitalize()}]", file=sys.stderr) + print_msgs(msgs) def _get_predit_stats(journal): return {"count": len(journal)} -def _pluralize_entry(num): - return "entry" if num == 1 else "entries" - - def _delete_search_results(journal, old_entries, **kwargs): if not journal.entries: raise JrnlException(Message(MsgText.NothingToDelete, MsgType.ERROR)) diff --git a/jrnl/messages.py b/jrnl/messages.py index 336fb33f..6d3ea094 100644 --- a/jrnl/messages.py +++ b/jrnl/messages.py @@ -11,7 +11,7 @@ class _MsgColor(NamedTuple): class MsgType(Enum): TITLE = _MsgColor("cyan") - NORMAL = _MsgColor("white") + NORMAL = _MsgColor("blue") WARNING = _MsgColor("yellow") ERROR = _MsgColor("red") @@ -34,6 +34,7 @@ class MsgText(Enum): """ ConfigDirectoryIsFile = """ + Problem with config file! The path to your jrnl configuration directory is a file, not a directory: {config_directory_path} @@ -73,6 +74,14 @@ class MsgText(Enum): JournalNotSaved = "Entry NOT saved to journal" JournalEntryAdded = "Entry added to {journal_name} journal" + JournalCountAddedSingular = "{num} entry added" + JournalCountModifiedSingular = "{num} entry modified" + JournalCountDeletedSingular = "{num} entry deleted" + + JournalCountAddedPlural = "{num} entries added" + JournalCountModifiedPlural = "{num} entries modified" + JournalCountDeletedPlural = "{num} entries deleted" + # --- Editor ---# WritingEntryStart = """ Writing Entry diff --git a/jrnl/output.py b/jrnl/output.py index f31a02e2..b32e4dda 100644 --- a/jrnl/output.py +++ b/jrnl/output.py @@ -4,11 +4,23 @@ import logging import sys import textwrap +from typing import Tuple + +from rich import print +import rich +from rich.panel import Panel +from rich.padding import Padding +from rich.text import Text +from rich.measure import Measurement +from rich.console import Group +from rich import box from jrnl.color import colorize from jrnl.color import RESET_COLOR from jrnl.color import WARNING_COLOR from jrnl.messages import Message +from jrnl.messages import MsgType +from jrnl.messages import MsgText def deprecated_cmd(old_cmd, new_cmd, callback=None, **kwargs): @@ -38,14 +50,49 @@ def list_journals(configuration): return result -def print_msg(msg: Message): - msg_text = textwrap.dedent(msg.text.value.format(**msg.params)).strip().split("\n") +def print_msg(msg: Message) -> None: + print_msgs([msg]) - longest_string = len(max(msg_text, key=len)) - msg_text = [f"[ {line:<{longest_string}} ]" for line in msg_text] - # colorize can't be called until after the lines are padded, - # because python gets confused by the ansi color codes - msg_text[0] = f"[{colorize(msg_text[0][1:-1], msg.type.color)}]" +def print_msgs(msgs: list[Message], delimiter: str = "\n") -> None: + # Same as print_msg, but for a list + text = Text("") - print("\n".join(msg_text), file=sys.stderr) + kwargs = { + "expand": False, + "border_style": None, + "padding": (0,2), + "title_align": "left", + "box": box.HEAVY + } + + for msg in msgs: + kwargs["border_style"] = msg.type.color + if msg.type == MsgType.ERROR: + kwargs["title"] = "Error" + + if is_keyboard_int(msg): + print() + + m = format_msg(msg) + m.append(delimiter) + text.append(m) + + text.rstrip() + print(Panel(text, **kwargs)) + + +def is_keyboard_int(msg: Message) -> bool: + return msg.text == MsgText.KeyboardInterruptMsg + +def format_msg(msg: Message) -> Text: + text = ( + textwrap.dedent(msg.text.value.format(**msg.params)) + .strip() + # .splitlines(keepends=True) + ) + result = Text(text) + # result = Text(text[0]) + # result.stylize(msg.type.color) + # result.append("".join(text[1:])) + return result diff --git a/pyproject.toml b/pyproject.toml index 88861740..9695b9f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,6 +49,7 @@ tzlocal = ">2.0, <3.0" # https://github.com/regebro/tzlocal/blob/master/CHANGE pytest = { version = ">=6.2", optional = true } pytest-bdd = { version = ">=4.0.1", optional = true } toml = { version = ">=0.10", optional = true } +rich = "^12.2.0" [tool.poetry.dev-dependencies] mkdocs = ">=1.0"