From cac3b43e19a0217b80232d9678a0c36805b1928a Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 18 Feb 2023 14:34:17 -0800 Subject: [PATCH] Preserve existing behavior when editor is empty but make the message more clear --- jrnl/controller.py | 9 ++++++++- jrnl/editor.py | 3 +++ jrnl/exception.py | 4 ++++ jrnl/messages/MsgText.py | 8 ++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/jrnl/controller.py b/jrnl/controller.py index fee04199..e89e0176 100644 --- a/jrnl/controller.py +++ b/jrnl/controller.py @@ -311,7 +311,14 @@ def _edit_search_results( other_entries = _other_entries(journal, old_entries) # Send user to the editor - edited = get_text_from_editor(config, journal.editable_str()) + try: + edited = get_text_from_editor(config, journal.editable_str()) + except JrnlException as e: + if e.has_message_text(MsgText.NoTextReceived): + raise JrnlException(Message(MsgText.NoEditsReceivedJournalNotDeleted, MsgStyle.WARNING)) + else: + raise e + journal.parse_editable_str(edited) # Put back entries we separated earlier, sort, and write the journal diff --git a/jrnl/editor.py b/jrnl/editor.py index 89fcb35d..b6799b41 100644 --- a/jrnl/editor.py +++ b/jrnl/editor.py @@ -44,6 +44,9 @@ def get_text_from_editor(config: dict, template: str = "") -> str: raw = f.read() os.remove(tmpfile) + if not raw: + raise JrnlException(Message(MsgText.NoTextReceived, MsgStyle.NORMAL)) + return raw diff --git a/jrnl/exception.py b/jrnl/exception.py index 2679c855..2178ea18 100644 --- a/jrnl/exception.py +++ b/jrnl/exception.py @@ -7,6 +7,7 @@ from jrnl.output import print_msg if TYPE_CHECKING: from jrnl.messages import Message + from jrnl.messages import MsgText class JrnlException(Exception): @@ -18,3 +19,6 @@ class JrnlException(Exception): def print(self) -> None: for msg in self.messages: print_msg(msg) + + def has_message_text(self, message_text: "MsgText"): + return any([m.text == message_text for m in self.messages]) \ No newline at end of file diff --git a/jrnl/messages/MsgText.py b/jrnl/messages/MsgText.py index 4a8b6c61..b7eca910 100644 --- a/jrnl/messages/MsgText.py +++ b/jrnl/messages/MsgText.py @@ -151,6 +151,14 @@ class MsgText(Enum): https://jrnl.sh/en/stable/external-editors/ """ + NoEditsReceivedJournalNotDeleted = """ + No text received from editor. Were you trying to delete all the entries? + + This seems a bit drastic, so the operation was cancelled. + + To delete all entries, use the --delete option. + """ + NoEditsReceived = "No edits to save, because nothing was changed" NoTextReceived = """