Track added/deleted counts in a register in the Journal class instead of attempting to infer it via controller counting

This commit is contained in:
Micah Jerome Ellison 2023-03-01 12:17:36 -08:00 committed by Jonathan Wren
parent 9f44ab22ce
commit 7eec231095
4 changed files with 39 additions and 33 deletions

View file

@ -79,10 +79,6 @@ def run(args: "Namespace"):
append_mode(**kwargs)
return
# Get stats now for summary later
old_stats = _get_predit_stats(journal)
logging.debug(f"old_stats: {old_stats}")
# If not append mode, then we're in search mode (only 2 modes exist)
search_mode(**kwargs)
entries_found_count = len(journal)
@ -92,7 +88,7 @@ def run(args: "Namespace"):
_perform_actions_on_search_results(**kwargs)
if entries_found_count != 0 and _has_action_args(args):
_print_edited_summary(journal, old_stats)
_print_changed_counts(journal)
else:
# display only occurs if no other action occurs
_display_search_results(**kwargs)
@ -188,11 +184,7 @@ def search_mode(args: "Namespace", journal: "Journal", **kwargs) -> None:
logging.debug("Search mode: starting")
# If no search args, then return all results (don't filter anything)
if (
not _has_search_args(args)
and not _has_display_args(args)
and not args.text
):
if not _has_search_args(args) and not _has_display_args(args) and not args.text:
logging.debug("Search mode: has no search args")
return
@ -281,9 +273,7 @@ def _print_entries_found_count(count: int, args: "Namespace") -> None:
logging.debug("Printing general summary")
my_msg = (
MsgText.EntryFoundCountSingular
if count == 1
else MsgText.EntryFoundCountPlural
MsgText.EntryFoundCountSingular if count == 1 else MsgText.EntryFoundCountPlural
)
print_msg(Message(my_msg, MsgStyle.NORMAL, {"num": count}))
@ -318,7 +308,9 @@ def _edit_search_results(
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))
raise JrnlException(
Message(MsgText.NoEditsReceivedJournalNotDeleted, MsgStyle.WARNING)
)
else:
raise e
@ -330,14 +322,8 @@ def _edit_search_results(
journal.write()
def _print_edited_summary(
journal: "Journal", old_stats: dict[str, int], **kwargs
) -> None:
stats = {
"added": len(journal) - old_stats["count"],
"deleted": old_stats["count"] - len(journal),
"modified": len([e for e in journal.entries if e.modified]),
}
def _print_changed_counts(journal: "Journal", **kwargs) -> None:
stats = journal.get_change_counts()
msgs = []
if stats["added"] > 0:
@ -380,7 +366,7 @@ def _delete_search_results(
entries_to_delete = journal.prompt_action_entries(MsgText.DeleteEntryQuestion)
journal.entries = old_entries
if entries_to_delete:
journal.delete_entries(entries_to_delete)

View file

@ -91,6 +91,7 @@ class Folder(Journal):
for entry in entries_to_delete:
self.entries.remove(entry)
self._diff_entry_dates.append(entry.date)
self.deleted_entry_count += 1
def change_date_entries(self, date: str, entries_to_change: list["Entry"]) -> None:
"""Changes entry dates to given date."""
@ -115,4 +116,6 @@ class Folder(Journal):
# modified and how many got deleted later.
for entry in mod_entries:
entry.modified = not any(entry == old_entry for old_entry in self.entries)
self.increment_change_counts_by_edit(mod_entries)
self.entries = mod_entries

View file

@ -51,6 +51,10 @@ class Journal:
self.entries = []
self.encryption_method = None
# Track changes to journal in session. Modified is tracked in Entry
self.added_entry_count = 0
self.deleted_entry_count = 0
def __len__(self):
"""Returns the number of entries"""
return len(self.entries)
@ -298,6 +302,7 @@ class Journal:
"""Deletes specific entries from a journal."""
for entry in entries_to_delete:
self.entries.remove(entry)
self.deleted_entry_count += 1
def change_date_entries(
self, date: datetime.datetime, entries_to_change: list[Entry]
@ -378,8 +383,24 @@ class Journal:
# modified and how many got deleted later.
for entry in mod_entries:
entry.modified = not any(entry == old_entry for old_entry in self.entries)
self.increment_change_counts_by_edit(mod_entries)
self.entries = mod_entries
def increment_change_counts_by_edit(self, mod_entries: Entry) -> None:
if len(mod_entries) > len(self.entries):
self.added_entry_count += len(mod_entries) - len(self.entries)
else:
self.deleted_entry_count += len(self.entries) - len(mod_entries)
def get_change_counts(self) -> dict:
return {
"added": self.added_entry_count,
"deleted": self.deleted_entry_count,
"modified": len([e for e in self.entries if e.modified]),
}
class LegacyJournal(Journal):
"""Legacy class to support opening journals formatted with the jrnl 1.x

View file

@ -36,10 +36,8 @@ Feature: Test combinations of edit, change-time, and delete
Y
Then the editor should have been called
And the error output should contain "3 entries found"
# 2 deleted minus 1 for the 1 added
And the error output should contain "1 entry deleted"
# Added entry counts as modified because it replaced a deleted entry
And the error output should contain "1 entry modified"
And the error output should contain "2 entries deleted"
And the error output should contain "1 entry added"
When we run "jrnl -99 --short"
Then the error output should contain "2 entries found"
And the output should be
@ -65,7 +63,7 @@ Feature: Test combinations of edit, change-time, and delete
N
Then the error output should contain "3 entries found"
And the error output should contain "1 entry deleted"
And the error output should contain "2 entries modified"
And the error output should contain "1 entry modified"
When we run "jrnl -99 --short"
Then the output should be
2020-08-31 14:32 A second entry in what I hope to be a long series.
@ -91,11 +89,9 @@ Feature: Test combinations of edit, change-time, and delete
Y
N
Then the error output should contain "3 entries found"
# deleted = 2 deleted minus 1 added by edit = 1
# modified = 2 modified by change-time minus 1 of those deleted plus 1 of the deleted replaced by edit = 2
# added = 1 added minus 2 deleted = none
And the error output should contain "1 entry deleted"
And the error output should contain "2 entries modified"
And the error output should contain "2 entries deleted"
And the error output should contain "1 entry modified" # only 1, because the other was deleted
And the error output should contain "1 entry added" # by edit
When we run "jrnl -99 --short"
Then the output should be
2022-04-23 10:30 The third entry finally after weeks without writing.