This commit is contained in:
Jonathan Wren 2022-04-16 15:19:38 -07:00
parent f1bd01594e
commit 4b9fe0cd4f
5 changed files with 101 additions and 26 deletions

View file

@ -45,7 +45,13 @@ def cli(manual_args=None):
except KeyboardInterrupt: except KeyboardInterrupt:
status_code = 1 status_code = 1
print_msg("\nKeyboardInterrupt", "\nAborted by user", msg=Message.ERROR)
print_msg(
Message(
MsgText.KeyboardInterruptMsg,
MsgType.ERROR,
)
)
except Exception as e: except Exception as e:
# uncaught exception # uncaught exception

View file

@ -15,6 +15,7 @@ from .editor import get_text_from_stdin
from . import time from . import time
from .override import apply_overrides from .override import apply_overrides
from jrnl.output import print_msg from jrnl.output import print_msg
from jrnl.output import print_msgs
from jrnl.exception import JrnlException from jrnl.exception import JrnlException
from jrnl.messages import Message 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 'Write mode: appending raw text to journal "%s": %s', args.journal_name, raw
) )
journal.new_entry(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() journal.write()
logging.debug("Write mode: completed journal.write()", args.journal_name, raw) 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), "deleted": old_stats["count"] - len(journal),
"modified": len([e for e in journal.entries if e.modified]), "modified": len([e for e in journal.entries if e.modified]),
} }
stats["modified"] -= stats["added"]
prompts = [] msgs = []
if stats["added"] > 0: if stats["added"] > 0:
prompts.append(f"{stats['added']} {_pluralize_entry(stats['added'])} added") my_msg = (
stats["modified"] -= stats["added"] MsgText.JournalCountAddedSingular
if stats["added"] == 1
else MsgText.JournalCountAddedPlural
)
msgs.append(Message(my_msg, MsgType.NORMAL, {"num": stats["added"]}))
if stats["deleted"] > 0: if stats["deleted"] > 0:
prompts.append( my_msg = (
f"{stats['deleted']} {_pluralize_entry(stats['deleted'])} deleted" MsgText.JournalCountDeletedSingular
if stats["added"] == 1
else MsgText.JournalCountDeletedPlural
) )
msgs.append(Message(my_msg, MsgType.NORMAL, {"num": stats["deleted"]}))
if stats["modified"]: if stats["modified"] > 0:
prompts.append( my_msg = (
f"{stats['modified']} {_pluralize_entry(stats['modified'])} modified" MsgText.JournalCountModifiedSingular
if stats["added"] == 1
else MsgText.JournalCountModifiedPlural
) )
msgs.append(Message(my_msg, MsgType.NORMAL, {"num": stats["modified"]}))
if prompts: print_msgs(msgs)
print(f"[{', '.join(prompts).capitalize()}]", file=sys.stderr)
def _get_predit_stats(journal): def _get_predit_stats(journal):
return {"count": len(journal)} return {"count": len(journal)}
def _pluralize_entry(num):
return "entry" if num == 1 else "entries"
def _delete_search_results(journal, old_entries, **kwargs): def _delete_search_results(journal, old_entries, **kwargs):
if not journal.entries: if not journal.entries:
raise JrnlException(Message(MsgText.NothingToDelete, MsgType.ERROR)) raise JrnlException(Message(MsgText.NothingToDelete, MsgType.ERROR))

View file

@ -11,7 +11,7 @@ class _MsgColor(NamedTuple):
class MsgType(Enum): class MsgType(Enum):
TITLE = _MsgColor("cyan") TITLE = _MsgColor("cyan")
NORMAL = _MsgColor("white") NORMAL = _MsgColor("blue")
WARNING = _MsgColor("yellow") WARNING = _MsgColor("yellow")
ERROR = _MsgColor("red") ERROR = _MsgColor("red")
@ -34,6 +34,7 @@ class MsgText(Enum):
""" """
ConfigDirectoryIsFile = """ ConfigDirectoryIsFile = """
Problem with config file!
The path to your jrnl configuration directory is a file, not a directory: The path to your jrnl configuration directory is a file, not a directory:
{config_directory_path} {config_directory_path}
@ -73,6 +74,14 @@ class MsgText(Enum):
JournalNotSaved = "Entry NOT saved to journal" JournalNotSaved = "Entry NOT saved to journal"
JournalEntryAdded = "Entry added to {journal_name} 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 ---# # --- Editor ---#
WritingEntryStart = """ WritingEntryStart = """
Writing Entry Writing Entry

View file

@ -4,11 +4,23 @@
import logging import logging
import sys import sys
import textwrap 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 colorize
from jrnl.color import RESET_COLOR from jrnl.color import RESET_COLOR
from jrnl.color import WARNING_COLOR from jrnl.color import WARNING_COLOR
from jrnl.messages import Message 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): def deprecated_cmd(old_cmd, new_cmd, callback=None, **kwargs):
@ -38,14 +50,49 @@ def list_journals(configuration):
return result return result
def print_msg(msg: Message): def print_msg(msg: Message) -> None:
msg_text = textwrap.dedent(msg.text.value.format(**msg.params)).strip().split("\n") 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, def print_msgs(msgs: list[Message], delimiter: str = "\n") -> None:
# because python gets confused by the ansi color codes # Same as print_msg, but for a list
msg_text[0] = f"[{colorize(msg_text[0][1:-1], msg.type.color)}]" 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

View file

@ -49,6 +49,7 @@ tzlocal = ">2.0, <3.0" # https://github.com/regebro/tzlocal/blob/master/CHANGE
pytest = { version = ">=6.2", optional = true } pytest = { version = ">=6.2", optional = true }
pytest-bdd = { version = ">=4.0.1", optional = true } pytest-bdd = { version = ">=4.0.1", optional = true }
toml = { version = ">=0.10", optional = true } toml = { version = ">=0.10", optional = true }
rich = "^12.2.0"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
mkdocs = ">=1.0" mkdocs = ">=1.0"